Add options-page section for “Download commands”

… though it is completely non-functional, as yet.
This commit is contained in:
Jaidyn Ann 2024-03-03 22:13:04 -06:00
parent a60d81009b
commit 596b383a54
4 changed files with 211 additions and 21 deletions

View File

@ -101,6 +101,41 @@
"description": "Displayed placeholder for options-page input for regex."
},
"optionsTitleDownload": {
"message": "Download commands",
"description": "Title of options-page section for download commands."
},
"optionsDescDownload": {
"message": "Assign commands to be executed for certain URLs (by regex) when downloads are initiated or finished. In commands, $URL will be replaced with the downloads URL, and $FILE with the target file.",
"description": "Description of options-page section for download commands."
},
"optionsHeadDownloadRule": {
"message": "URL regex",
"description": "Header of table-element for options-page section for download commands."
},
"optionsHeadDownloadCommand": {
"message": "Shell command",
"description": "Header of table-element for options-page section for download commands."
},
"optionsHeadDownloadType": {
"message": "Run when…",
"description": "Header of table-element for options-page section for download commands."
},
"optionsDownloadWhenStarted": {
"message": "… started",
"description": "Member of menu-list for options-page section for download commands. Completes the sentence, “Run when…”"
},
"optionsDownloadWhenFinished": {
"message": "… finished",
"description": "Member of menu-list for options-page section for download commands. Completes the sentence, “Run when…”"
},
"optionsSaveButton": {
"message": "Apply",
"description": "Name of button in options-page to save changes."

View File

@ -101,6 +101,41 @@
"description": "Provizora valoro ĉe agordopaĝo por enigo de reteja regula esprimo."
},
"optionsTitleDownload": {
"message": "Elŝutaj ŝel-ordonoj",
"description": "Titolo de agordo-parto por elŝutaj ordonoj."
},
"optionsDescDownload": {
"message": "Agordi ordonojn kiuj ruliĝu dum komenciĝo aŭ finiĝo de elŝutado, laŭ URL (regulesprime). En ordonoj, «$URL» anstataŭiĝos per la elŝuta URL, kaj «$FILE» per la elŝuta dosiervojo.",
"description": "Priskribo de agordo-parto por elŝutaj ordonoj."
},
"optionsHeadDownloadRule": {
"message": "URL regula esprimo",
"description": "Titolo de tabelo-kolumno ĉe agordo-parto por elŝutaj ordonoj."
},
"optionsHeadDownloadCommand": {
"message": "Ŝel-ordono",
"description": "Titolo de tabelo-kolumno ĉe agordo-parto por elŝutaj ordonoj."
},
"optionsHeadDownloadType": {
"message": "Rulu je…",
"description": "Titolo de tabelo-kolumno ĉe agordo-parto por elŝutaj ordonoj."
},
"optionsDownloadWhenStarted": {
"message": "… komenciĝo",
"description": "Menuero ĉe agordo-parto por elŝutaj ordonoj. Finas la frazon, «Rulu je…»"
},
"optionsDownloadWhenFinished": {
"message": "… finiĝo",
"description": "Menuero ĉe agordo-parto por elŝutaj ordonoj. Finas la frazon, «Rulu je…»"
},
"optionsSaveButton": {
"message": "Konservi",
"description": "Nomo de butono ĉe agordopaĝo por konservi ŝanĝojn."

View File

@ -4,30 +4,47 @@
<meta charset="utf8">
</head>
<body>
<section>
<h3 id="commandTitle">Shell commands</h3>
<p id="commandP">Add shell commands, to be executed with a pages URL as its argument. In a command, $URL will be replaced with the target pages URL.</p>
<table id="commandTable">
<tr>
<th id="commandNameTh">Name</th>
<th id="commandShellTh">Shell command</th>
</tr>
</table>
<button style="width: 5em;" id="save-cmd">Apply</button>
</section>
<article>
<section>
<h3 id="commandTitle">Shell commands</h3>
<p id="commandP">Add shell commands, to be executed with a pages URL as its argument. In a command, $URL will be replaced with the target pages URL.</p>
<table id="commandTable">
<tr>
<th id="commandNameTh">Name</th>
<th id="commandShellTh">Shell command</th>
</tr>
</table>
<button style="width: 5em;" id="save-cmd">Apply</button>
</section>
<section>
<h3 id="ruleTitle">URL/Page rules</h3>
<p id="ruleP">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.</p>
<table id="regexTable">
<section>
<h3 id="ruleTitle">URL/Page rules</h3>
<p id="ruleP">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.</p>
<table id="regexTable">
<tr>
<th id="ruleRegexTh">URL regex</th>
<th id="ruleShellTh">Shell command</th>
</tr>
</table>
<button style="width: 5em;" id="save-regex">Apply</button>
</section>
</article>
<hr/>
<article>
<h3 id="downloadTitle">Download commands</h3>
<p id="downloadP">Assign commands to be executed for certain URLs (by regex) when downloads are initiated or finished. In commands, $URL will be replaced with the downloads URL, and $FILE with the target file.</p>
<table id="downloadTable">
<tr>
<th id="ruleRegexTh">URL regex</th>
<th id="ruleShellTh">Shell command</th>
<th id="downloadTypeTh">On start/finish</th>
<th id="downloadRegexTh">URL regex</th>
<th id="downloadShellTh">Shell command</th>
</tr>
</table>
<button style="width: 5em;" id="save-regex">Apply</button>
</section>
<button style="width: 5em;" id="save-downloads">Apply</button>
</article>
<script src="options.js"></script>

View File

@ -35,6 +35,22 @@ function saveRegexRules() {
}
// Iterate over the commands-table and save each of the users valid
// name+command rows to storage.
function saveDownloadCommands() {
let downloads = [];
for (downloadTr of document.getElementsByClassName("downloadCommandRow")) {
let regex = downloadTr.getElementsByClassName("regex")[0].value;
let command = downloadTr.getElementsByClassName("command")[0].value;
let type = downloadTr.getElementsByClassName("startFinishMenu")[0].value;
if (regex && command && type)
downloads.push([regex, command, type]);
}
console.log(downloads);
localStorage.setItem("downloadCommands", JSON.stringify(downloads));
}
// Read the users saved name+command pairs from storage, and populate the
// command-table with them.
function populateCommandTable() {
@ -67,13 +83,29 @@ function populateRegexTable() {
}
// Read the users saved type+name+command pairs from storage, and populate the
// downloads-table with them.
function populateDownloadTable() {
let downloadTable = document.getElementById("downloadTable");
try {
let savedDownloads = savedArray("downloadCommands") || [];
for (regexCommandType of savedDownloads) {
let downloadTr = createDownloadTr(regexCommandType[0], regexCommandType[1], regexCommandType[2]);
downloadTable.appendChild(downloadTr);
}
} catch {};
// And yet again! Have spares!!!
downloadTable.appendChild(createDownloadTr("", "", 0));
}
// Create a <select> drop-down menu containing all of the users commands.
function createCommandMenu() {
let commandMenu = document.createElement("SELECT");
commandMenu.setAttribute("class", "commandMenu");
commandMenu.setAttribute("type", "text");
let savedCommands = savedArray("commands");
let savedCommands = savedArray("commands") || [];
for (let i = 0; i < savedCommands.length; i++) {
let commandOption = document.createElement("OPTION");
commandOption.setAttribute("value", i);
@ -84,6 +116,64 @@ function createCommandMenu() {
}
function createDownloadTypeMenu(type) {
let typeMenu = document.createElement("SELECT");
typeMenu.setAttribute("class", "startFinishMenu");
typeMenu.setAttribute("type", "text");
let onStartOption = document.createElement("OPTION");
onStartOption.setAttribute("value", 0);
onStartOption.text = browser.i18n.getMessage("optionsDownloadWhenStarted");
typeMenu.appendChild(onStartOption);
let onEndOption = document.createElement("OPTION");
onEndOption.setAttribute("value", 1);
onEndOption.text = browser.i18n.getMessage("optionsDownloadWhenFinished");
typeMenu.appendChild(onEndOption);
try {
typeMenu.childNodes[type].setAttribute("selected", true);
} catch { };
return typeMenu;
}
function createDownloadTr(regex, command, type) {
let typeMenu = createDownloadTypeMenu(type);
let typeTd = document.createElement("TD");
typeTd.appendChild(typeMenu);
let regexInput = document.createElement("INPUT");
regexInput.setAttribute("class", "regex");
regexInput.setAttribute("type", "text");
regexInput.setAttribute("placeholder", browser.i18n.getMessage("optionsPlaceholderDownloadRule"));
if (regex && command && type)
regexInput.setAttribute("value", regex);
let regexTd = document.createElement("TD");
regexTd.appendChild(regexInput);
let commandInput = document.createElement("INPUT");
commandInput.setAttribute("class", "command");
commandInput.setAttribute("type", "text");
commandInput.setAttribute("placeholder", browser.i18n.getMessage("optionsPlaceholderDownloadRule"));
if (regex && command && type)
commandInput.setAttribute("value", command);
let commandTd = document.createElement("TD");
commandTd.appendChild(commandInput);
let tr = document.createElement("TR");
tr.setAttribute("class", "downloadCommandRow");
tr.appendChild(typeTd);
tr.appendChild(regexTd);
tr.appendChild(commandTd);
return tr;
}
// Create a table-row for the command-table, with the command & regex inputs
// values set to the given parameters. If they are undefined, the inputs will
// have no value.
@ -97,11 +187,13 @@ function createRegexTr(regex, command_i, commandMenu) {
let regexTd = document.createElement("TD");
regexTd.appendChild(regexInput);
let commandTd = document.createElement("TD");
try {
commandMenu.childNodes[command_i].setAttribute("selected", true);
} catch { };
commandTd.appendChild(commandMenu);
let tr = document.createElement("TR");
tr.setAttribute("class", "regexCommandRow");
tr.appendChild(regexTd);
@ -156,8 +248,15 @@ function i18nPage() {
document.getElementById("ruleRegexTh").innerText = browser.i18n.getMessage("optionsHeadRuleName");
document.getElementById("ruleShellTh").innerText = browser.i18n.getMessage("optionsHeadRuleCommand");
document.getElementById("downloadTitle").innerText = browser.i18n.getMessage("optionsTitleDownload");
document.getElementById("downloadP").innerText = browser.i18n.getMessage("optionsDescDownload");
document.getElementById("downloadRegexTh").innerText = browser.i18n.getMessage("optionsHeadDownloadRule");
document.getElementById("downloadTypeTh").innerText = browser.i18n.getMessage("optionsHeadDownloadType");
document.getElementById("downloadShellTh").innerText = browser.i18n.getMessage("optionsHeadDownloadCommand");
document.getElementById("save-cmd").innerText = browser.i18n.getMessage("optionsSaveButton");
document.getElementById("save-regex").innerText = browser.i18n.getMessage("optionsSaveButton");
document.getElementById("save-downloads").innerText = browser.i18n.getMessage("optionsSaveButton");
}
@ -168,10 +267,14 @@ document.addEventListener("click", e => {
} else if (e.target.id == ("save-regex")) {
saveRegexRules();
location.reload();
}
} else if (e.target.id == ("save-downloads")) {
saveDownloadCommands();
location.reload();
};
});
populateCommandTable();
populateRegexTable();
populateDownloadTable();
i18nPage();