问题
Hi I need to know if there is any way to perform auto refreshment of extension after installation.Currently I am using alarms to refresh the extension.But I need to perform automatic refreshment of extension after installation.Is there any way to perform it?Please help me. Below is my code by using alarms. here is my background.js
var oldChromeVersion = !chrome.runtime;
function getGmailUrl() {
return "http://calpinemate.com/";
}
function isGmailUrl(url) {
return url.indexOf(getGmailUrl()) == 0;
}
if (chrome.runtime && chrome.runtime.onStartup) {
chrome.runtime.onStartup.addListener(function() {
updateIcon();
});
} else {
chrome.windows.onCreated.addListener(function() {
updateIcon();
});
}
function onInit() {
updateIcon();
if (!oldChromeVersion) {
chrome.alarms.create('watchdog', {periodInMinutes:5});
}
}
function onAlarm(alarm) {
console.log('Got alarm', alarm);
if (alarm && alarm.name == 'watchdog') {
onWatchdog();
}
else {
updateIcon();
}
}
function onWatchdog() {
chrome.alarms.get('refresh', function(alarm) {
if (alarm) {
console.log('Refresh alarm exists.');
}
else {
updateIcon();
}
});
}
if (oldChromeVersion) {
updateIcon();
onInit();
}
else {
chrome.runtime.onInstalled.addListener(onInit);
chrome.alarms.onAlarm.addListener(onAlarm);
}
function updateIcon(){
.....//certain functions are performed
}
回答1:
In order to have your operations run when the extension is installed (which is something totally different from being disabled and re-enabled):
Replace that line:
chrome.alarms.create('watchdog', {periodInMinutes:5});
With this line:
chrome.alarms.create('watchdog', {
periodInMinutes: 5,
delayInMinutes: 0
});
If you want your operations to be run when the extension is enabled:
On a new line at the very, very end of your file add this:
onInit();
来源:https://stackoverflow.com/questions/20369102/how-to-perform-automatic-refresh-of-extension-after-installation-in-chrome-exten