How to restart a chrome extension automatically?

jason picture jason · Oct 27, 2012 · Viewed 7.6k times · Source

I have a chrome extension that I built that leaks memory. I am working on fixing the memory leak, but in the mean time a few friends are already using it.

As a temporary measure I want to include a patch which would restart the extension automatically, periodically.

How to do this?, namely restart a chrome extension from within the extension itself.

thanks,

Answer

simo picture simo · Feb 5, 2013

You can have two extensions. And restart the target extension from the other. If that suits you read on.

content.js

window.addEventListener('load', function (e) {
    // create a button and add it to the page
    var btn = document.createElement('button');
    btn.innerHTML = 'Restart child extension';
    btn.addEventListener('click', function (e) {
        // on button click send message to the background script
        chrome.extension.sendMessage({restart: true}, function (res) {
            console.log(res);
        });
    }, false);

    var body = document.querySelector('body');
    body.appendChild(btn);
}, false);

background.js

// first get your target (child) extension by it's name
var child = null;
chrome.management.getAll(function (info) {
    for (var i=0; i < info.length; i++) {
        if (info[i].name == 'Test child extension') {
            child = info[i];
            break;
        }
    }
});

function disable (cb) {
    chrome.management.setEnabled(child.id, false, cb);
}
function enable (cb) {
    chrome.management.setEnabled(child.id, true, cb);
}
function afterEnable () {
    // notify the content script
    resRestart({restarted: true});
}

var resRestart = null;
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
    console.log(request);
    // if we receive request with restart variable, save a reference to the
    // sendResponse function and disable the child extension
    switch (true) {
        case request.restart: resRestart = sendResponse; disable(); break;
    }
    return true;
});
chrome.management.onDisabled.addListener(function (extension) {
    // this one is fired when extension is restarted
    // check if this is our child extension and re-enable it
    if (extension.name == 'Test child extension') {
        enable(afterEnable);
    }
});

manifest.json (parent)

{
    "manifest_version": 2,
    "name"            : "Test parent extension",
    "version"         : "1.0",
    "description"     : "Whatever",

    "background" : {
        "scripts": [
            "background.js"
        ]
    },

    "content_scripts": [
        {
            "matches": [
                "*://localhost/*"
            ],
            "js": [
                "content.js"
            ],
            "run_at": "document_end",
            "all_frames": true
        }
    ],

    "permissions": [
        "tabs",
        "management",
        "*://localhost/*"
    ]
}

manifest.json (child)

{
    "manifest_version": 2,
    "name"            : "Test child extension",
    "version"         : "1.0",
    "description"     : "Whatever",

    "content_scripts": [
        {
            "matches": [
                "*://localhost/*"
            ],
            "css": [
                "style.css"
            ],
            "run_at": "document_end",
            "all_frames": true
        }
    ]
}

directory structure

.
├── background.js
├── child
│   ├── manifest.json
│   └── style.css
├── content.js
└── manifest.json

Now open up about:extensions and http://localhost in split screen. Click on the button and see how the child extension is refreshed each time. Also you can check out the console. Even try to disable the child extension from within about:extensions - it's not possible as long as the parent extension is running.