如何在Google Chrome扩展程序上检测标签更改网址或标签?

xx3*_*004 22 google-chrome-extension

我有关于编写Google Chrome扩展程序的问题.我现在的目标是检测是否创建了选项卡或选项卡的URL已更改.

实际上,我想从在线链接中插入字典.js到Chrome上的任何网页,该脚本将作为background.html运行.例如,如果您打开浏览器并转到主页,它将运行脚本以将dictionary.js插入该页面.创建新选项卡或打开新页面时,它也会运行脚本.当人们更改tab的url时,它也会运行脚本.如何在这种情况下检测标签是否发生变化?好的,这是我的...代码,我想,解释一下.

chrome.someFunctionThatDetectTheSituationsAbove(function() {
    insertDictionaryScript();//I'd love to have the script of detection, not the insertDictionaryScript();
}
Run Code Online (Sandbox Code Playgroud)

任何想法我都会感激.谢谢.:P.

[X]

Sin*_*dar 54

只需在background.js上添加:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    insertDictionaryScript();
});

chrome.tabs.onCreated.addListener(function(tab) {         
   insertDictionaryScript();
});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的帮助!非常清楚!我想我现在知道该怎么做了.[X] (2认同)
  • @ vanduc1102-您可以检查网址是否更改,例如`if(changeInfo.url)myFunc();` (2认同)

kag*_*san 12

还有onActivated事件:

chrome.tabs.onActivated.addListener(function(tabId, changeInfo, tab) {
   insertDictionaryScript();
});
Run Code Online (Sandbox Code Playgroud)

  • `onActivated`回调签名不正确 - 它现在需要一个对象.请参阅http://developer.chrome.com/extensions/tabs.html#event-onActivated (5认同)

ser*_*erg 8

您所描述的内容称为内容脚本.您不需要任何编码,只需在清单文件中进行声明即可.更多关于内容脚本的信息.