javascript 如何将 chrome.alarms 用于 Google Chrome 扩展程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17727734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:23:01  来源:igfitidea点击:

How to use chrome.alarms for Google Chrome extension

javascriptjavascript-eventsgoogle-chrome-extension

提问by lomse

manifest.json

清单文件.json

{
    "manifest_version": 2,
    "name": "App name",
    "description": "Description goes here",
    "version": "1.0",    
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": [
        "tabs",
        "alarms"
    ],
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    }
}

I trying to create a function to make a popup "great" every minute like this:

我试图创建一个函数来让弹出窗口每分钟都“很棒”,如下所示:

chrome.alarms.onAlarm.addListener(function(){
        alert('great');
    });

Could someone please tell why is it not triggering that alert. I check the console, no error was displayed. Thanks.

有人可以告诉为什么它没有触发该警报。我检查了控制台,没有显示错误。谢谢。

回答by Salix alba

Here is the simplest working example I can think of, warning it is very annoying as when the alarm is on it alerts "Beep" every 12 seconds. It uses a popup browser action to switch the alarm on and off.

这是我能想到的最简单的工作示例,警告它非常烦人,因为当警报响起时,它每 12 秒就会发出“哔”声。它使用弹出式浏览器操作来打开和关闭警报。

manifest.json

清单文件.json

{
  "manifest_version": 2,

  "name": "Alarm test",
  "description": "This extension alarms.",
  "version": "1.0",

  "permissions": [
    "alarms"
  ],

  "background": {
    "scripts": ["eventPage.js"],
    "persistent": false
  },

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

popup.html

弹出窗口.html

<!doctype html>
<html>
<head>
<title>Alarms Popup</title>

<script src="popup.js"></script>
</head>
<body>
<a href="" id="alarmOn">ON</a>
<a href="" id="alarmOff">OFF</a>
</ul>
</body>
</html>

popup.js

弹出窗口.js

var alarmClock = {

        onHandler : function(e) {
            chrome.alarms.create("myAlarm", {delayInMinutes: 0.1, periodInMinutes: 0.2} );
                    window.close();
        },

        offHandler : function(e) {
            chrome.alarms.clear("myAlarm");
                    window.close();
        },

        setup: function() {
            var a = document.getElementById('alarmOn');
            a.addEventListener('click',  alarmClock.onHandler );
            var a = document.getElementById('alarmOff');
            a.addEventListener('click',  alarmClock.offHandler );
        }
};

document.addEventListener('DOMContentLoaded', function () {
    alarmClock.setup();
});

And the important bit in eventPage.js

以及 eventPage.js 中的重要部分

chrome.alarms.onAlarm.addListener(function(alarm) {
  alert("Beep");
});

回答by u1841048

You didn't create any alarm, so no onAlarmevent will be fired.

您没有创建任何警报,因此不会onAlarm触发任何事件。

Create an alarm with chrome.alarms.create. Note: you should do it in the chrome.runtime.onInstalledevent.

使用chrome.alarms.create创建闹钟。注意:您应该在chrome.runtime.onInstalled事件中执行此操作