2024-02-27 23:53:53 -06:00
|
|
|
|
let port = browser.runtime.connectNative("shellfox");
|
|
|
|
|
|
|
|
|
|
|
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-02-28 18:52:02 -06:00
|
|
|
|
// Find the most-applicable command…
|
2024-02-28 18:37:13 -06:00
|
|
|
|
for (regexCommandPair of savedCommands) {
|
|
|
|
|
let regex = regexCommandPair[0];
|
|
|
|
|
let match = url.match(regex);
|
|
|
|
|
let compared = compareRegexComplexity(matchRegex, regex);
|
|
|
|
|
if (match && (compared == 0 || compared == 1)) {
|
|
|
|
|
matchCommand = regexCommandPair[1];
|
|
|
|
|
matchRegex = regex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-28 18:52:02 -06:00
|
|
|
|
// … and replace the substitution-string with the URL.
|
|
|
|
|
matchCommand = matchCommand.replaceAll("%s", 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) {
|
|
|
|
|
console.log("Executing…");
|
|
|
|
|
let command = getUrlCommand(url);
|
|
|
|
|
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-02-27 23:53:53 -06:00
|
|
|
|
port.onDisconnect.addListener((port) => {
|
|
|
|
|
console.log(port.error);
|
|
|
|
|
port = undefined;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2024-02-28 18:37:13 -06:00
|
|
|
|
browser.pageAction.onClicked.addListener((tab) => {
|
|
|
|
|
console.log("onClicked");
|
|
|
|
|
runUrlCommand(tab.url);
|
2024-02-27 23:53:53 -06:00
|
|
|
|
});
|
2024-02-28 18:37:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
|
|
|
|
console.log("onUpdated");
|
|
|
|
|
if (getUrlCommand(tab.url))
|
|
|
|
|
browser.pageAction.show(tabId);
|
|
|
|
|
else
|
|
|
|
|
browser.pageAction.hide(tabId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
browser.tabs.onActivated.addListener((activeInfo) => {
|
|
|
|
|
console.log("activated");
|
|
|
|
|
let url = browser.tabs.get(activeInfo.tabId).url;
|
|
|
|
|
});
|
|
|
|
|
|