Shun Koper
Shun Koper

Reputation: 11

How to run extension code only once upon installation

When my extension runs for the first time, I want to register the installation with my server. However, I only want the registration to occur the first time the extension is run after installation. How can I achieve this in by background scope? Currently, my code looks like this:

appAPI.ready(function($) {
    appAPI.request.get({
        url: 'http://myserver.com?action=install&id='+appAPI.appInfo.id,
        onSuccess: function(res) {
            console.log('Registration complete');
        }
    });
});

The problem with the code above is that the registrations happens every time I open by browser. Please help!

Upvotes: 1

Views: 41

Answers (1)

Shlomo
Shlomo

Reputation: 3753

Whilst there isn't an onInstall type of event, you can achieve the same result by keeping the installation status in the local database, as follows:

appAPI.ready(function($) {
    if (!appAPI.db.get('FirstRunDone')) {
        appAPI.request.get({
            url: 'http://myserver.com?action=install&id='+appAPI.appInfo.id,
            onSuccess: function(res) {
                appAPI.db.set('FirstRunDone', true);
                console.log('Registration complete');
            }
        });
    }
});

[Disclosure: I am a Crossrider employee]

Upvotes: 1

Related Questions