2012-11-17 139 views
2

步驟來重現問題端口錯誤:無法建立連接。接收端不存在

一)具有以下

B中的所有代碼編寫的擴展)轉到https://stackoverflow.com/

c)單擊「熱門問題:標題enter image description here

您會收到一條錯誤消息,說「端口錯誤:無法建立連接。接收端不存在」

清單文件

{ 
    "name": "Demo", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "This is a demo", 

    "content_scripts": [ { 
     "all_frames": true, 
     "js": ["contentscript1.js" ], 
     "matches": [ "https://stackoverflow.com/" ] 
     }] 
    } 

Editor.html文件

<html> 
<head> 
<script src="editor.js"></script> 
</head> 
<body> 
<div id="content"></div> 
</body> 
</html> 

editor.js內文件

function onRequest(message,sender,call){ 
    document.getElementById("content").value = message; 
} 
chrome.extension.onMessage.addListener(onRequest); 

contentscript1.js

function bindevent(){ 
window.open(chrome.extension.getURL("editor.html"),"_blank","toolbar=no, titlebar=no,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=720, height=400"); 
    message = "Sample Message"; 
    chrome.extension.sendMessage(message,function (res){}); 

}; 

document.getElementById('h-top-questions').onclick=bindevent; 

任何建議?

+0

什麼是「不是default_popup html文件的HTML頁面」? –

+0

此HTML頁面包含'code''code'且span是內容的接收者;通過窗口打開此頁面的更多內容。打開()點擊的圖標也注入DOM – Sudarshan

+0

我的.crx中有2個html文件1)瀏覽器操作圖標被點擊時打開的文件2)我所指的文件 – Sudarshan

回答

1

我正在發送消息,同時子窗口/擴展頁面仍在加載;我完成加載後發送了消息,它解決了我的問題。

終極密碼

清單文件

{ 
    "name": "Demo", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "This is a demo", 

    "content_scripts": [ { 
     "all_frames": true, 
     "js": ["contentscript1.js" ], 
     "matches": [ "http://stackoverflow.com/" ] 
     }] 
    } 

Editor.html文件

<html> 
<head> 
<script src="editor.js"></script> 
</head> 
<body> 
<div id="content"></div> 
</body> 
</html> 

editor.js內文件

var message = window.location.hash.substring(1); 

contentscript1.js

function bindevent(){ 
message = "Sample Message"; 
    window.open(chrome.extension.getURL("editor.html")+"#"+message,"_blank","toolbar=no, titlebar=no,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=720, height=400"); 
    }; 

document.getElementById('h-top-questions').onclick=bindevent 

;

相關問題