Q:

Chrome Extension: Call content script when needed

I am developing an extension and got an issue here. Is there a way to call content script when needed? Couldn't find a workaround about it.
run_at: 'document_idle'

didn't help.
EDIT: run_at at document_start starts in the beginning, document_end after loading. Content script loads automatically all the time, no way to execute it when needed.

A:

You can call chrome.tabs.executeScript to run a script in a webpage when you need it.
For example:
background.js
if (needed == true) {
    chrome.tabs.executeScript(tabId,{file:"myContentScript.js"},function(){
        console.log("run when needed");
    });
}

