0

My extension should use the user's options to build submenus under the main extension context menu entry. The options are stored in a table, where each line is defining a submenu. The whole table is stored as a json string in chrome.local.storage with the key jsondata.

The manifest is:

   "background": {
  "persistent": true,
  "scripts": [ "js/storage.js", "js/backgroundlib.js", "js/background.js" ]
},
...
"permissions": [ "storage", "contextMenus", "http://*/*", "https://*/*",   "tabs", "clipboardRead", "clipboardWrite" ],
...

In the background script, I'm trying to get the data using:

window.addEventListener('load', function () {
var key = 'jsondata';

 storage.area.get(key, function (items){ 
     console.log(items[key]);
     build_submenu(items[key]);}); 
 }); 

function build_submenu(json) {
     console.log("build_submenu: " + json);
 }

and build_submenu should then call multiple chrome.contextMenus.create({... }) to add the submenus. For now, I can't get build_submenu being called. Am I trying to do something that is not possible or am I just missing something obvious?

Thanks, F.

AL.
  • 36,815
  • 10
  • 142
  • 281
fraz
  • 29
  • 2

2 Answers2

0

Replace storage.area.get with chrome.storage.local.get.

Another suggestion would be removing the outer window.onload listener, since you are using background scripts and window.onload makes no sense.

Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
0

OK, I finally got this, that works: manifest.json

   "background": {
  "persistent": false,
  "scripts": [ "js/storage.js", "js/backgroundlib.js", "js/background.js" ]
  },

in background.js, The context menu is build in the callback function when reading from storage. This reading is called when onInstalled is fired. I use a global var that is saved onSuspend et read again onStartup. and that associate the submenu id and the corresponding row from the user's option. The onClick listener test if the global variable is defined. If not it is read again from storage.

    var regex = new Object();

chrome.runtime.onInstalled.addListener( function () {    
     console.log("onInstalled called");
     var key = 'jsondata';
     storage.area.get(key, function (items){ get_jsondata(items[key]);}); 
      function get_jsondata(value){
      var data = JSON.parse(value);

      var fcb =[ {fcb_context: "fcb_copy", title:"Copy filtered", context: ["selection", "link"]}, {fcb_context:"fcb_paste", context:["editable"], title:"Paste filtered"}];
     for (var i=0; i<fcb.length; i++) {
         var menu = fcb[i];
        chrome.contextMenus.create({
    //title: "Look up: %s",
        title: menu.title,
        id: menu.fcb_context,
        contexts: menu.context,

        });
        var last = data.length;
    //var sel = info.selectionText;
        for (var j=0; j<last; j++){
            chrome.contextMenus.create({ 
            title: data[j].name, 
            contexts: menu.context, 
            id: menu.fcb_context + "_" + j,
            parentId: menu.fcb_context,
                    //onclick:  function(info, tab){ run_cmd( data[j].regex, info, menu.fcb_context ); }
            });
            regex[ menu.fcb_context + "_" + j] = data[j];
            //console.log(regex[menu.fcb_context + "_" + j]);
        }// for j

    } // for i
    }//get_jsondata

}); //add listener


chrome.contextMenus.onClicked.addListener(function(info, tabs){ 
                var id = info.menuItemId;
                if (typeof regex === "undefined" ){
                    storage.area.get("regex", function(items){
                        regex = JSON.parse(items["regex"]);
                        console.log("get " + items["regex"] + " from storage");
                            run_cmd( regex, info );  
                    });
                 } else { 
                     console.log("regex was defined... " + JSON.stringify(regex));
                         run_cmd( regex, info );  
                 }
   });

chrome.runtime.onSuspend.addListener(function() {
  // Do some simple clean-up tasks.
  console.log("onSuspend called saving " + JSON.stringify(regex));
  storage.area.set({ "regex" : JSON.stringify(regex)}, function(){console.log("regex saved");} );
});

chrome.runtime.onStartup.addListener(function() {
     console.log("onStartup called");
     storage.area.get("regex", function(items){
        regex = JSON.parse(items["regex"]);
        console.log("get " + items["regex"] + " from storage");
     });
});



function getSelectedText(info){
    var sel = info.selectionText;
    chrome.tabs.executeScript(null, {file:"js/script.js"});
}

function pasteFilteredText(info){
    chrome.tabs.executeScript(null, {file:"js/script.js"});
}




function run_cmd(regex, info){
    var id = info.menuItemId;
     var data =  regex[id];

        var sel = info.selectionText;
    var fcb_context = info.parentMenuItemId;
    //console.log("run_cmd regex " + data.regex + " sel " + (sel ? sel : ""));
    alert("run_cmd regex " + data.regex + " sel " + (sel ? sel : "") + " fcb_context: " + fcb_context);

}

Thanks for pointing me what is superfluous or missing.

fraz
  • 29
  • 2