Answer the question
In order to leave comments, you need to log in
Why doesn't messaging inside the background page work in Chrome extension?
From the popup and inline scripts in pages, chrome.runtime.sendMessage messages on the background page are visible, everything is ok. And sending inside the background page does not occur.
Manifest
"background": {
"scripts": ["surfer.js", "eventPage.js"],
"persistent": false
},
'use strict';
/**
* Surfer by profile pages
*
* @constructor
*/
function Surfer() {
}
Surfer.prototype.openWindows = function (urls) {
var self = this;
var urlCount = urls.length;
var stoppedWork = false;
// Listener status stopped
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type == 'stopOpenWindows') {
stoppedWork = true;
sendResponse('OK');
}
});
return new Promise(function (resolve, reject) {
var currentUrl = 0;
var urlOpen = function () {
if (currentUrl < urlCount) {
self.createTab(urls[currentUrl]).then(function (tab) {
chrome.runtime.sendMessage({type: 'newTab', tab: tab.id}, response => {
currentUrl++;
console.log('Send new tab', response);
urlOpen();
});
}).catch(function (err) {
console.log(err);
});
} else {
resolve();
}
};
if (!stoppedWork) {
urlOpen();
} else{
resolve();
}
});
};
'use strict';
let siteTabId;
let tabs = [];
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Run script
if (message.type == 'run') {
siteTabId = message.tabId;
chrome.tabs.executeScript(message.tabId, {
file: 'popupExecutor.js'
}, () => {
console.log('Script runned.');
sendResponse('OK');
});
}
// Handler received users
if (message.type == 'popupExecutor') {
new Surfer()
.openWindows(message.users.slice(2))
.then(() => {
console.log('Script is stopped!');
}).catch(err => console.log(err));
sendResponse('OK');
}
if (message.type == 'stopWork') {
chrome.runtime.sendMessage({type: 'stopOpenWindows'}, response => {
console.log('stepped');
sendResponse('OK');
});
}
console.log("Message eventPage", message);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question