Chrome扩展程序:从注入的脚本向后台脚本发回消息时发生错误 [英] Chrome extension : error when sending message back from injected script to background script

查看:101
本文介绍了Chrome扩展程序:从注入的脚本向后台脚本发回消息时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验Chrome扩展。我的目标是从后台脚本向注入的脚本发送消息,并让注入的脚本返回结果。



代码如下:

background.js

  chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab ){
chrome.tabs.executeScript(tabId,{'file':content.js},
function(){
chrome.tabs.sendMessage(tabId,{msg:'测试'},
函数(响应){
console.log(响应);
}
);
返回true;
}
);
});

content.js

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
sendResponse('test message');
return true;
}
);

我收到错误消息

 无法发送响应:如果您想在侦听器返回
content.js 中调用 sendResponse 时调用p>

我已经从听众返回true了.. confused?

我错过了什么明显的东西?

解决方案

我认为我的问题已被修正

  chrome.tabs.sendMessage(tabId, {action:'test'},

注意,我错误地使用 msg ,而不是 action 在上述问题中。 另外,我不使用第三个参数sendResponse以避免全双工通信
我只需将内容脚本中的另一条消息发送回后台页面。



从内容脚本中,使用以下命令将消息发送到后台页面:

 函数SendMessageToBackgroundPage(data)
{
chrome.runtime.sendMessage({
action:'功夫',
来源:'熊猫'
});

使用以下代码在背景页面中抓取它:

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
switch(request.action){
'kungfu':alert(request.source);
}
});


I was experimenting with chrome extension. My goal is to send a message from background script to injected script and let the injected script return back the result.

Code is as below:

background.js

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab){
    chrome.tabs.executeScript(tabId, {'file': "content.js"}, 
        function(){
            chrome.tabs.sendMessage(tabId, {msg: 'test'}, 
                function(response){
                    console.log(response);
                }
            );
            return true;
        }
    );
});

content.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        sendResponse('test message');
        return true;
    }
);

I get the error message

Could not send response: The chrome.runtime.onMessage listener must return true if you want to send a response after the listener returns 

when calling sendResponse inside content.js

I am already returning true from the listener.. confused?
Am I missing something obvious?

解决方案

I think my issue was fixed by

chrome.tabs.sendMessage(tabId, {action: 'test'}, 

Note, I incorrectly use msg instead of action in above question.

Also, I do not use the third parameter sendResponse to avoid full-duplex communication I simply post another message from content script back to background page.

From content script, send message to background page using:

function SendMessageToBackgroundPage(data)
{
    chrome.runtime.sendMessage({
        action: 'kungfu',
        source: 'panda'
    });
}

Catch it inside background page using:

chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            switch (request.action) {
                case 'kungfu': alert(request.source);
            }
        });

这篇关于Chrome扩展程序:从注入的脚本向后台脚本发回消息时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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