How to perform automatic refresh of extension after installation in chrome extension

主宰稳场 提交于 2019-12-13 06:11:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!