简体   繁体   English

Chrome:运行时的消息内容脚本。onInstalled

[英]Chrome: message content-script on runtime.onInstalled

I'm the maker of an addon called BeautifyTumblr which changes the apperance of Tumblr. 我是一个名为BeautifyTumblr的插件的制造商,该插件可更改Tumblr的外观。 I wish for my Chrome extension to automatically detect when it has been updated and display changelog to the user. 我希望我的Chrome扩展程序能够自动检测到更新时间,并向用户显示更改日志。 I use an event page with the chrome.runtime.onInstalled.addListener hook to detect when an update has occured, retrieve the changelog from a text file in the extension.. this all works fine, then when I want to forward it to my content script via chrome.tabs.sendmessage it just wont work, nothing ever happens, no errors no nothing. 我使用带有chrome.runtime.onInstalled.addListener钩子的事件页面来检测何时发生了更新,从扩展中的文本文件中检索更改日志。这一切都很好,然后当我想将其转发到我的内容时通过chrome.tabs.sendmessage编写脚本,它将无法正常工作,什么都不会发生,没有错误也没有。 I'm stumped. 我很沮丧

Any help would be much appreciated! 任何帮助将非常感激!

Event Page: 活动页面:

chrome.runtime.onInstalled.addListener(function (details) {
    "use strict";
    if (details.reason === "install") {

    } else if (details.reason === "update") {
        var thisVersion = chrome.runtime.getManifest().version, xmlDom, xmlhttp;
        xmlDom = null;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", chrome.extension.getURL("changelog.txt"), false);
        xmlhttp.send(null);
        xmlDom = xmlhttp.responseText;
        chrome.tabs.query({'url' : 'http://www.tumblr.com/*'}, function (tabs) {
            if (tabs.length > 0) {
                var mTab = tabs[0].id;
                chrome.tabs.update(mTab, {active: true});
                setTimeout(chrome.tabs.sendMessage(mTab, {beautifyTumblrUpdate: xmlDom}), 500);
            } else {
                chrome.tabs.create({'url' : 'http://www.tumblr.com/dashboard'}, function (tab) {
                    setTimeout(chrome.tabs.sendMessage(tab.id, {beautifyTumblrUpdate: xmlDom}), 500);
                });
            }
        });
    }
});

Relevant code in Content Script: 内容脚本中的相关代码:

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        "use strict";
        window.alert('test');
        if (request.beautifyTumblrUpdate) {
            window.alert(request.beautifyTumblrUpdate);
        } else if (request.beautifyTumblrInstall) {
            window.alert(request.beautifyTumblrInstall);
        }
    }
);

I am also seeing the same thing. 我也看到了同样的事情。 I am not a 100% sure but I think this happens because chrome shuts off connection between background page and "old" content scripts the moment the extension is updated. 我不是100%肯定的人,但是我认为这是因为chrome在扩展程序更新时会关闭后台页面和“旧”内容脚本之间的连接。 There's more info here in this bug : https://code.google.com/p/chromium/issues/detail?id=168263 此错误有更多信息: https : //code.google.com/p/chromium/issues/detail?id=168263

simple, use the following code in background, 简单,请在后台使用以下代码,

chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
chrome.tabs.create({ url: chrome.extension.getURL('welcome.html')});  
}

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM