How to get chrome.runtime.on Installed to run when using Require.JS in the Chrome extension

I am having problems getting the setup code that runs when my extension is installed. I am using chrome.runtime.on Installed as suggested on the Chrome developers page, but it does not work. The problem seems to be related to using Require.JS. This is my code:

requirejs.config({...});// i'll not reproduce the whole config here for brevity

// Define the Global Object that will hold the extension functionality
var EXT = EXT || {};

// Start the main app logic.

requirejs(['chrome.storage', 'chrome.settings', 'chrome.tabs'],
function (chromeStorage, chromeSettings, chromeTabs) {
    'use strict';

    var getLocalRepos;

    /**
     * Executes the callback with an array of repositories as the parameter.
     * @param {function} callback
     */
    getLocalRepos = function (callback) {
        'use strict';

        chromeStorage.get('localRepos', callback);
    };

    // Take care of the extension lifecycle.
    chrome.runtime.onInstalled.addListener(function (details) {
        "use strict";

        // Create dummy data to populate the local storage for testing purposes.
        var dummyData = [
            {
                name: 'repo1',
                description: 'description1',
                username: 'reydel',
                age: 'theAge'
            },
            {
                name: 'repo2',
                description: 'description2',
                username: 'reydel',
                age: 'theAge'
            }
        ];

        switch (details.reason) {
            case 'install':
                chromeStorage.set({ localRepos: dummyData }, function () {
                    // Do nothing
                });
                break;
        }
    });

    // Bind all functions to an object in the Global Space to make them accessible from the outside scripts
    // referencing the BackgroundPage object
    window.EXT.getLocalRepos = getLocalRepos;
});

I used the code in a listener callback in the console, and it works, simply because the event is not firing.

Any ideas on how to solve this? Has anyone done this before?

+4
source share
1 answer

chrome.runtime.onInstalled . , onInstalled , .

, . , require.js script. , - .

(AMD) Chrome, , ( r.js grunt-contrib-requirejs) almond.js script. require.js, almond.js .

, all.js, almond.js( script), mymain.js (main script):

({
    name: 'mymain',
    out: 'all.js',
    include: [
        'almond'
    ],

    skipModuleInsertion: true,
    wrap: {
        start: '(function(){',
        // true = load synchronously. This is a feature of almond.js
        end: 'require(["mymain"], null, null, true);})();'
    }
})

, n-invoked. , (, ), (function(){ })();.

( ) . https://github.com/jrburke/r.js/blob/master/build/example.build.js.


:

  • r.js: npm install -g requirejs
  • myfancyname.build.js
  • script mymain.js: define(function() { ... });.
    ( . require.js, , ).
  • r.js : r.js -o myfancyname.build.js
  • all.js, , . "background": { "scripts": ["all.js"] } Chrome.
+5

Source: https://habr.com/ru/post/1569583/


All Articles