【问题标题】:Chrome extension background message listener fires up twiceChrome 扩展后台消息监听器启动两次
【发布时间】:2015-10-26 12:42:47
【问题描述】:

我有一个像这样工作的 chrome 扩展:

后台每隔几秒检查站点 x 的更改,如果发生更改,后台会打开带有页面 Y 的新选项卡并执行一些页面自动化(在基于 ajax 的页面中填写调查)。页面自动化由内容脚本完成。内容脚本需要请求后台许可才能开始工作。它通过发送消息并等待回复来做到这一点。当内容脚本完成它的工作时,它会向后台发送消息,其中包含有关成功与否的信息。当失败选项卡应该使用新链接更新时,当成功时 - 选项卡应该关闭。问题是,有时 - 并非总是如此,免费获取消息两次。一旦 - 当标签失败时,它就会失败,而标签后的第二次是以新链接更新的。我希望我能知道为什么...

这是脚本:

Background.js 监听器创建

function create_listerner(){
    chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            switch(request.greeting){
                case "site_opened":
                    if (sender.tab.id == tabId) {
                        sendResponse({
                            farewell : "go"
                        });
                    } else {
                        sendResponse({
                            farewell : "stop"
                        });
                    }
                    break;
                case "imDoneWithSuccess":
                    //update tab logic
                    break;
                case "imDoneWithFail":
                    //close tab logic
                    actNgo(url,arr);
                    break;
            }
        });
}

当用户从上下文菜单启动脚本时,会进行一次侦听器调用:background.js

chrome.contextMenus.create({
    title : "Start",
    contexts : ["browser_action"],
    onclick : function () {
        create_listerner();
    }
});

标签更新看起来很简单:background.js

function actNgo(url,arr){
    chrome.storage.local.set({
        array : arr,
        ndz : 1
    }, function () {
        chrome.tabs.update(
            tabId,
            {
                "url" : url,
                "active" : false
            }, function (tab) {
                console.log("tabId "+tabId);
            });
    });
}

内容脚本被注入网站,网站不会像基于 ajax 的那样重新加载,即使 - 如果不满足适当的条件也无法发送消息

content.js

function SR(st) {
    var status ;
    if(st){
        status = "imDoneWithSuccess";
    } else {
        status = "imDoneWithFail";
    }
    chrome.runtime.sendMessage({
        greeting : status 
    }, function (response) {
    });
}

内容脚本应该只适用于 bacground.js 打开的标签

function askForPermission() {
    chrome.runtime.sendMessage({
        greeting : "site_opened"
    }, function (response) {
        if (response.farewell == 'go') {
            start();
        } else {
            console.log("bad tab");
        }
    });
}

再次 - 他们不可能自己开火。

日志文件如下所示:

Background: tab created;
content script: askForPermission(); <-sends message asking for permission to start
Bacground: go <- allows. 
content script: (content script logs, working as intended)
Background listener: imDoneWithFail;
Background update tab;
background listener: imDoneWithFail; <- shouldn't happen, doesn't look like it conten't script even started to work as it didn't ask for permission. Looks like listener is fired up twice...
Background update tab;
content script: askForPermission(); <-sends message asking for permission to start
Bacground: go <- allows. 

编辑:清单

{
  "name": "Test script",
  "version": "0.0.78",
  "manifest_version": 2,
  "background": {
      "scripts": ["src/background/background.js"]
    },
  "icons": {
    "19": "icons/icon19.png"
  },
  "default_locale": "en",
  "options_ui": {
    "page": "src/options/index.html",
    "chrome_style": true
  },
  "browser_action": {
    "default_icon": "icons/icon19.png",
    "default_title": "Test",
    "default_popup": "src/browser_action/browser_action.html"
  },
  "permissions": [
    "http://localhost/survey/*",
    "storage",
    "contextMenus",
      "tabs",
      "webRequest"
  ],
    "content_scripts": [
    {
        "matches": [
        "http://localhost/survey/*",
        "https://localhost/survey/*"
      ],
      "js": [
        "js/jquery/jquery.min.js",
        "src/inject/content.js"
      ]
    }
  ]
}

在有人问之前 - 我尝试使用长期连接,但我遇到了同样的问题。为什么会这样?

【问题讨论】:

  • 也许你的内容脚本被注入到所有帧中。您可以编辑问题并添加 manifest.json 吗?顺便说一句,您每次单击扩展工具栏图标时都会添加侦听器,尽管据说 Chrome 会忽略后续调用,因为回调是相同的。
  • @wOxxOm 我刚刚用清单更新了问题,我知道如果我将启动脚本几次,将会添加更多的侦听器,但我不这样做,所以它是别的东西......可以每个框架将自己的消息发送回后台?内容脚本代码根据日志文件只触发一次。
  • 好吧,我会在内容和后台脚本中发送和接收消息的所有行设置断点。你能做到吗?或者将扩展上传到某处并在此处链接?

标签: javascript ajax google-chrome-extension listener


【解决方案1】:

好的,我找到了答案,问题在于功能更改选项卡的 url。

function actNgo(url,arr){
    chrome.storage.local.set({
        array : arr,
        ndz : 1
    }, function () {
        chrome.tabs.update(
            tabId,
            {
                "url" : url,
                "active" : false
            }, function (tab) {
                console.log("tabId "+tabId);
            });
    });
}

功能,当给定与当前相同的 url 时,没有刷新选项卡,并且内容脚本无法像新的一样启动...仍然不知道为什么侦听器会两次启动消息,但是自从我的更改它已经停止。

我不知道这个修复是否对任何人都有帮助,但我通过首先将标签 URL 更改为空白,然后更改为新的来解决此问题。

    chrome.tabs.update(
        tabId,
        {
            "url" : "about:blank",
            "active" : false
        }, function (tab) {
            console.log("bcgr: aktualizujStroneIObstaw: pusta: tabId "+tabId);
            chrome.tabs.update(
                tabId,
                {
                    "url" : url_beta,
                    "active" : false
                }, function (tab) {
                    console.log("bcgr: aktualizujStroneIObstaw: wlasciwa: tabId "+tabId);

                }
              );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多