当Chrome浏览器在时间用完后重新打开时,Chrome扩展程序警报会熄灭吗? [英] Chrome Extension Alarms go off when Chrome is reopened after time runs out?

查看:379
本文介绍了当Chrome浏览器在时间用完后重新打开时,Chrome扩展程序警报会熄灭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Google Chrome扩展程序警报时,如果警报设置并且Chrome在警报到期后关闭并重新打开,警报将会关闭。



我停止了这个吗?



这里有一个小代码示例来解释我的意思。

  / * 
如果我们执行浏览器操作来创建闹钟,那么
关闭浏览器,等待约2分钟让闹钟过期
,然后重新打开浏览器,闹钟将会关闭,DoSomething
函数会被onStartup事件调用两次,一次被onAlarm事件调用

* /
chrome.browserAction.onClicked.addListener(function(tab){
chrome.alarms.create('myAlarm',{
delayInMinutes:2.0
} );
});

chrome.alarms.onAlarm.addListener(function(alarm){
console.log('Fired alarm!');
if(alarm.name =='myAlarm' ){
createListener();
}
});

chrome.runtime.onStartup.addListener(function(){
console.log('Extension started up ...');
DoSomething();
});

函数DoSomething(){
alert('Function executed!');

$ / code>

因此,如果您将阅读代码示例顶部的注释,您将看看会发生什么。

我想要的是,如果浏览器关闭,报警将被清除,因为我希望DoSomething函数只能通过onStartup事件来执行如果浏览器刚刚启动,并且只有在浏览器启动后我的报警才会执行DoSomething功能,并且我的代码会创建一个新的报警。



我不希望发生报警浏览器关闭后待在身边,然后在浏览器重新打开时执行onAlarm。



如何实现这个功能?

解决方案

Chrome浏览器扩展程序无法在浏览器关闭时可靠地运行一些代码。



不是在关闭时清理,只要确保旧的警报不在启动时运行。这可以通过生成一个唯一的(对会话)标识符来实现。



如果您使用的是事件页面,请将标识符存储在 chrome.storage.local (不要忘记将清单文件中的存储权限)。

  // ID生成:
chrome.runtime.onStartup.addListener( function(){
console.log('Extension started up ...');
chrome.storage.local.set({
alarm_suffix:Date.now()
},function(){
//初始化您的扩展,例如创建浏览器动作处理程序
//并绑定警报侦听器
doSomething();
});
});

//闹钟设置器:
chrome.storage.local.get('alarm_suffix',function(items){
chrome.alarms.create('myAlarm'+ items。 alarm_suffix,{
delayInMinutes:2.0
});
});

//绑定警报监听器。注意:这必须在设置唯一ID之后发生
chrome.alarms.onAlarm.addListener(function(alarm){
var parsedName = alarm.name.match(/ ^([\如果(parsedName){
alarm.name = parsedName [0];
alarm.suffix = + parsedName [ 1];
}
if(alarm.name =='myAlarm'){
chrome.storage.local.get('alarm_suffix',function(data){
if (data.alarm_suffix === alarm.suffix){
doSomething();
}
});
}
});

如果您 不是使用活动页面,而是普通的背景页面,只需在全局存储变量(优点:id读取/写入变为同步,这需要更少的代码):

  chrome.runtime.onStartup .addListener(function(){
window.alarm_suffix = Date.now();
});
chrome.alarms.create('myAlarm'+ window.alarm_suffix,{
delayInMinutes:2.0
});
chrome.alarms.onAlarm.addListener(function(alarm){
var parsedName = alarm.name.match(/ ^([\S\s] *?)(\d +)$ /);
if(parsedName){
alarm.name = parsedName [0];
alarm.suffix = + parsedName [1];
}
if( ();
if(alarm.suffix === window.alarm_suffix){
doSomething();
}
}
} );

或者使用旧的 setTimeout

  setTimeout(function(){
doSomething();
},2 * 60 * 1000); // 2分钟


When using Google Chrome extension alarms, the alarm will go off if it was set and Chrome is closed and reopened after the time expires for the alarm.

How can I stop this?

Here is a small code sample to explain what I mean.

/* 
If we perform Browser Action to create alarm, then 
close the browser, wait about 2 minutes for the alarm to expire
and then reopen the browser, the alarm will go off and the DoSomething 
function will get called twice, once by the onStartup event and once
by the onAlarm event.
*/
chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.alarms.create('myAlarm', {
        delayInMinutes : 2.0
    });
});

chrome.alarms.onAlarm.addListener(function (alarm) {
    console.log('Fired alarm!');
    if (alarm.name == 'myAlarm') {
        createListener();
    }
});

chrome.runtime.onStartup.addListener(function () {
    console.log('Extension started up...');
    DoSomething();
});

function DoSomething() {
    alert('Function executed!');
}

So if you will read the comment at the top of my code sample you will see what happens.

What I want though, is for the alarm to get cleared if the browser is closed as I want the DoSomething function to get executed only by the onStartup event if the browser is just started, and let the alarm execute the DoSomething function only after the browser is started and my code creates a new alarm.

I never want an alarm to stay around after the browser is closed and then execute onAlarm when the browser is reopened.

How can achieve this?

解决方案

It's not possible for a Chrome extension to reliably run some code when the browser closes.

Instead of cleaning up on shutdown, just make sure that old alarms are not run on startup. This can be achieved by generating an unique (to the session) identifier.

If you're using event pages, store the identifier in chrome.storage.local (don't forget to set the storage permission in the manifest file). Otherwise, store it in the global scope.

// ID generation:
chrome.runtime.onStartup.addListener(function () {
    console.log('Extension started up...');
    chrome.storage.local.set({
        alarm_suffix: Date.now()
    }, function() {
        // Initialize your extension, e.g. create browser action handler
        // and bind alarm listener
        doSomething();
    });
});

// Alarm setter:
chrome.storage.local.get('alarm_suffix', function(items) {
    chrome.alarms.create('myAlarm' + items.alarm_suffix, {
        delayInMinutes : 2.0
    });
});

// Bind alarm listener. Note: this must happen *after* the unique ID has been set
chrome.alarms.onAlarm.addListener(function(alarm) {
    var parsedName = alarm.name.match(/^([\S\s]*?)(\d+)$/);
    if (parsedName) {
        alarm.name = parsedName[0];
        alarm.suffix = +parsedName[1];
    }
    if (alarm.name == 'myAlarm') {
        chrome.storage.local.get('alarm_suffix', function(data) {
            if (data.alarm_suffix === alarm.suffix) {
                doSomething();
            }
        });
    }
});

If you're not using event pages, but normal background pages, just store the variable globally (advantage: id reading/writing becomes synchronous, which requires less code):

chrome.runtime.onStartup.addListener(function () {
    window.alarm_suffix = Date.now();
});
chrome.alarms.create('myAlarm' + window.alarm_suffix, {
    delayInMinutes : 2.0
});
chrome.alarms.onAlarm.addListener(function(alarm) {
    var parsedName = alarm.name.match(/^([\S\s]*?)(\d+)$/);
    if (parsedName) {
        alarm.name = parsedName[0];
        alarm.suffix = +parsedName[1];
    }
    if (alarm.name == 'myAlarm') {
        if (alarm.suffix === window.alarm_suffix) {
            doSomething();
        }
    }
});

Or just use the good old setTimeout to achieve the same goal without side effects.

setTimeout(function() {
    doSomething();
}, 2*60*1000); // 2 minutes

这篇关于当Chrome浏览器在时间用完后重新打开时,Chrome扩展程序警报会熄灭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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