Context-menu items to run a page/link’s command

Now you can right-click on a page and see a
“Run shell command” item or a
“Run command on link” item.
This commit is contained in:
Jaidyn Ann 2024-03-02 11:39:44 -06:00
parent 4d74ed0a10
commit 40b5ae16cb
2 changed files with 63 additions and 28 deletions

View File

@ -56,8 +56,31 @@ function compareRegexComplexity(a, b) {
} }
// Display the “Run shell command” context-menu item.
function showPageContextMenuItem() {
browser.menus.update("run-page-command", { "visible": true });
}
// Display the “Run command on link” context-menu item.
function showLinkContextMenuItem() {
browser.menus.update("run-page-commands", { "visible": true });
}
// Hide the “Run shell command context-menu item.
function hidePageContextMenuItem() {
browser.menus.update("run-page-command", { "visible": false });
}
// Hide the “Run command on link” context-menu item.
function hideLinkContextMenuItem() {
browser.menus.update("run-page-commands", { "visible": false });
}
// Add a context-menu item for running the current pages associated command. // Add a context-menu item for running the current pages associated command.
function createContextMenuItem() {
browser.menus.create( browser.menus.create(
{ {
id: "run-page-command", id: "run-page-command",
@ -65,13 +88,16 @@ function createContextMenuItem() {
contexts: ["page"] contexts: ["page"]
} }
); );
}
// Simply remove the context-menu item. // Add a context-menu item for running the command associated with a link.
function removeContextMenuItem() { browser.menus.create(
browser.menus.remove("run-page-command"); {
id: "run-link-command",
title: "Run command on link",
contexts: ["link"]
} }
);
// When the address-bar button is clicked, run the according command (if any). // When the address-bar button is clicked, run the according command (if any).
@ -80,19 +106,27 @@ browser.pageAction.onClicked.addListener((tab) => {
}); });
browser.menus.onShown.addListener(info => {
if (info.contexts.includes("link") && getUrlCommand(info.linkUrl))
showLinkContextMenuItem();
else if (info.contexts.includes("page") && getUrlCommand(info.pageUrl))
showPageContextMenuItem();
else {
hidePageContextMenuItem();
hideLinkContextMenuItem();
}
browser.menus.refresh();
});
// When a tabs URL has been changed, enable/disable the address-bar button // When a tabs URL has been changed, enable/disable the address-bar button
// based on whether or not there is an according command. // based on whether or not there is an according command.
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
let command = getUrlCommand(tab.url); let command = getUrlCommand(tab.url);
if (command) { if (command)
browser.pageAction.show(tabId); browser.pageAction.show(tabId);
if (tab.active) else
createContextMenuItem();
} else {
browser.pageAction.hide(tabId); browser.pageAction.hide(tabId);
if (tab.active)
removeContextMenuItem();
}
}); });
@ -100,18 +134,19 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// on whether or not there is an according command for it. // on whether or not there is an according command for it.
browser.tabs.onActivated.addListener((activeInfo) => { browser.tabs.onActivated.addListener((activeInfo) => {
browser.tabs.get(activeInfo.tabId).then((tab) => { browser.tabs.get(activeInfo.tabId).then((tab) => {
if (getUrlCommand(tab.url)) { if (getUrlCommand(tab.url))
browser.pageAction.show(tab.id); browser.pageAction.show(tab.id);
createContextMenuItem(); else
} else {
browser.pageAction.hide(tab.id); browser.pageAction.hide(tab.id);
removeContextMenuItem();
}
}); });
}); });
// When a context-menu item is selected, lets execute its will!
browser.menus.onClicked.addListener((info, tab) => { browser.menus.onClicked.addListener((info, tab) => {
if (info.menuItemId == "run-page-command") if (info.menuItemId == "run-page-command")
runUrlCommand(tab.url); runUrlCommand(tab.url);
else if (info.menuItemId == "run-link-command" && info.linkUrl)
runUrlCommand(info.linkUrl);
}); });

View File

@ -19,8 +19,9 @@
}, },
"permissions": [ "permissions": [
"nativeMessaging",
"activeTab", "activeTab",
"nativeMessaging",
"menus",
"tabs" "tabs"
], ],
@ -38,7 +39,6 @@
}, },
"options_ui": { "options_ui": {
"page": "options.html", "page": "options.html"
"open_in_tab": true
} }
} }