From 0ba9d329151582fce37aade6a940ccfad8476c05 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:34 -0600 Subject: [PATCH] Add support for download-commands Now you can set commands to run on the beginning or completion of a file-download. --- background.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- manifest.json | 3 ++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/background.js b/background.js index 8906b13..afd5aff 100644 --- a/background.js +++ b/background.js @@ -48,12 +48,42 @@ function getUrlCommands(url) { } -// Execute the given command string, subsituting “$URL” with the given url. -function runCommand(command, 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) { if (!port) initShellfoxProgram(); 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() { - let savedCommands = savedArray("commands"); + let savedCommands = savedArray("commands") || []; for (let i = 0; i < savedCommands.length; i++) { let nameCommandPair = savedCommands[i]; 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. window.addEventListener("storage", (e) => { createCommandMenuItems(); diff --git a/manifest.json b/manifest.json index 2d37aca..73d1816 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "__MSG_extensionName__", - "version": "0.1", + "version": "0.12", "description": "__MSG_extensionDescription__", "homepage_url": "https://hak.xwx.moe/jadedctrl/shellfox", @@ -25,6 +25,7 @@ "permissions": [ "activeTab", + "downloads", "nativeMessaging", "menus", "tabs"