我在Google Chrome Extension的contentscript.js文件中使用WebSocket对象时遇到问题。它仅为facebook.com主页生成错误。 Err.Message给了我不确定的价值。我没有得到任何线索,为什么它只在facebook.com上给出错误。在以下行产生错误
var ws = new WebSocket("ws://localhost:3345/");
以下是contentscript.js
的代码段:
/////////////////////SendMessage/////////////////////// <br/>
//Description: We are creating websocket(Chrome API). <br/>
/////////////////////////////////////////////////////// <br/>
function SendMessage(message) {
try {
if ("WebSocket" in window) {
//creating websocket(Chrome API)
//Below code line produces error on facebook.com
var ws = new WebSocket("ws://localhost:3345/");
//Hook function against ws.onmessage event.
ws.onmessage = function (evt) {
OnMessage(evt);
};
ws.onopen = function () {
ws.send( message););
};
}
}
catch (err) { alert("Err->" + err.Message); }
}
这是mainfest.json代码
{
"name" : "myextension",
"version" : "1.1",
"description" : "me extension detail",
"background" : {
"scripts": ["background.js"]
},
"content_scripts" : [
{
"matches" : [
"http://*/*",
"https://*/*"
],
"js" : ["contentscript.js"],
"run_at" : "document_end",
"all_frames" : true
}
],
"permissions": [
"tabs", "<all_urls>",
"*://*.facebook.com/",
"*://www.facebook.com/"
],
"content_security_policy": "script-src https://www.facebook.com 'self'; object-src 'self'; connect-src https://www.facebook.com",
"manifest_version": 2
}