2013-11-07 05:56:12 +00:00
|
|
|
define(function() {
|
|
|
|
|
|
|
|
var registry = {};
|
|
|
|
|
|
|
|
var onClick = function(info) {
|
|
|
|
var handler = registry[info.menuItemId];
|
|
|
|
if (handler) {
|
|
|
|
var params = handler.parse(info.linkUrl);
|
|
|
|
handler.callback(params);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var makeURL = function(filter, id) {
|
|
|
|
var url = "chrome-extension://" + chrome.runtime.id + "/" + filter;
|
|
|
|
if (typeof id != "undefined") {
|
|
|
|
url += "/" + id;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
|
|
|
var createRoute = function(route, handler) {
|
|
|
|
var parts = route.split("/");
|
2013-12-04 17:24:04 +00:00
|
|
|
var positionMap = {};
|
|
|
|
var position = 1;
|
|
|
|
parts = parts.map(function(s) {
|
2013-11-07 05:56:12 +00:00
|
|
|
if (s[0] == ":") {
|
|
|
|
//set the key to be used on parsing
|
2013-12-04 17:24:04 +00:00
|
|
|
positionMap[position++] = s.replace(/^:/, "");
|
2013-11-07 05:56:12 +00:00
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
return s.replace(/[\^()\[\]]/, function(match) { return "\\" + match });
|
|
|
|
});
|
|
|
|
var re = new RegExp(parts.map(function(s) { return s == "*" ? "([^\/]+)" : s }).join("\\/"));
|
|
|
|
var parser = function(url) {
|
|
|
|
var result = re.exec(url);
|
|
|
|
if (!result) return result;
|
|
|
|
var params = {};
|
2013-12-04 17:24:04 +00:00
|
|
|
for (var place in positionMap) {
|
|
|
|
var key = positionMap[place];
|
2013-11-07 05:56:12 +00:00
|
|
|
params[key] = result[place];
|
|
|
|
}
|
2013-11-08 01:32:38 +00:00
|
|
|
params.url = url;
|
2013-11-07 05:56:12 +00:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
parse: parser,
|
|
|
|
regex: re,
|
|
|
|
url: makeURL(parts.join("/")),
|
|
|
|
callback: handler
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.contextMenus.onClicked.addListener(onClick);
|
|
|
|
|
|
|
|
return {
|
|
|
|
register: function(label, id, route, handler) {
|
|
|
|
var compiled = createRoute(route, handler);
|
|
|
|
registry[id] = compiled;
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
title: label,
|
|
|
|
targetUrlPatterns: [ compiled.url ],
|
|
|
|
contexts: ["link"],
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
makeURL: makeURL
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|