Skip to content

Commit

Permalink
Initialize popup buttons from userscript instead of of popup page
Browse files Browse the repository at this point in the history
  • Loading branch information
qsniyg committed Dec 8, 2023
1 parent 77627e5 commit e8b21fc
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 42 deletions.
69 changes: 67 additions & 2 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var override_headers = {};
var override_download = {};
var reqid_to_redid = {};
var notifications = {};
var menucommands = {};

var ready_functions = [];
var on_ready = function(f) {
Expand Down Expand Up @@ -1169,8 +1170,7 @@ var extension_message_handler = (message, sender, respond) => {
}
});
} else if (message.type === "popupaction") {
if (message.data.action === "replace_images" ||
message.data.action === "highlight_images") {
if (message.data.action) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var currentTab = tabs[0];
chrome.tabs.sendMessage(currentTab.id, message);
Expand Down Expand Up @@ -1385,6 +1385,57 @@ var extension_message_handler = (message, sender, respond) => {
});

return true;
} else if (message.type === "register_menucommand") {
debug("register_menucommand", message);

if (sender.frameId)
return;

var tabid = sender.tab.id;
if (!(tabid in menucommands))
menucommands[tabid] = [];

menucommands[tabid].push(message.data);
} else if (message.type === "unregister_menucommand") {
debug("unregister_menucommand", message);

if (sender.frameId)
return;

var tabid = sender.tab.id;
if (tabid in menucommands) {
for (let i = 0; i < menucommands.length; i++) {
if (menucommands[i].id === message.data.id) {
menucommands.splice(i, 1);
break;
}
}
}
} else if (message.type === "get_menucommands") {
debug("get_menucommands", message);

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var currentTab = tabs[0];
var tabid = currentTab.id;

var commands = [];
if (tabid in menucommands)
commands = menucommands[tabid];

respond({
type: "get_menucommands",
data: commands
});
});

return true;
} else if (message.type === "init") {
debug("init", message);

if (sender.frameId)
return;

tabclear(sender.tab.id);
}
};

Expand Down Expand Up @@ -1585,6 +1636,15 @@ chrome.storage.onChanged.addListener(function(changes, namespace) {
}
});

function tabclear(tabid) {
if (nir_debug)
console.log("Clearing tab: ", tabid);

if (tabid in menucommands) {
delete menucommands[tabid];
}
}

function tabremoved(tabid) {
if (nir_debug)
console.log("Removed tab: ", tabid);
Expand Down Expand Up @@ -1613,6 +1673,8 @@ function tabremoved(tabid) {
if (tabid in redirects) {
delete redirects[tabid];
}

tabclear(tabid);
}

function tabready(tabid) {
Expand Down Expand Up @@ -1645,6 +1707,9 @@ chrome.tabs.onReplaced.addListener(function (added, removed) {
});

chrome.tabs.onUpdated.addListener(function(tabid, info, tab) {
if (nir_debug)
console.log("tabs.onUpdated", info, tab);

if (info.status === "loading") {
if (nir_debug)
console.log("tabs.onUpdated: loading");
Expand Down
9 changes: 9 additions & 0 deletions extension/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -805,11 +805,20 @@ input:checked + label, button, .button, .dark .button, .button:visited, .dark .b
margin: 0;
}

#popup #buttons li.action {
padding: .25em;
}

#popup #buttons .button, #popup #buttons button {
padding: 8px 12px;
line-height: 1;
}

#popup #buttons button.action {
padding: 6px 8px;
font-size: 95%;
}

#popup #footer {
margin-top: 8px;
margin-bottom: 12px;
Expand Down
2 changes: 1 addition & 1 deletion extension/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="initial-scale=1.0" />
<link rel="stylesheet" href="options.css" />
</head>
<body style="width:210px" id="popup">
<body style="width:250px" id="popup">
<div id="outer-container">
<div id="container" style="height:100%;text-align:center">
<div id="logo" title="Click to enable/disable">
Expand Down
36 changes: 21 additions & 15 deletions extension/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
});
93 changes: 79 additions & 14 deletions src/userscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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"];
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit e8b21fc

Please sign in to comment.