简体   繁体   中英

chrome.runtime.onInstalled does not fire first?

I'm a beginner. I am writing a countdown timer chrome extension. I want to run the countdown timer when the browser starts. From reading the documentation, I know that the settings can be stored in chrome.storage.sync. However, the chrome.storage.sync is asynchronous. So the first time the app is installed, the function chrome.storage.sync.get can occur before chrome.storage.sync.set in the event chrome.runtime.onInstalled.addListener, that results in undefined. So how can I ensure that the chrome.storage.sync.set in chrome.runtime.onInstalled.addListener is fired first in the first installation?

background.js

function initialize_timer() {
    window.curr_mode = WORKING_MODE;
    window.timer_state = PAUSE_STATE;
    chrome.storage.sync.get('working_time', function(result) {
        console.log("Get " + 'working_time' + " : " + result.working_time);
        window.waiting_time = result.working_time;
        window.remaining_time = result.working_time;
    });
};

chrome.runtime.onInstalled.addListener(function() {
    chrome.storage.sync.set({'working_time': 20 * 60 * 1000}, function() {
        console.log("Set working time: " + 20 * 60 * 1000);
    });
    chrome.storage.sync.set({'rest_time': 20 * 1000}, function() {
        console.log("set rest time: " + 20 * 1000);
    });
    console.log("WHAT'S UP BRO!");
});
initialize_timer();

However I got this result when I load unpacked:

Inspect of background.js:

https://i.imgur.com/nKz9k3d.png

In this image, because of the asynchronous characteristic, the chrome.runtime.sync.get in function initialize_timer happens before chrome.runtime.sync.set in chrome.runtime.onInstalled.addListerner. How to make the chrome.runtime.sync.set happens first?

Based on @wOxxOm suggestion, I have a fix to this problem. Store a default value first and check for undefined value when getting value from function chrome.storage.sync.get.

However, this is more like a hacking way. I am looking forward to a better solution.

background.js

const DEFAULT_WORKING_TIME = 20 * 60 * 1000;
const DEFAULT_RESTING_TIME = 20 * 1000;
var remaining_time = DEFAULT_WORKING_TIME;
var waiting_time = DEFAULT_WORKING_TIME;

function initialize_timer() {
    chrome.storage.local.get(['working_time'], function(result) {
        if (result.working_time === undefined) {
            return;
        }
        window.waiting_time = result.working_time;
        window.remaining_time = result.working_time;
    });
    window.curr_mode = WORKING_MODE;
    window.timer_state = PAUSE_STATE;
};


chrome.runtime.onInstalled.addListener(function() {
    chrome.storage.local.set({working_time: 20 * 60 * 1000}, function() {
        console.log("Set working time: " + 20 * 60 * 1000);
    });
    chrome.storage.local.set({rest_time: 20 * 1000}, function() {
        console.log("set rest time: " + 20 * 1000);
    });
});

initialize_timer();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM