0

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.

5
  • It depends on what exactly you want it to do on each page load. Depending on that you may need a listener like chrome.tabs.onUpdated (example) or an entirely separate content script. Commented Sep 25, 2019 at 5:25
  • I need to tack a screenshot. now I using this API "chrome.browserAction.onClicked.addListener" Commented Sep 25, 2019 at 5:26
  • Here I want to take a screenshot of every webpage, after refresh extension it automatically runs and takes a screenshot without a click on the extension icon. Hope You Understood now! Commented Sep 25, 2019 at 5:30
  • I see, but Chrome doesn't allow extensions to make screenshots silently without user confirmation to prevent malware from spying on users. AFAIK the only solution is to use chrome.debugger API to send Page.captureScreenshot command, but that will show a warning panel about extension debugging above each tab. Commented Sep 25, 2019 at 5:40
  • Ah, you can hide the warning by running chrome with --silent-debugger-extension-api command line. Commented Sep 25, 2019 at 5:50

1 Answer 1

0

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 } }) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.