简体   繁体   中英

Chrome Extension ContextMenu: Get title %s as a string

I'm writing my first Chrome extension and have run into an issue. My desired functionality is that when a user right-clicks a selected word, I want the context menu that spawns to display that word as a menu item AND log the text as a string for later use. By using %s as the value for the title property, when the menu is created it will display that text. I can't seem to figure out how to capture that in a variable as well. Code below:

chrome.storage.onChanged.addListener(function() {
    chrome.storage.local.set({'wasFired': false});
    console.log("event firing");

    // begin creation of menu
    var master = chrome.contextMenus.create({
        "id": "SynoStart",
        "title": '%s',
        "contexts":["selection"]
    });
});

If I run a function inside contextmenu.create and log the object to the console I can dig down and parse out the title. Unfortunately it returns as %s instead of the user's selected text (code below).

chrome.storage.onChanged.addListener(function() {
    chrome.storage.local.set({'wasFired': false});
    console.log("event firing");

    //begin creation of menu
    var master = chrome.contextMenus.create({
    "id": "SynoStart",
    "title": '%s',
    "contexts":["selection"]
    },
    function(){
        console.log(this.args[0].title); //returns "%s"
    });
});

If I let the callback finish and run a simply console.log(master); it only returns the ID as a string (in this case "SynoStart"). Running console.log(master.title); returns undefined. Examples below:

    chrome.storage.onChanged.addListener(function() {
        chrome.storage.local.set({'wasFired': false});
        console.log("event firing");

        //begin creation of menu
        var master = chrome.contextMenus.create({
            "id": "SynoStart",
            "title": '%s',
            "contexts":["selection"]
        });
        console.log(master); //returns "SynoStart"
        console.log(master.title); //returns undefined
    });

Is there any way to dynamically extract the title property (as a string) of this menu object at its creation?

This is my first question on SO, so please feel free to let me know how I should improve it. Thank you very much!

Chrome documentation for chrome.contextMenus.create indicates that you can specify a callback function that receives two params, the first of type OnClickData and the second of type Tab. Some current bug in their documentation is causing the description of OnClickData to be inaccessible. Fortunately, you can still read it in Opera's documentation . There you'll see that there is a selectionText field with description: "The text for the context selection, if any."

Note that the callback function for click events on your menu is specified as an onclick field of the create details. The callback function specified as the second parameter of chrome.contextMenus.create is just to report you if the menu item was created.

So, your code can be:

var master = chrome.contextMenus.create({
    "id": "SynoStart",
    "title": '%s',
    "contexts":["selection"]
    "onclick": function(onClickData, tab){
        console.log(onClickData.selectionText);
    });
}

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