4

使用Google Chrome瀏覽器擴展程序警報時,如果警報設置並且Chrome在警報的時間到期後關閉並重新打開,則該警報將關閉。當Chrome瀏覽器在時間用完之後重新打開時,Chrome擴展程序警報會關閉?

我該如何解決這個問題?

這裏是一個小代碼示例來解釋我的意思。

/* 
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!'); 
} 

所以,如果你會在我的代碼示例頂部閱讀評論,您將看看會發生什麼。

我想要的是,如果瀏覽器關閉,警報就會被清除,因爲我希望DoSomething函數只在onStartup事件中執行,如果瀏覽器剛剛啓動,並讓警報執行DoSomething函數只有在瀏覽器啓動後,我的代碼纔會創建新的警報。

在瀏覽器關閉後,我不希望報警留在周圍,然後在瀏覽器重新打開時執行onAlarm。

這怎麼能實現呢?

回答

5

當瀏覽器關閉時,Chrome擴展無法可靠地運行某些代碼。

而不是在關機時清理,只要確保在啓動時不會運行舊報警。這可以通過生成唯一的(會話)標識符來實現。

如果您使用活動頁面,請將標識符存儲在chrome.storage.local(不要忘記在清單文件中設置storage權限)。否則,將其存儲在全局範圍內。

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

如果你使用活動頁面,但正常的背景網頁,只存儲全局變量(優點: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 (alarm.name == 'myAlarm') { 
     if (alarm.suffix === window.alarm_suffix) { 
      doSomething(); 
     } 
    } 
}); 

或者只是使用好舊的setTimeout來達到同樣的目標而沒有副作用。

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

data.alarm_suffix === alarm.suffix是不正確的,因爲報警被以比onStartup事件熄滅不同的時間創建的。(只有幾毫秒)。 – user1876122

+0

@ user1876122因此,前述評論「//綁定警報監聽器。注意:這必須發生在*唯一的ID被設置後。 –

相關問題