我看到了与我有类似问题的所有stackoverflow相关的答案。但他们都没有解决我的问题
我有这个:
的manifest.json
{
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["contentscript.js"],
"run_at": "document_end",
"all_frames": true
}
],
"web_accessible_resources": [
"script_inpage.js"
],
"browser_action": {
"default_icon": "icon19.png",
"default_popup": "popup.html",
"default_title": "Player"
},
"content_security_policy": "script-src 'self'; media-src *; object-src 'self'",
"description": "blah.",
"icons": {
"128": "icon128.png",
"16": "icon16.png",
"32": "icon32.png",
"48": "icon48.png"
},
"manifest_version": 2,
"minimum_chrome_version": "18",
"name": "Player",
"permissions": [
"unlimitedStorage",
"http://*/",
"tabs"
],
"update_url": "http://clients2.google.com/service/update2/crx",
"version": "2.6"
}
background.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="background.js"></script>
</head>
<body>
<audio src="" id="mplayer"></audio>
</body>
</html>
background.js
function playMusicFile() {
console.log("playMusicPlayer() from background.js");
var id="";
chrome.tabs.getSelected(null, function(tab) {
id=tab.id;
console.log(tab.id);
var port = chrome.tabs.connect(id);
port.postMessage({text: "post from plugin",name:"playermsg"});
});
document.getElementById("mplayer").play()
}
contentscript.js
var portRec = chrome.extension.connect();
chrome.extension.onConnect.addListener(function(port) {
portRec.onMessage.addListener(function(msg) {
alert(msg["text"]+" "+msg["name"]);
portRec.postMessage({text: "hello"});
});
});
我看到日志打印后不断收到此错误:
端口错误:无法建立连接。接收端不存在。 miscellaneous_bindings:235 chromeHidden.Port.dispatchOnDisconnect