How to restart a chrome extension automatically?
Asked Answered
S

1

5

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,

Stamin answered 27/10, 2012 at 21:30 Comment(4)
Since this question ranks pretty highly in search results, I'll also mention chrome.extension.getBackgroundPage().window.location.reload();. Cheers.Auctorial
^^^^ should be the accepted answerWakeless
chrome.runtime.reload(); also worksChopine
Addendum: the getBackgroundPage() method may be superior as it doesn't close any tabs the extension currently has open.Chopine
C
8

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.

Consumptive answered 5/2, 2013 at 9:22 Comment(1)
I fixed the leak the hard way, but this is a good trick to keep in mind. thanks!Stamin

© 2022 - 2024 — McMap. All rights reserved.