如何在Chrome扩展中安装后自动刷新扩展 [英] How to perform automatic refresh of extension after installation in chrome extension

查看:379
本文介绍了如何在Chrome扩展中安装后自动刷新扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要知道是否有任何方法可以在安装后执行扩展的自动刷新。目前我正在使用警报来刷新扩展。但是我需要在安装后执行扩展的自动刷新。是否有任何方法可以执行它?请帮助我。以下是我使用警报的代码。
这里是我的background.js

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
   }


推荐答案

为了让您的操作在安装扩展时运行(这与完全不同被禁用并重新启用):

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();

这篇关于如何在Chrome扩展中安装后自动刷新扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆