我有一個關於鉻擴展安裝/更新事件的問題。如果我在後臺腳本的頂級代碼中添加onInstalled事件偵聽器,是否有一個時間框架,其中我的事件偵聽器將捕獲該事件?鉻擴展onInstalled事件
我在問這個問題,因爲我的演示表明,如果我在掛接安裝的偵聽器之前執行了一些邏輯,它看起來像它永遠不會執行,就像事件同時發生一樣。
有人可以向我解釋這個事件是如何工作的,在後臺腳本中的其他邏輯的背景下,或者指向我的一些文檔,因爲我一直沒有找到有用的東西。
謝謝!
更新@Noam黑客:由於公司政策,我不能在這裏發表任何實際的代碼,但我有我的說明一些問題的僞代碼:
/**
* setup in which I miss onInstalled event
*/
function firstLogicThatRunsOnBackgroundLoad() {
// perform some logic
// perform some asynchronous operations via generators and promises
// which can take a while
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") {
// this logic never gets executed
} else if(details.reason == "update") {
// perform some logic
}
});
}
/**
* setup in which I catch onInstalled event
*/
function firstLogicThatRunsOnBackgroundLoad() {
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") {
// this logic executes
} else if(details.reason == "update") {
// perform some logic
}
});
// perform some logic
// perform some asynchronous operations via generators and promises
// which can take a while
}
你有沒有背景腳本的示例代碼? –
如果將主邏輯放入偵聽器函數中,該怎麼辦? 'chrome.runtime.onInstalled.addListener(函數(詳細){ //執行您想要先執行的邏輯... //安裝/更新邏輯... } –
@NoamHacker如果我把一些我想先在偵聽器函數內執行的邏輯,我的測試表明,如果在後臺有其他邏輯,則不能保證這個邏輯將被首先執行。 – slickman