Skip to content

Instantly share code, notes, and snippets.

@daniellizik
Last active September 9, 2015 14:33
Show Gist options
  • Save daniellizik/1c29dcf2152fbfac2a62 to your computer and use it in GitHub Desktop.
Save daniellizik/1c29dcf2152fbfac2a62 to your computer and use it in GitHub Desktop.
create chrome extension context menus via json file
{
"root": {
"config": {
"title": "Bookmarker",
"id": "bkm",
"contexts": [
"all"
]
},
"children": [
{
"manage": {
"config": {
"title": "Manage links",
"id": "read",
"contexts": [
"all"
]
}
}
},
{
"create": {
"config": {
"title": "Add current page",
"id": "create",
"contexts": [
"all"
]
}
}
},
{
"update": {
"config": {
"title": "Update current page",
"id": "update",
"contexts": [
"all"
]
}
}
},
{
"delete": {
"config": {
"title": "Remove current page",
"id": "delete",
"contexts": [
"all"
]
}
}
},
{
"all": {
"config": {
"title": "Add all open tabs",
"id": "all",
"contexts": [
"all"
]
}
}
}
]
}
}
//Take json file and create chrome extension context menus
(function() {
"use strict";
get(chrome.extension.getURL("./background/contextmenu.config.json"), receive);
function receive(res) {
var data = JSON.parse(res.response);
makemenus(data);
}
function makemenus(obj, parent) {
for (var p in obj) {
if (obj[p].config) {
if (parent) obj[p].config.parentId = parent;
chrome.contextMenus.create(obj[p].config);
}
if (obj[p].children) {
for (var i = 0; i < obj[p].children.length; i++) {
makemenus(obj[p].children[i], obj[p].config.id);
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment