从内容脚本到后台HTML的Chrome扩展程序sendMessage错误 [英] Chrome Extension sendMessage error from content script to background html

查看:311
本文介绍了从内容脚本到后台HTML的Chrome扩展程序sendMessage错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的Chrome扩展更新为json版本2,并试图让我的扩展再次运行。问题是sendRequest一路贬值。因此,我将 https://developer.chrome.com/extensions/messaging.html 中的代码复制到我的脚本中并将其修改为我自己的变量名称,并且它不起作用。

然后我回去放入原始代码,它仍然不起作用。我已经阅读了多个类似的问题[并希望这不会因为重复而关闭,因为它们都与我的情况一样。]

manifest.json:

  {
background:{
page :background.html
},
... ... ...
content_scripts:[{
css:[style.css]] ,
js:[jq.js,script.js],
matches:[http://*.craigslist.org/*/*.htm* ]
}],
... ... ...
permissions:[tabs,http://*.craigslist.org/*/*.htm *],
manifest_version:2,
update_url:http://clients2.google.com/service/update2/crx,
version:3.0

background.html:

 < html> 
< script type ='text / javascript'>
chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
console.log(sender.tab?$ b $from a content script:+ sender .tab.url:
from the extension);
if(request.greeting ==hello)
sendResponse({farewell:goodbye});
});
});
< / script>
< / html>

script.js:

  chrome.runtime.sendMessage({greeting:hello},function(response){
console.log(response.farewell);
});

现在我在[在craigslist上]运行一个页面,并进入控制台,这是错误:

 端口错误:无法建立连接。接收结束不存在。 
TypeError:无法读取chrome-extension中未定义
的属性'farewell'://dhmjefbokfkjpdbigkadjpgjeflchgea/script.js:9:23

我在Ubuntu 12.10 64位版本中使用Chrome Beta(Google Chrome:27.0.1453.15(官方版本191758)beta)

解决方案

您正在从背景和内容脚本发送消息,但并未试图接收它们。尝试在其中一个或两个地方收听消息。另外,内联代码是针对CSP的 a>将其全部移至外部文件。



例如:

manifest.json $ b

 background:{
scripts:[background.js]
$,

background.js

  chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
sendResponse({farewell:goodbye});
});

script.js

  chrome.runtime.sendMessage({greeting:hello},function(response){
console.log(response.farewell);
} );

另外, chrome.tabs.getSelected()已被弃用,因此请使用 chrome.tabs .query() 代替。


I just updated my chrome extension to json version 2, and am trying to get my extension to work again. The problem is sendRequest was depreciated along the way. So I copy the code from https://developer.chrome.com/extensions/messaging.html into my script and modify it to my own variable names, and it doesn't work.

So then I go back and put in the original code and it still doesn't work. I have read multiple questions that are similar [and hopefully this won't get closed as a duplicate, because none of them were the same as my situation].

manifest.json:

{
   "background": {
        "page": "background.html"
        },
    ... ... ...
   "content_scripts": [ {
      "css": [ "style.css" ],
      "js": [ "jq.js", "script.js" ],
      "matches": [ "http://*.craigslist.org/*/*.htm*" ]
   } ],
   ... ... ...
   "permissions": [ "tabs", "http://*.craigslist.org/*/*.htm*" ],
   "manifest_version": 2,
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "3.0"
}

background.html:

<html>
<script type='text/javascript'>
   chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });
    });
</script>
</html>

script.js:

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});

Now I run a page [on craigslist], and go to the Console and this is the error:

Port error: Could not establish connection. Receiving end does not exist.
TypeError: Cannot read property 'farewell' of undefined
    at chrome-extension://dhmjefbokfkjpdbigkadjpgjeflchgea/script.js:9:23

I use Chrome Beta on Ubuntu 12.10 64-bit (Google Chrome: 27.0.1453.15 (Official Build 191758) beta)

解决方案

You are sending messages from both your background and your content script, but not trying to receive them at all. Try listening for messages in one or both of those places. Also, inline code is against the CSP so move it all to an external file.

For example:

manifest.json

"background": {
    "scripts": ["background.js"]
},

background.js

chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
  sendResponse({farewell:"goodbye"});
});

script.js

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});

Also, chrome.tabs.getSelected() has been deprecated as well, so use chrome.tabs.query() instead.

这篇关于从内容脚本到后台HTML的Chrome扩展程序sendMessage错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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