Mat*_*att 10 google-chrome-extension
我刚刚将我的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"
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
的script.js:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
Run Code Online (Sandbox Code Playgroud)
现在我在[craigslist]上运行一个页面,然后转到控制台,这是错误:
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
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu 12.10 64位上使用Chrome测试版(谷歌浏览器:27.0.1453.15(官方版本191758)测试版)
Bea*_*ist 31
您正在从后台和内容脚本发送消息,但根本不尝试接收消息.尝试在其中一个或两个地方收听消息.此外,内联代码针对CSP,因此将其全部移动到外部文件.
例如:
的manifest.json
"background": {
"scripts": ["background.js"]
},
Run Code Online (Sandbox Code Playgroud)
background.js
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
sendResponse({farewell:"goodbye"});
});
Run Code Online (Sandbox Code Playgroud)
的script.js
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
Run Code Online (Sandbox Code Playgroud)
此外,chrome.tabs.getSelected()也已被弃用,因此请chrome.tabs.query()改用.
| 归档时间: |
|
| 查看次数: |
11864 次 |
| 最近记录: |