Chrome:消息内容 - 脚本在runtime.onInstalled [英] Chrome: message content-script on runtime.onInstalled

查看:1709
本文介绍了Chrome:消息内容 - 脚本在runtime.onInstalled的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是名为BeautifyTumblr的插件的制造商,它改变了Tumblr的外观。
我希望我的Chrome扩展程序能够自动检测它的更新时间并向用户显示更新日志。我使用chrome.runtime.onInstalled.addListener挂钩的事件页面来检测更新何时发生,从扩展中的文本文件检索更新日志..这一切正常,然后当我想转发它到我的内容脚本通过chrome.tabs.sendmessage它只是不会工作,什么都没有发生,没有错误没有什么。我很难过。

I'm the maker of an addon called BeautifyTumblr which changes the apperance of Tumblr. I wish for my Chrome extension to automatically detect when it has been updated and display changelog to the user. 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. 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);
        }
    }
);


推荐答案

简单,在后台使用下面的代码, p>

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')});  
}

});

这篇关于Chrome:消息内容 - 脚本在runtime.onInstalled的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆