It is not possible to “delete” old content scripts (other than reloading the page in question using window.location.reload, which would be bad)
If you want to be more flexible about what code you are executing in your script content, use the "code" parameter in the executeScript function, which allows you to pass a line with javascript code. If the content of the script is only one big function (i.e. content_script_function) that lives in background.js
in background.js:
function content_script_function(relevant_background_script_info) { // this function will be serialized as a string using .toString() // and will be called in the context of the content script page // do your content script stuff here... } function execute_script_in_content_page(info) { chrome.tabs.executeScript(tabid, {code: "(" + content_script_function.toString() + ")(" + JSON.stringify(info) + ");"}); } chrome.tabs.onUpdated.addListener( execute_script_in_content_page.bind( { reason: 'onUpdated', otherinfo: chrome.app.getDetails() }); chrome.runtime.onInstalled.addListener( execute_script_in_content_page.bind( { reason: 'onInstalled', otherinfo: chrome.app.getDetails() }); )
Where actual_background_script_info contains information about the original page, that is, what version it has, whether there was an update event and why this function is called. The script content page still saves all the relevant state. Thus, you have full control over how to handle the "update" event.
source share