From a41b7a70cf1de2c41e5dc15ecac36eda795c1e67 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:37:13 -0600 Subject: [PATCH] Execute configured command for URL, on button-hit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we don’t run a hardcoded command when the address-bar button is pressed, but the command configured for that URL. Also, we only enable the address-bar button for tabs whose URL is matched by some command. --- background.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++-- manifest.json | 7 +++--- options.js | 11 +++++---- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/background.js b/background.js index d9982df..26793c7 100644 --- a/background.js +++ b/background.js @@ -1,12 +1,73 @@ let port = browser.runtime.connectNative("shellfox"); +// 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")); + for (regexCommandPair of savedCommands) { + let regex = regexCommandPair[0]; + let match = url.match(regex); + let compared = compareRegexComplexity(matchRegex, regex); + console.log(matchRegex, "v", regex, "=", compared); + if (match && (compared == 0 || compared == 1)) { + matchCommand = regexCommandPair[1]; + matchRegex = regex; + } + } + } 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; +} + + port.onDisconnect.addListener((port) => { console.log(port.error); port = undefined; }); -browser.pageAction.onClicked.addListener(() => { - port.postMessage("emacs /tmp/f"); +browser.pageAction.onClicked.addListener((tab) => { + console.log("onClicked"); + runUrlCommand(tab.url); }); + + +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; +}); + diff --git a/manifest.json b/manifest.json index 74a7a7f..c2983e3 100644 --- a/manifest.json +++ b/manifest.json @@ -15,12 +15,13 @@ "19": "res/shellfox-19.png", "38": "res/shellfox-38.png" }, - "default_title": "Shellfox", - "show_matches": [""] + "default_title": "Shellfox" }, "permissions": [ - "nativeMessaging" + "nativeMessaging", + "activeTab", + "tabs" ], "browser_specific_settings": { diff --git a/options.js b/options.js index 1bcfebc..befb0bc 100644 --- a/options.js +++ b/options.js @@ -16,10 +16,13 @@ function saveCommands() { // command-table with them. function populateCommandTable() { let commandTable = document.getElementById("commandTable"); - for (cmdRegex of JSON.parse(localStorage.getItem("commands"))) { - let commandTr = createCommandTr(cmdRegex[0], cmdRegex[1]); - commandTable.appendChild(commandTr); - } + try { + let savedCommands = JSON.parse(localStorage.getItem("commands")); + for (cmdRegex of savedCommands) { + let commandTr = createCommandTr(cmdRegex[0], cmdRegex[1]); + commandTable.appendChild(commandTr); + } + } catch { }; // Always add a spare entry. commandTable.appendChild(createCommandTr("", "")); }