popup.js无法接收来自后台Chrome扩展程序的邮件

时间:2018-01-28 18:10:03

标签: javascript google-chrome-extension sendmessage

我试试这个:

background.js

从popup.js接收消息(正常工作)

        chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
            console.log('chrome.runtime.onMessage');
            console.log(request);
            if (request&&request.method&&request.action) {
                if($.isFunction(getLr()[request.method][request.action])) {
                    sendResponse(getLr()[request.method][request.action](request.data?request.data:undefined));
                } else {
                    sendResponse(getLr()[request.method][request.action]);
                }
            }
        });

将数据发送到popup.js(不工作

    chrome.tabs.query({active: true}, function(tabs){
        for(let t in tabs) {
            chrome.tabs.sendMessage(tabs[t].id, $.extend({}, {method: method}, data), callback);
        }
    });

popup.js

发送到后台脚本(正常工作)

chrome.runtime.sendMessage({method: method, action: action, data: data}, callback);

从后台接收(不工作

window.onload = function () {
    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
        console.log('chrome.runtime.onMessage');
    });
};
什么错了?

0 个答案:

没有答案