2024-02-28 22:30:32 -06:00
|
|
|
|
let port = undefined;
|
|
|
|
|
|
|
|
|
|
// Run the shellfox helper program.
|
2024-02-28 23:23:08 -06:00
|
|
|
|
function initShellfoxProgram() {
|
2024-02-28 22:30:32 -06:00
|
|
|
|
port = browser.runtime.connectNative("shellfox");
|
|
|
|
|
port.onDisconnect.addListener((port) => {
|
|
|
|
|
console.log(port.error);
|
|
|
|
|
port = undefined;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-27 23:53:53 -06:00
|
|
|
|
|
|
|
|
|
|
2024-02-28 18:37:13 -06:00
|
|
|
|
// Return the command-string associated with a URL, if any.
|
|
|
|
|
function getUrlCommand(url) {
|
|
|
|
|
let matchCommand = undefined;
|
|
|
|
|
let matchRegex = "";
|
|
|
|
|
try {
|
|
|
|
|
let savedCommands = JSON.parse(localStorage.getItem("commands"));
|
2024-03-02 13:25:06 -06:00
|
|
|
|
let savedRegexRules = JSON.parse(localStorage.getItem("urlRules"));
|
2024-02-28 18:52:02 -06:00
|
|
|
|
// Find the most-applicable command…
|
2024-03-02 13:25:06 -06:00
|
|
|
|
for (regexCommandIPair of savedRegexRules) {
|
|
|
|
|
let regex = regexCommandIPair[0];
|
2024-02-28 18:37:13 -06:00
|
|
|
|
let match = url.match(regex);
|
|
|
|
|
let compared = compareRegexComplexity(matchRegex, regex);
|
|
|
|
|
if (match && (compared == 0 || compared == 1)) {
|
2024-03-02 13:25:06 -06:00
|
|
|
|
let command_i = regexCommandIPair[1];
|
|
|
|
|
matchCommand = savedCommands[command_i][1];
|
2024-02-28 18:37:13 -06:00
|
|
|
|
matchRegex = regex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-28 18:52:02 -06:00
|
|
|
|
// … and replace the substitution-string with the URL.
|
2024-03-02 13:25:06 -06:00
|
|
|
|
matchCommand = matchCommand.replaceAll("$URL", url);
|
2024-02-28 18:37:13 -06:00
|
|
|
|
} catch {};
|
|
|
|
|
return matchCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Execute the shell command associated with the given URL, if any.
|
|
|
|
|
function runUrlCommand(url) {
|
|
|
|
|
let command = getUrlCommand(url);
|
2024-02-28 22:30:32 -06:00
|
|
|
|
if (!port)
|
2024-02-28 23:23:08 -06:00
|
|
|
|
initShellfoxProgram();
|
2024-02-28 18:37:13 -06:00
|
|
|
|
if (command && port) {
|
|
|
|
|
port.postMessage(command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Compare two regular expressions, returning which one is more specific.
|
|
|
|
|
// Returns -1 if a is more specific, 1 if b is, and 0 if they are equal.
|
|
|
|
|
// It’s a simple (and unreliable) algorithm, for now — purely based on length.
|
|
|
|
|
function compareRegexComplexity(a, b) {
|
|
|
|
|
if (a && ((a && !b) || (a.length > b.length)))
|
|
|
|
|
return -1;
|
|
|
|
|
else if (b && ((b && !a) || (a.length < b.length)))
|
|
|
|
|
return 1
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-02 11:39:44 -06:00
|
|
|
|
// 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 });
|
2024-02-28 23:23:08 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-02 11:39:44 -06:00
|
|
|
|
// Hide the “Run shell command context-menu item.
|
|
|
|
|
function hidePageContextMenuItem() {
|
|
|
|
|
browser.menus.update("run-page-command", { "visible": false });
|
2024-02-28 23:23:08 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-02 11:39:44 -06:00
|
|
|
|
// 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 page’s associated command.
|
|
|
|
|
browser.menus.create(
|
|
|
|
|
{
|
|
|
|
|
id: "run-page-command",
|
|
|
|
|
title: "Run shell command",
|
|
|
|
|
contexts: ["page"]
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add a context-menu item for running the command associated with a link.
|
|
|
|
|
browser.menus.create(
|
|
|
|
|
{
|
|
|
|
|
id: "run-link-command",
|
|
|
|
|
title: "Run command on link",
|
|
|
|
|
contexts: ["link"]
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
2024-02-28 22:30:32 -06:00
|
|
|
|
// When the address-bar button is clicked, run the according command (if any).
|
2024-02-28 18:37:13 -06:00
|
|
|
|
browser.pageAction.onClicked.addListener((tab) => {
|
|
|
|
|
runUrlCommand(tab.url);
|
2024-02-27 23:53:53 -06:00
|
|
|
|
});
|
2024-02-28 18:37:13 -06:00
|
|
|
|
|
|
|
|
|
|
2024-03-02 11:39:44 -06:00
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2024-02-28 22:30:32 -06:00
|
|
|
|
// When a tab’s URL has been changed, enable/disable the address-bar button
|
|
|
|
|
// based on whether or not there is an according command.
|
2024-02-28 18:37:13 -06:00
|
|
|
|
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
2024-02-28 23:23:08 -06:00
|
|
|
|
let command = getUrlCommand(tab.url);
|
2024-03-02 11:39:44 -06:00
|
|
|
|
if (command)
|
2024-02-28 18:37:13 -06:00
|
|
|
|
browser.pageAction.show(tabId);
|
2024-03-02 11:39:44 -06:00
|
|
|
|
else
|
2024-02-28 18:37:13 -06:00
|
|
|
|
browser.pageAction.hide(tabId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2024-02-28 22:30:32 -06:00
|
|
|
|
// When the active tab has changed, enable/disable the address-bar button based
|
|
|
|
|
// on whether or not there is an according command for it.
|
2024-02-28 18:37:13 -06:00
|
|
|
|
browser.tabs.onActivated.addListener((activeInfo) => {
|
2024-02-28 22:30:32 -06:00
|
|
|
|
browser.tabs.get(activeInfo.tabId).then((tab) => {
|
2024-03-02 11:39:44 -06:00
|
|
|
|
if (getUrlCommand(tab.url))
|
2024-02-28 22:30:32 -06:00
|
|
|
|
browser.pageAction.show(tab.id);
|
2024-03-02 11:39:44 -06:00
|
|
|
|
else
|
2024-02-28 23:23:08 -06:00
|
|
|
|
browser.pageAction.hide(tab.id);
|
2024-02-28 22:30:32 -06:00
|
|
|
|
});
|
2024-02-28 18:37:13 -06:00
|
|
|
|
});
|
2024-02-28 23:23:08 -06:00
|
|
|
|
|
|
|
|
|
|
2024-03-02 11:39:44 -06:00
|
|
|
|
// When a context-menu item is selected, let’s execute its will!
|
2024-02-28 23:23:08 -06:00
|
|
|
|
browser.menus.onClicked.addListener((info, tab) => {
|
|
|
|
|
if (info.menuItemId == "run-page-command")
|
|
|
|
|
runUrlCommand(tab.url);
|
2024-03-02 11:39:44 -06:00
|
|
|
|
else if (info.menuItemId == "run-link-command" && info.linkUrl)
|
|
|
|
|
runUrlCommand(info.linkUrl);
|
|
|
|
|
|
2024-02-28 23:23:08 -06:00
|
|
|
|
});
|