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,
On a Mac computer or Windows PC, type chrome://restart in the address bar of your Chrome browser, then hit the Enter or Return key on your keyboard to enter the command. The browser will automatically restart and the tabs you previously had open will reappear.
Apps and extensions work like regular programs for desktop computers, but they run entirely in the Chrome browser. You don't need to install software or restart your computer.
Most Chrome extensions start working automatically when you add them to your browser, but they can also be disabled after you install them. If you or another user has disabled an extension in your Chrome browser, it's easy to re-enable it.
Chrome does a good job of updating your extensions automatically, but it checks for updates on its own schedule. If you know that a new version of an extension is out but Chrome hasn't updated it, here's how to do it manually. Start Google Chrome.
You can have two extensions. And restart the target extension from the other. If that suits you read on.
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);
// 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_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_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
}
]
}
.
├── 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With