Reputation: 487
I started with chrome extensions three days ago and I like it quite a bit. I ran into a problem: I minimalized the script that reproduces my problem:
If I am on stackoverflow.com, I can click the icon and a popup will open that said 'worked', if it sent a message to the background script and received the message 'worked'.
If I now restart the browser, I get a popup that extensions in developer mode could be harmful and whether I want to deactivate them. I dismiss this message and when I now click the extension it doesn't work and I get the following error:
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
I figured out that the background script is NOT running (I get no alert from background.js or prints to the background.js console). I suppose the extension and therefore background.js was probably prevented from starting by chrome as it is an extension in developer mode. The only way to get the extension running again is to refresh the extension from chrome://extensions.
I tried it with persistent and non-persistent background script, but that didn't change the behaviour.
Is my analysis correct and therefore the only solution to deploy the script on the webstore to make it run properly? Or is there another way to make the app in developer mode start when chrome starts?
Here is the minimal example:
manifest.json
{
"name": "BackgroundScript Error",
"version": "0.0.1",
"manifest_version": 2,
"description": "When starting Chrome, background doesn't start, but when refreshing, background starts",
"page_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["declarativeContent", "<all_urls>"],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
background.js
chrome.runtime.onInstalled.addListener(function() {
console.log('Background script is running');
alert("Background is running");
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
pageUrl: {hostEquals: 'stackoverflow.com'},
})],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
console.log('setting up connection to popup');
chrome.runtime.onConnect.addListener(connected);
});
chrome.runtime.onMessage.addListener(function(request, sender) {
console.log(request.action);
});
function connected(p) {
console.log("connected to "+p);
p.postMessage({action: 'didSomething', result: 'worked' });
p.onDisconnect.addListener(disconnected);
}
function disconnected(p) {
console.log("disconnected from "+p);
}
popup.js
var myPort
function onWindowLoad() {
var message = document.getElementById('message');
myPort = chrome.runtime.connect({name:"port-from-cs"});
myPort.postMessage({request: "doSomething"});
myPort.onMessage.addListener(function(m) {
console.log("In popup script, received message from background script: ");
message.innerHTML = m.result;
});
}
window.onload = onWindowLoad;
popup.html
<!DOCTYPE html>
<html style=''>
<head>
<script src='popup.js' ></script>
</head>
<body>
<div id="message">Popup</div>
</body>
</html>
Thanks!
Upvotes: 1
Views: 3593
Reputation: 487
I figured out what to do:
as mentioned, the functions in the installed-listener are only called, when the script is actually installed (e.g. refresh) and not later when restarting the browser.
I found my Rookie mistake: I registered listeners asynchronously as is listed as a NOT TO DO in the Chrome documentation. In detail, this line should go out of the installed listener:
chrome.runtime.onConnect.addListener(connected);
thanks for pointing me in the right direction!
Upvotes: 1
Reputation: 1507
No, you don't need to deploy to the web store to get it to work.
You're having a problem because you have attached much of the background script activity to the chrome.runtime.onInstalled
listener - which only fires when the extension is installed or the manifest version changes. When you restarted the browser for the second time, your extension is installed already, the manifest version remains the same so that event won't fire.
Upvotes: 5