Chrome扩展程序:在URL更新时调用后台

问题描述 投票:0回答:1

我正在尝试为某些网站创建自动登录扩展程序。

目标是创建一个后台脚本,每次活动选项卡URL更改时都会接收更新。

经过多次尝试,我仍然没有成功捕获该事件,因此无法调用我的代码。

我的manifest.json:

{
  "manifest_version": 2,
  "name": "OpenU AutoLogin",
  "description": "",
  "version": "1.0",
  "background": {
    "scripts": [
        "background.js"
    ]
},
  "browser_action": {
   "default_icon": "OUAL48.png"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js":["OUAL.js"]
    }
  ],
  "permissions": [
    "tabs",
    "activeTab",
    "http://*/*", "https://*/*", "<all_url>", "background"
  ]
}

我的background.is :( SendScriptIntoActiveTab函数工作得很好)

    chrome.tabs.onActivated.addListener(function(tabId, changeInfo, tab) {
    SendScriptIntoActiveTab({code:"console.log(changeInfo);"});
 });

 function SendScriptIntoActiveTab(code){
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id,code, function(response) {
         });
       });
  }

但遗憾的是,当我从后台跑步时,什么也没发生。任何人有任何想法为什么?

javascript google-chrome google-chrome-extension
1个回答
1
投票

chrome.tabs.onActivated签名是错误的。此外,您需要使用chrome.tabs.onUpdated来收听标签网址更改。

来自文档:

onActivated

窗口中的活动选项卡更改时触发。请注意,此事件触发时可能未设置选项卡的URL,但您可以侦听onUpdated事件,以便在设置URL时收到通知。

要检查活动选项卡URL是否已更改:

chrome.tabs.onActivated.addListener(function (activeInfo) {
  chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (activeInfo.tabId === tabId && changeInfo.url) {
       console.log(`URL has changed to ${changeInfo.url}`)
    }
  })
})
© www.soinside.com 2019 - 2024. All rights reserved.