开发者

Populate Chrome context menu from external source/file

开发者 https://www.devze.com 2023-03-16 00:16 出处:网络
I am currently writing a Chrome extension whose basic function is to serve as a quick selection tool. What this means is that I am working with a file classification interface implemented through an A

I am currently writing a Chrome extension whose basic function is to serve as a quick selection tool. What this means is that I am working with a file classification interface implemented through an ASP.NET web site and I have to select particular values (such as the date, description, tags, etc.) describing a particular file when I upload it. So, I have decided to use Chrome's developpement tools to build an easy to use context menu (you just have to right click on the page) that presents a list of pre-defined rules that, when clicked, fill out all the appropriate fields (text boxes, drop down list, etc) on the page automatically.

The thing is that there's a lot of these pre-defined rules and they are prone to change and most of them have children nodes (ex: rules 1 contain 1.1, 1.2, etc.), which kinda makes it difficult to code directly in script form. Therefore, my idea was to include the definitions in a separate file, like an XML document, and s开发者_如何学编程imply populate the context menus with the extracted data. Only I've tried many different methods and none of them worked. Isn't it possible to have this XML file in the extension folder and simply access it ? If so, what would the code calling it look like ? Can I even interact with this kind of local resource from within a Chrome extension ? Any input is welcome.

Example:

// This is the parent "general" section
var general = chrome.contextMenus.create({ "title": "General" });

 // Here, there would be a loop iterating through the "general" division of the XML,
 // creating the sub-menus (general_item1, general_item2, etc).


You can load it through XMLHttpRequest and parse it with jquery for example:

$.ajax({
    url: "include.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find("entry").each(function(){
            //loop through children
            $(this).children("subentry").each(function(){
                console.log($(this).attr("param"));
            });
        });
    }
});

But I would go with what Jim Schubert suggested in comments - just include js file that contains all those settings in some sort of object ready to use. There is no need in converting XML to javascript if you can just store everything as javascript object in the first place.

0

精彩评论

暂无评论...
验证码 换一张
取 消