From 45462ffa98af1dd83d0a5cbd62b8ce54a94abd6a Mon Sep 17 00:00:00 2001
From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com>
Date: Sat, 2 Mar 2024 13:25:06 -0600
Subject: [PATCH] Register commands and URL-regexes separately
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This allows easy re-use of commands, tying names
to URL-regexes rather than command-strings.
Much more user-friendly, I reckon!
Also, now we use “$URL” instead of “%s” for URL
substitution in commands. More shelly that way.
---
background.js | 10 ++--
options.html | 44 ++++++++++-------
options.js | 134 +++++++++++++++++++++++++++++++++++++++++---------
3 files changed, 144 insertions(+), 44 deletions(-)
diff --git a/background.js b/background.js
index 94c6867..c5a73c3 100644
--- a/background.js
+++ b/background.js
@@ -16,18 +16,20 @@ function getUrlCommand(url) {
let matchRegex = "";
try {
let savedCommands = JSON.parse(localStorage.getItem("commands"));
+ let savedRegexRules = JSON.parse(localStorage.getItem("urlRules"));
// Find the most-applicable command…
- for (regexCommandPair of savedCommands) {
- let regex = regexCommandPair[0];
+ for (regexCommandIPair of savedRegexRules) {
+ let regex = regexCommandIPair[0];
let match = url.match(regex);
let compared = compareRegexComplexity(matchRegex, regex);
if (match && (compared == 0 || compared == 1)) {
- matchCommand = regexCommandPair[1];
+ let command_i = regexCommandIPair[1];
+ matchCommand = savedCommands[command_i][1];
matchRegex = regex;
}
}
// … and replace the substitution-string with the URL.
- matchCommand = matchCommand.replaceAll("%s", url);
+ matchCommand = matchCommand.replaceAll("$URL", url);
} catch {};
return matchCommand;
}
diff --git a/options.html b/options.html
index 4c4528d..d172235 100644
--- a/options.html
+++ b/options.html
@@ -1,24 +1,34 @@
+
+
+
-
-
-
URL regex
-
Shell command
-
-
-
+
+
Shell commands
+
Add shell commands, to be executed with a page’s URL as its argument. In a command, $URL will be replaced with the target page’s URL.
+
+
+
Name
+
Shell command
+
+
+
+
+
+
+
+
URL/Page rules
+
Associate the above commands with URLs, based on regex rules. When a URL is tied to a command, a button will appear in context-menus and the address bar to run said command.
+
+
+
URL regex
+
Shell command
+
+
+
+
-
diff --git a/options.js b/options.js
index befb0bc..9b4b39d 100644
--- a/options.js
+++ b/options.js
@@ -1,25 +1,50 @@
+// Given the name of an array saved to localStorage, return it (if possible).
+function savedArray(name) {
+ let saved = [];
+ try {
+ saved = JSON.parse(localStorage.getItem(name));
+ return saved;
+ } catch { };
+ return saved;
+}
+
+
// Iterate over the commands-table and save each of the user’s valid
-// regex+command rows to storage.
+// name+command rows to storage.
function saveCommands() {
let commands = [];
- for (commandTr of document.getElementsByClassName("regexCommandRow")) {
- let regex = commandTr.getElementsByClassName("regex")[0].value;
+ for (commandTr of document.getElementsByClassName("nameCommandRow")) {
+ let name = commandTr.getElementsByClassName("name")[0].value;
let command = commandTr.getElementsByClassName("command")[0].value;
- if (regex && command)
- commands.push([regex, command]);
+ if (name && command)
+ commands.push([name, command]);
}
localStorage.setItem("commands", JSON.stringify(commands));
}
-// Read the user’s saved command-regex pairs from storage, and populate the
+// Iterate over the regex-table and save each of the user’s valid
+// regex+command-index rows to storage.
+function saveRegexRules() {
+ let rules = [];
+ for (regexTr of document.getElementsByClassName("regexCommandRow")) {
+ let regex = regexTr.getElementsByClassName("regex")[0].value;
+ let command_i = regexTr.getElementsByClassName("commandMenu")[0].value;
+ if (regex && command_i)
+ rules.push([regex, command_i]);
+ }
+ localStorage.setItem("urlRules", JSON.stringify(rules));
+}
+
+
+// Read the user’s saved name+command pairs from storage, and populate the
// command-table with them.
function populateCommandTable() {
let commandTable = document.getElementById("commandTable");
try {
- let savedCommands = JSON.parse(localStorage.getItem("commands"));
- for (cmdRegex of savedCommands) {
- let commandTr = createCommandTr(cmdRegex[0], cmdRegex[1]);
+ let savedCommands = savedArray("commands");
+ for (cmdName of savedCommands) {
+ let commandTr = createCommandTr(cmdName[0], cmdName[1]);
commandTable.appendChild(commandTr);
}
} catch { };
@@ -28,30 +53,57 @@ function populateCommandTable() {
}
+// Read the user’s saved regex+command-index pairs from storage, and populate the
+// command-table with them.
+function populateRegexTable() {
+ let regexTable = document.getElementById("regexTable");
+ try {
+ let savedRegex = savedArray("urlRules");
+ for (let i = 0; i < savedRegex.length; i++) {
+ let regexTr = createRegexTr(savedRegex[i][0], savedRegex[i][1], createCommandMenu());
+ regexTable.appendChild(regexTr);
+ }
+ } catch { };
+ // Always, again, have a spare tire!!
+ regexTable.appendChild(createRegexTr("", "", createCommandMenu()));
+}
+
+
+// Create a