Steps to reproduce problem
a) Prepare an extension with all the code below
b) Go to https://stackoverflow.com/
c) Click on “Top Questions: heading
You will get an error saying “Port error: Could not establish connection. Receiving end does not exist”
Manifest file
{
"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 file
<html>
<head>
<script src="editor.js"></script>
</head>
<body>
<div id="content"></div>
</body>
</html>
Editor.js file
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;
Any suggestions??
I am sending message while child window/extension page is still loading; I have sent message after it loaded completely and it solved my problem.
Final Code
Manifest file
Editor.html file
Editor.js File
contentscript1.js
;