Compare commits
No commits in common. "2c73b5a956d151444fc38a79cd2ba1542a7a9b93" and "a60d81009b62679807a1a3703f8ffa8243ac8809" have entirely different histories.
2c73b5a956
...
a60d81009b
|
@ -101,51 +101,6 @@
|
|||
"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 download’s 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…”"
|
||||
},
|
||||
|
||||
"optionsPlaceholderDownloadRegex": {
|
||||
"message": ".*.png",
|
||||
"description": "Displayed placeholder for options-page input for download command’s regex."
|
||||
},
|
||||
|
||||
"optionsPlaceholderDownloadCommand": {
|
||||
"message": "echo $URL > $FILE.url",
|
||||
"description": "Displayed placeholder for options-page input for download command."
|
||||
},
|
||||
|
||||
"optionsSaveButton": {
|
||||
"message": "Apply",
|
||||
"description": "Name of button in options-page to save changes."
|
||||
|
|
|
@ -101,51 +101,6 @@
|
|||
"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…»"
|
||||
},
|
||||
|
||||
"optionsPlaceholderDownloadRegex": {
|
||||
"message": ".*.png",
|
||||
"description": "Provizora valoro ĉe agordopaĝo por enigo de regula esprimo por elŝuta ordono."
|
||||
},
|
||||
|
||||
"optionsPlaceholderDownloadCommand": {
|
||||
"message": "echo $URL > $FILE.url",
|
||||
"description": "Provizora valoro ĉe agordopaĝo por enigo de elŝuta ordono."
|
||||
},
|
||||
|
||||
"optionsSaveButton": {
|
||||
"message": "Konservi",
|
||||
"description": "Nomo de butono ĉe agordopaĝo por konservi ŝanĝojn."
|
||||
|
|
|
@ -48,42 +48,12 @@ function getUrlCommands(url) {
|
|||
}
|
||||
|
||||
|
||||
// Return the download-command string (if any) associated with a URL and type
|
||||
// integer (0→started; 1→finished).
|
||||
function getDownloadCommand(url, type) {
|
||||
let matchCommand = undefined;
|
||||
let matchRegex = "";
|
||||
try {
|
||||
let savedDownloads = savedArray("downloadCommands");
|
||||
// Find the most-applicable command.
|
||||
for (regexCommandType of savedDownloads) {
|
||||
let regex = regexCommandType[0];
|
||||
let match = url.match(regex);
|
||||
|
||||
let compared = compareRegexComplexity(matchRegex, regex);
|
||||
if ((match && (compared == 0 || compared == 1))
|
||||
&& (regexCommandType[2] == type))
|
||||
{
|
||||
matchCommand = regexCommandType[1];
|
||||
matchRegex = regex;
|
||||
}
|
||||
}
|
||||
} catch {};
|
||||
return matchCommand;
|
||||
}
|
||||
|
||||
|
||||
// Execute the given command string, subsituting “$URL” with url and
|
||||
// “$FILE” with filepath.
|
||||
function runCommand(command, url, filepath) {
|
||||
// Execute the given command string, subsituting “$URL” with the given url.
|
||||
function runCommand(command, url) {
|
||||
if (!port)
|
||||
initShellfoxProgram();
|
||||
if (command && port)
|
||||
port.postMessage(command
|
||||
.replaceAll("$URL", url)
|
||||
.replaceAll("${URL}", url)
|
||||
.replaceAll("$FILE", filepath)
|
||||
.replaceAll("${FILE}", filepath));
|
||||
port.postMessage(command.replaceAll("$URL", url));
|
||||
}
|
||||
|
||||
|
||||
|
@ -131,9 +101,8 @@ function hideLinkContextMenuItem() {
|
|||
}
|
||||
|
||||
|
||||
// (Re-)Create the menu-items for each context menu.
|
||||
function createCommandMenuItems() {
|
||||
let savedCommands = savedArray("commands") || [];
|
||||
let savedCommands = savedArray("commands");
|
||||
for (let i = 0; i < savedCommands.length; i++) {
|
||||
let nameCommandPair = savedCommands[i];
|
||||
let name = nameCommandPair[0];
|
||||
|
@ -246,26 +215,6 @@ browser.menus.onClicked.addListener((info, tab) => {
|
|||
});
|
||||
|
||||
|
||||
// When a download starts, run any applicable download commands.
|
||||
browser.downloads.onCreated.addListener((downloadItem) => {
|
||||
let command = getDownloadCommand(downloadItem.url, 0);
|
||||
if (command)
|
||||
runCommand(command, downloadItem.url, downloadItem.filename);
|
||||
});
|
||||
|
||||
|
||||
// When a download completes, run any applicable download commands.
|
||||
browser.downloads.onChanged.addListener((downloadDelta) => {
|
||||
browser.downloads.search({ "id": downloadDelta.id }).then((downloadItems) => {
|
||||
if (downloadDelta.state.current == "complete" && downloadItems.length > 0) {
|
||||
let command = getDownloadCommand(downloadItems[0].url, 1);
|
||||
if (command)
|
||||
runCommand(command, downloadItems[0].url, downloadItems[0].filename);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
// Whenever settings (commands) are updated, repopulate context-menus’ items.
|
||||
window.addEventListener("storage", (e) => {
|
||||
createCommandMenuItems();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"manifest_version": 2,
|
||||
"name": "__MSG_extensionName__",
|
||||
"version": "0.12",
|
||||
"version": "0.1",
|
||||
|
||||
"description": "__MSG_extensionDescription__",
|
||||
"homepage_url": "https://hak.xwx.moe/jadedctrl/shellfox",
|
||||
|
@ -25,7 +25,6 @@
|
|||
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"downloads",
|
||||
"nativeMessaging",
|
||||
"menus",
|
||||
"tabs"
|
||||
|
|
17
options.html
17
options.html
|
@ -4,7 +4,6 @@
|
|||
<meta charset="utf8">
|
||||
</head>
|
||||
<body>
|
||||
<article>
|
||||
<section>
|
||||
<h3 id="commandTitle">Shell commands</h3>
|
||||
<p id="commandP">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.</p>
|
||||
|
@ -29,22 +28,6 @@
|
|||
</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 download’s URL, and $FILE with the target file.</p>
|
||||
<table id="downloadTable">
|
||||
<tr>
|
||||
<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-downloads">Apply</button>
|
||||
</article>
|
||||
|
||||
|
||||
<script src="options.js"></script>
|
||||
|
|
109
options.js
109
options.js
|
@ -35,22 +35,6 @@ function saveRegexRules() {
|
|||
}
|
||||
|
||||
|
||||
// Iterate over the commands-table and save each of the user’s 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 user’s saved name+command pairs from storage, and populate the
|
||||
// command-table with them.
|
||||
function populateCommandTable() {
|
||||
|
@ -83,29 +67,13 @@ function populateRegexTable() {
|
|||
}
|
||||
|
||||
|
||||
// Read the user’s 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 user’s 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);
|
||||
|
@ -116,66 +84,6 @@ 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("optionsPlaceholderDownloadRegex"));
|
||||
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("optionsPlaceholderDownloadCommand"));
|
||||
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.
|
||||
|
@ -189,13 +97,11 @@ 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);
|
||||
|
@ -250,15 +156,8 @@ 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");
|
||||
}
|
||||
|
||||
|
||||
|
@ -269,14 +168,10 @@ 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();
|
||||
|
|
Ŝarĝante…
Reference in New Issue