一直扫描URL - Chrome浏览器扩展程序

3
我需要开发一个扩展程序,每次在Chrome浏览器中输入网址并按下回车时,该程序会首先扫描该网址,如果它符合某些模式,比如说如果该网址包含youtube,则将其重定向到facebook。
这应该是自动完成的 - 不是每次都要点击(chrome extension)图标,也就是说这个脚本或代码片段不会在单击事件上运行,而是一旦安装后,它将始终扫描输入的网址并执行所需的更改并重新加载选项卡。
请帮我解决问题。我已经做到了这一步。
<html>
   <script>
  function getname()
  { 

  chrome.tabs.getSelected( null , function(tab) {   


        var rawurl="http://www.youtube.com/watch?";
        var newurl= "http://www.youtube.com/watch?feature=player_leanback&"

           if (0 === tab.url.indexOf(rawurl)) 
          chrome.tabs.update(tab.id, {url: tab.url.replace(rawurl,newurl)});    
  });
  }

  </script>
   <body onload="getname();">

  </body>
</html>

我可以用J-Script的onload事件实现这个,但是有没有其他方法可以不用每次都需要显式点击,而是一直运行呢?
2个回答

1

重写代码

   <html>
   <script>
  function getname()
  { 

  chrome.tabs.getSelected( null , function(tab) {   


        var rawurl="http://www.youtube.com/watch?";
        var newurl= "http://www.youtube.com/watch?feature=player_leanback&"

           if (0 === tab.url.indexOf(rawurl)) 
          chrome.tabs.update(tab.id, {url: tab.url.replace(rawurl,newurl)});    
  });
  }

  </script>
   <body onload="getname();">

  </body>
</html>

0

你应该考虑使用像 chrome.webRequest 或者 chrome.webNavigation 这样的 API。

此外,我相信 this extension 已经实现了你提到的所有功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接