I have created a background script which runs on short keys and click on the extension icon but, now I want to run it on every web page load.
1 Answer
The background script already loads on every page request. If your looking to update the data between the background script and content script in your background process you need to create two listeners, the second is usefull when multiple tabs are open. Remember data in your popup can be retrieved by simply calling chrome.extension.getBackgroundPage()
background.js
chrome.tabs.onUpdated.addListener(() => { chrome.tabs.sendMessage(info.tabId, { message: 'DO_SOMETHING_MESSAGE' }); }) chrome.tabs.onActivated.addListener(() => { chrome.tabs.sendMessage(info.tabId, { message: 'DO_SOMETHING_MESSAGE' }); }) content.js should function similar to this.
const processContent = () => { // do whatever here. let data = {message: UPDATE_BACKGROUND_DATA} chrome.runtime.sendMessage(data); } // run when messages sent from background.js chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.message === 'DO_SOMETHING_MESSAGE') { processContent(); } }); // run onload processContent(); And finally, back in your background script create a listener that listens for any updated data.
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { if (msg.message === UPDATE_BACKGROUND_DATA) { // update background vars } })
--silent-debugger-extension-apicommand line.