Add support for download-commands
Now you can set commands to run on the beginning or completion of a file-download.
This commit is contained in:
parent
596b383a54
commit
0ba9d32915
|
@ -48,12 +48,42 @@ function getUrlCommands(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Execute the given command string, subsituting “$URL” with the given url.
|
// Return the download-command string (if any) associated with a URL and type
|
||||||
function runCommand(command, url) {
|
// 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) {
|
||||||
if (!port)
|
if (!port)
|
||||||
initShellfoxProgram();
|
initShellfoxProgram();
|
||||||
if (command && port)
|
if (command && port)
|
||||||
port.postMessage(command.replaceAll("$URL", url));
|
port.postMessage(command
|
||||||
|
.replaceAll("$URL", url)
|
||||||
|
.replaceAll("${URL}", url)
|
||||||
|
.replaceAll("$FILE", filepath)
|
||||||
|
.replaceAll("${FILE}", filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,8 +131,9 @@ function hideLinkContextMenuItem() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// (Re-)Create the menu-items for each context menu.
|
||||||
function createCommandMenuItems() {
|
function createCommandMenuItems() {
|
||||||
let savedCommands = savedArray("commands");
|
let savedCommands = savedArray("commands") || [];
|
||||||
for (let i = 0; i < savedCommands.length; i++) {
|
for (let i = 0; i < savedCommands.length; i++) {
|
||||||
let nameCommandPair = savedCommands[i];
|
let nameCommandPair = savedCommands[i];
|
||||||
let name = nameCommandPair[0];
|
let name = nameCommandPair[0];
|
||||||
|
@ -215,6 +246,26 @@ 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.
|
// Whenever settings (commands) are updated, repopulate context-menus’ items.
|
||||||
window.addEventListener("storage", (e) => {
|
window.addEventListener("storage", (e) => {
|
||||||
createCommandMenuItems();
|
createCommandMenuItems();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "__MSG_extensionName__",
|
"name": "__MSG_extensionName__",
|
||||||
"version": "0.1",
|
"version": "0.12",
|
||||||
|
|
||||||
"description": "__MSG_extensionDescription__",
|
"description": "__MSG_extensionDescription__",
|
||||||
"homepage_url": "https://hak.xwx.moe/jadedctrl/shellfox",
|
"homepage_url": "https://hak.xwx.moe/jadedctrl/shellfox",
|
||||||
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"activeTab",
|
"activeTab",
|
||||||
|
"downloads",
|
||||||
"nativeMessaging",
|
"nativeMessaging",
|
||||||
"menus",
|
"menus",
|
||||||
"tabs"
|
"tabs"
|
||||||
|
|
Ŝarĝante…
Reference in New Issue