diff --git a/extension/popup.js b/extension/popup.js
index b76a58d5..a74c9345 100644
--- a/extension/popup.js
+++ b/extension/popup.js
@@ -19,9 +19,11 @@ function addactionbtn(info) {
var buttons_el = document.getElementById("buttons");
var button_container_el = document.createElement("li");
+ button_container_el.classList.add("action");
button_container_el.id = info.id + "_container";
var button_el = document.createElement("button");
+ button_el.classList.add("action");
button_el.id = info.id;
button_el.innerText = info.name;
button_el.onclick = function() {
@@ -78,6 +80,14 @@ function set_option(name, value) {
});
}
+function get_menucommands(cb) {
+ chrome.runtime.sendMessage({
+ type: "get_menucommands",
+ }, function(response) {
+ cb(response.data);
+ });
+}
+
function update_logo(value) {
if (value) {
document.getElementById("enabled-state").classList.remove("disabled");
@@ -143,20 +153,16 @@ get_option("imu_enabled", function(value) {
update_logo(value);
}, true);
-var promises = [];
-
-promises.push(addactionbtn({
- id: "replaceimages",
- action: "replace_images",
- name: "Replace images"
-}));
+get_menucommands(function(menuitems) {
+ var promises = [];
-promises.push(addactionbtn({
- id: "highlightimgs",
- action: "highlight_images",
- name: "Highlight images",
- toggle_setting: "highlightimgs_enable",
- toggle_default: false
-}));
+ for (let item of menuitems) {
+ promises.push(addactionbtn({
+ id: item.id,
+ action: item.id,
+ name: item.name
+ }));
+ }
-Promise.all(promises).then(updateheight);
+ Promise.all(promises).then(updateheight);
+});
diff --git a/src/userscript.ts b/src/userscript.ts
index db704d95..e27eb9a2 100644
--- a/src/userscript.ts
+++ b/src/userscript.ts
@@ -293,7 +293,7 @@ var $$IMU_EXPORT$$;
current_version = extension_manifest.version;
- extension_send_message = function(message, respond) {
+ extension_send_message = function(message, respond?) {
message = deepcopy(message, {json:true});
if (!respond)
respond = nullfunc;
@@ -1969,25 +1969,41 @@ var $$IMU_EXPORT$$;
do_request_raw = GM.xmlHttpRequest;
}
+ if (is_extension && !is_extension_bg) {
+ // used for clearing menu items
+ extension_send_message({
+ type: "init"
+ });
+ }
+
var register_menucommand = common_functions["nullfunc"];
var unregister_menucommand = common_functions["nullfunc"];
var num_menucommands = 0;
+ var menucommands_map = {};
+
+ var transform_menucommand_func = function(func) {
+ if (typeof func === "string") {
+ var dest = func;
+ func = function() {
+ // this gets run for every frame the script is injected in
+ if (is_in_iframe)
+ return;
+
+ open_in_tab_imu({
+ url: dest
+ }, false);
+ };
+ }
+
+ return func;
+ };
if (is_userscript) {
if (typeof(GM_registerMenuCommand) !== "undefined") {
register_menucommand = function(name, func) {
num_menucommands++;
- if (typeof func === "string") {
- var dest = func;
- func = function() {
- // this gets run for every frame the script is injected in
- if (is_in_iframe)
- return;
-
- open_in_tab(dest);
- };
- }
+ func = transform_menucommand_func(func);
var caption = "[" + num_menucommands + "] " + name;
var id = GM_registerMenuCommand(caption, func);
@@ -2005,6 +2021,38 @@ var $$IMU_EXPORT$$;
return GM_unregisterMenuCommand(id);
};
}
+ } else if (is_extension && !is_extension_bg) {
+ register_menucommand = function(name:string, func):string {
+ let id = get_random_id();
+
+ func = transform_menucommand_func(func);
+
+ menucommands_map[id] = {
+ name,
+ func
+ };
+
+ extension_send_message({
+ type: "register_menucommand",
+ data: {
+ id: id,
+ name: name
+ }
+ });
+
+ return id;
+ };
+
+ unregister_menucommand = function(id:string) {
+ extension_send_message({
+ type: "unregister_menucommand",
+ data: {
+ id: id
+ }
+ });
+
+ delete menucommands_map[id];
+ };
}
var open_in_tab = common_functions["nullfunc"];
@@ -2035,7 +2083,7 @@ var $$IMU_EXPORT$$;
type: "newtab",
data: {
imu: imu,
- background: bg
+ background: bg || false
}
}, cb);
} else if (is_userscript && open_in_tab) {
@@ -2046,6 +2094,21 @@ var $$IMU_EXPORT$$;
}
};
+ if ((is_userscript && open_in_tab !== common_functions["nullfunc"]) || is_extension) {
+ if (is_userscript)
+ register_menucommand("Options", get_options_page());
+
+ register_menucommand("Request support for this page", function() {
+ if (is_in_iframe)
+ return;
+
+ var location = window.location.href;
+ open_in_tab_imu({
+ url: "https://qsniyg.github.io/maxurl/#imu-request-site&url=" + encodeURIComponent(location)
+ }, false);
+ });
+ }
+
var check_tracking_blocked = function(result) {
// FireMonkey returns null for result if blocked
// GreaseMonkey returns null for status if blocked
@@ -126052,7 +126115,7 @@ var $$IMU_EXPORT$$;
return base_options;
};
- var replace_images_full = function(options) {
+ var replace_images_full = function(options?) {
var base_options = get_replace_images_options(options);
return replace_images(base_options);
};
@@ -126837,7 +126900,7 @@ var $$IMU_EXPORT$$;
};
var auto_highlighted_imgs = [];
- var highlight_images = function(options) {
+ var highlight_images = function(options?) {
if (currenttab_is_image() && settings.mouseover_exclude_imagetab)
return;
@@ -128365,6 +128428,8 @@ var $$IMU_EXPORT$$;
replace_images_full();
} else if (message.data.action === "highlight_images") {
highlight_images();
+ } else if (message.data.action in menucommands_map) {
+ menucommands_map[message.data.action].func();
}
} else if (message.type === "remote" || message.type === "remote_reply") {
handle_remote_event(message);
diff --git a/userscript.user.js b/userscript.user.js
index 9bde191d..fb960aeb 100644
--- a/userscript.user.js
+++ b/userscript.user.js
@@ -1640,22 +1640,35 @@ var $$IMU_EXPORT$$;
} else if (typeof (GM) !== "undefined" && typeof (GM.xmlHttpRequest) !== "undefined") {
do_request_raw = GM.xmlHttpRequest;
}
+ if (is_extension && !is_extension_bg) {
+ // used for clearing menu items
+ extension_send_message({
+ type: "init"
+ });
+ }
var register_menucommand = common_functions["nullfunc"];
var unregister_menucommand = common_functions["nullfunc"];
var num_menucommands = 0;
+ var menucommands_map = {};
+ var transform_menucommand_func = function(func) {
+ if (typeof func === "string") {
+ var dest = func;
+ func = function() {
+ // this gets run for every frame the script is injected in
+ if (is_in_iframe)
+ return;
+ open_in_tab_imu({
+ url: dest
+ }, false);
+ };
+ }
+ return func;
+ };
if (is_userscript) {
if (typeof (GM_registerMenuCommand) !== "undefined") {
register_menucommand = function(name, func) {
num_menucommands++;
- if (typeof func === "string") {
- var dest = func;
- func = function() {
- // this gets run for every frame the script is injected in
- if (is_in_iframe)
- return;
- open_in_tab(dest);
- };
- }
+ func = transform_menucommand_func(func);
var caption = "[" + num_menucommands + "] " + name;
var id = GM_registerMenuCommand(caption, func);
if (id === void 0 || id === null)
@@ -1669,6 +1682,32 @@ var $$IMU_EXPORT$$;
return GM_unregisterMenuCommand(id);
};
}
+ } else if (is_extension && !is_extension_bg) {
+ register_menucommand = function(name, func) {
+ var id = get_random_id();
+ func = transform_menucommand_func(func);
+ menucommands_map[id] = {
+ name: name,
+ func: func
+ };
+ extension_send_message({
+ type: "register_menucommand",
+ data: {
+ id: id,
+ name: name
+ }
+ });
+ return id;
+ };
+ unregister_menucommand = function(id) {
+ extension_send_message({
+ type: "unregister_menucommand",
+ data: {
+ id: id
+ }
+ });
+ delete menucommands_map[id];
+ };
}
var open_in_tab = common_functions["nullfunc"];
// todo: move below settings for options page
@@ -1694,7 +1733,7 @@ var $$IMU_EXPORT$$;
type: "newtab",
data: {
imu: imu,
- background: bg
+ background: bg || false
}
}, cb);
} else if (is_userscript && open_in_tab) {
@@ -1704,6 +1743,18 @@ var $$IMU_EXPORT$$;
}
}
};
+ if ((is_userscript && open_in_tab !== common_functions["nullfunc"]) || is_extension) {
+ if (is_userscript)
+ register_menucommand("Options", get_options_page());
+ register_menucommand("Request support for this page", function() {
+ if (is_in_iframe)
+ return;
+ var location = window.location.href;
+ open_in_tab_imu({
+ url: "https://qsniyg.github.io/maxurl/#imu-request-site&url=" + encodeURIComponent(location)
+ }, false);
+ });
+ }
var check_tracking_blocked = function(result) {
// FireMonkey returns null for result if blocked
// GreaseMonkey returns null for status if blocked
@@ -113661,6 +113712,8 @@ var $$IMU_EXPORT$$;
replace_images_full();
} else if (message.data.action === "highlight_images") {
highlight_images();
+ } else if (message.data.action in menucommands_map) {
+ menucommands_map[message.data.action].func();
}
} else if (message.type === "remote" || message.type === "remote_reply") {
handle_remote_event(message);