Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a way to allow a content script heavy extension to continue functioning after an upgrade, and to make it work immediately upon installation.</p> <h1>Install</h1> <p>The install method is to simply iterate through all tabs in all windows, and inject some scripts programmatically into tabs with matching URLs. </p> <p>Obviously, you have to do it in a <a href="https://developer.chrome.com/extensions/background_pages" rel="noreferrer">background page</a> or <a href="https://developer.chrome.com/extensions/event_pages" rel="noreferrer">event page</a> script declared in manifest.json:</p> <pre><code>"background": { "scripts": ["background.js"] }, </code></pre> <p>background.js:</p> <pre><code>// Add a `manifest` property to the `chrome` object. chrome.manifest = chrome.app.getDetails(); var injectIntoTab = function (tab) { // You could iterate through the content scripts here var scripts = chrome.manifest.content_scripts[0].js; var i = 0, s = scripts.length; for( ; i &lt; s; i++ ) { chrome.tabs.executeScript(tab.id, { file: scripts[i] }); } } // Get all windows chrome.windows.getAll({ populate: true }, function (windows) { var i = 0, w = windows.length, currentWindow; for( ; i &lt; w; i++ ) { currentWindow = windows[i]; var j = 0, t = currentWindow.tabs.length, currentTab; for( ; j &lt; t; j++ ) { currentTab = currentWindow.tabs[j]; // Skip chrome:// and https:// pages if( ! currentTab.url.match(/(chrome|https):\/\//gi) ) { injectIntoTab(currentTab); } } } }); </code></pre> <h1>Upgrade</h1> <p>The upgrade method relies on the fact that the content scripts are left injected after an extension is disabled, uninstalled or upgraded.</p> <p>When the port connection is made, an onDisconnect handler is added. This waits a second after the disconnect event, then attempts to reconnect. If it fails, another onDisconnect is fired so the process happens again, until a connection is made. It's not perfect, but it works.</p> <p>The content script:</p> <pre><code>var port; // Attempt to reconnect var reconnectToExtension = function () { // Reset port port = null; // Attempt to reconnect after 1 second setTimeout(connectToExtension, 1000 * 1); }; // Attempt to connect var connectToExtension = function () { // Make the connection port = chrome.runtime.connect({name: "my-port"}); // When extension is upgraded or disabled and renabled, the content scripts // will still be injected, so we have to reconnect them. // We listen for an onDisconnect event, and then wait for a second before // trying to connect again. Becuase chrome.runtime.connect fires an onDisconnect // event if it does not connect, an unsuccessful connection should trigger // another attempt, 1 second later. port.onDisconnect.addListener(reconnectToExtension); }; // Connect for the first time connectToExtension(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload