From b392c0ea00501b4a134a4987cb7ca1c7907a775b Mon Sep 17 00:00:00 2001 From: Ng Guoyou Date: Sat, 2 Feb 2019 15:22:18 +0800 Subject: [PATCH] Add option to always prefer links when downloading from pages that match a regex list of URLs #100 --- CHANGELOG.md | 5 + _locales/en/messages.json | 81 +-- manifest.json | 2 +- package.json | 8 +- src/menu.js | 47 +- src/option.js | 7 + src/options/options.html | 1020 ++++++++++++++++++++++--------------- src/options/style.css | 4 + 8 files changed, 729 insertions(+), 445 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 871ad11..5f5e961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 3.4.0 + +* Add option to always prefer links when downloading from pages that match a regex list of URLs (#100) +* Add notification option for when a link is downloaded instead of the source + # 3.3.0 * Add downloading of multiple highlighted tabs (FF63, #91) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index cb814fa..367f370 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -5,8 +5,7 @@ }, "extensionDescription": { - "message": - "Saves {image, video, audio, links, selection, page} in directories relative to the default download directory.", + "message": "Saves {image, video, audio, links, selection, page} in directories relative to the default download directory.", "description": "Description of the add-on." }, @@ -22,8 +21,7 @@ "contextMenuItemOptions": { "message": "Options", - "description": - "Title of context menu item that opens the options menu when clicked." + "description": "Title of context menu item that opens the options menu when clicked." }, "contextMenuExclusive": { @@ -63,8 +61,7 @@ "contextMenuShowDefaultFolder": { "message": "Open Default Folder", - "description": - "Title of context menu item that opens the default downloads directory in the system file manager when clicked." + "description": "Title of context menu item that opens the default downloads directory in the system file manager when clicked." }, "notificationSuccessTitle": { @@ -90,14 +87,12 @@ "notificationRuleMatchFailedExclusiveTitle": { "message": "Save In: Failed to route or rename download", - "description": - "Notification title for no rule matches when in exclusive mode" + "description": "Notification title for no rule matches when in exclusive mode" }, "notificationRuleMatchFailedExclusiveMessage": { "message": "No matching rule found for $URL$", - "description": - "Notification title for no rule matches when in exclusive mode", + "description": "Notification title for no rule matches when in exclusive mode", "placeholders": { "url": { "content": "$1", @@ -158,8 +153,7 @@ "rulePathStartsWithDot": { "message": "Path cannot start with .. or /", - "description": - "Error message for paths in routing rules that start with invalid characters" + "description": "Error message for paths in routing rules that start with invalid characters" }, "tabstripMenuSelectedTab": { @@ -185,20 +179,17 @@ "tabstripMenuSaveRightTabs": { "message": "Save all tabs to right including this", - "description": - "Tabstrip context menu label for saving tabs to the right (inclusive)" + "description": "Tabstrip context menu label for saving tabs to the right (inclusive)" }, "tabstripMenuSaveRightTabsMatched": { "message": "Save all tabs to right including this (must match a route)", - "description": - "Tabstrip context menu label for saving tabs to the right (inclusive) that match a route" + "description": "Tabstrip context menu label for saving tabs to the right (inclusive) that match a route" }, "o_sDownloads": { "message": "Downloads", - "description": - "o prefixes are for options page text, s prefix are for section/subsection titles" + "description": "o prefixes are for options page text, s prefix are for section/subsection titles" }, "o_sDirectories": { @@ -287,13 +278,11 @@ }, "o_lDirectoryInstructionSymlink": { - "message": - "A symlink is needed to save outside the default download directory." + "message": "A symlink is needed to save outside the default download directory." }, "o_lRoutingInstruction": { - "message": - "Automate routing of downloads or renaming of files. Separate multiple rules with an empty line. See the guide for more details." + "message": "Automate routing of downloads or renaming of files. Separate multiple rules with an empty line. See the guide for more details." }, "o_lRoutingLastDownload": { @@ -337,8 +326,7 @@ }, "o_lAccessKeysInstruction": { - "message": - "Set accesskeys for Save In menu items. Chrome or Firefox >= 63." + "message": "Set accesskeys for Save In menu items. Chrome or Firefox >= 63." }, "o_cLastUsedMenu": { @@ -351,12 +339,11 @@ }, "o_cPreferLinks": { - "message": "Prefer links over sources instead" + "message": "Always prefer links over sources" }, "o_cSaveText": { - "message": - "Enable saving of selected text with a \".selection.txt\" extension" + "message": "Enable saving of selected text with a \".selection.txt\" extension" }, "o_cSavePage": { @@ -384,8 +371,7 @@ }, "o_cOpenDialogShift": { - "message": - "Open a save file dialog when menu item is clicked and SHIFT is held down" + "message": "Open a save file dialog when menu item is clicked and SHIFT is held down" }, "o_cOpenDialogOnFailure": { @@ -417,8 +403,7 @@ }, "o_cExclusiveModeHelp": { - "message": - "Disables submenu for two-click downloads, saves relative to default downloads directory" + "message": "Disables submenu for two-click downloads, saves relative to default downloads directory" }, "o_cNotifySuccess": { @@ -438,8 +423,7 @@ }, "o_cNotifyCloseAfterHelp": { - "message": - "1s = 1000ms. Will not extend beyond OS/browser notification duration." + "message": "1s = 1000ms. Will not extend beyond OS/browser notification duration." }, "o_cSaveShortcutsAs": { @@ -495,8 +479,7 @@ }, "o_cKeyboardShortcutClickToHelp": { - "message": - "Hold down Alt (default) and click images or media to save them in the last used directory or default download directory. Injects a script into pages to listen for click events. Not all variables are available when using this. Requires page refresh to take effect." + "message": "Hold down Alt (default) and click images or media to save them in the last used directory or default download directory. Injects a script into pages to listen for click events. Not all variables are available when using this. Requires page refresh to take effect." }, "o_cKeyboardShotcutKeycode": { @@ -516,8 +499,7 @@ }, "o_cKeyboardShortcutModifierHelp": { - "message": - "Modifier keycode for click-to-save. Requires page refresh to take effect." + "message": "Modifier keycode for click-to-save. Requires page refresh to take effect." }, "o_cRestoreDefaults": { @@ -586,5 +568,30 @@ "o_lSubdirectoryInstructionComment": { "message": "at the start creates nested submenus depending on the number of arrows." + }, + + "notificationLinkPreferred": { + "message": "A link was downloaded instead", + "description": "Notification title when link was downloaded over a source" + }, + + "o_cPreferLinksFilterEnabled": { + "message": "Prefer links for pages that match the following URLs" + }, + + "o_cPreferLinksFilter": { + "message": "URL filters" + }, + + "o_cPreferLinksFilterHelp": { + "message": "One regular expression to match against the page URL per line." + }, + + "notificationBadPreferLinksPattern": { + "message": "Invalid regex for prefer links" + }, + + "o_cNotifyLinkPreferred": { + "message": "Show notification when a link was downloaded instead of the source" } } diff --git a/manifest.json b/manifest.json index bfd082e..6e4b344 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "__MSG_extensionName__", "description": "__MSG_extensionDescription__", - "version": "3.3.0", + "version": "3.4.0", "default_locale": "en", "applications": { diff --git a/package.json b/package.json index 06f5e04..73bede0 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "save-in", - "version": "3.3.0", + "version": "3.4.0", "license": "MIT", "scripts": { "build": "env -u WEB_EXT_API_KEY -u WEB_EXT_API_SECRET web-ext build --overwrite-dest -i test docs yarn.lock yarn-error.log", "build:firefox:submit": "web-ext sign", "d": "env -u WEB_EXT_API_KEY -u WEB_EXT_API_SECRET web-ext run --verbose --start-url about:debugging --start-url about:addons", "d:noenv": "web-ext run --verbose", - "d:win:stable": "web-ext run --verbose --firefox=\"C:/Program Files/Mozilla Firefox/firefox.exe\" -s=\".\"", - "d:win:nightly": "web-ext run --verbose --firefox=\"C:/Program Files/Nightly/firefox.exe\" -s=\".\"", - "lint": "env -u WEB_EXT_API_KEY -u WEB_EXT_API_SECRET web-ext lint && eslint src test", + "d:win:stable": "web-ext run --start-url about:debugging --start-url about:addons --verbose --firefox=\"C:/Program Files/Mozilla Firefox/firefox.exe\" -s=\".\"", + "d:win:nightly": "web-ext run --start-url about:debugging --start-url about:addons --verbose --firefox=\"C:/Program Files/Nightly/firefox.exe\" -s=\".\"", + "lint": "web-ext lint && eslint src test", "lint:fix": "eslint --fix src test", "mockserve": "node test/mockserver", "prettier": "echo '💈 Listing files to format. Run yarn prettier:write to write.\n' && prettier --list-different src/**/*.js src/*.js test/**/*.js test/*.js", diff --git a/src/menu.js b/src/menu.js index db2a438..bfa70d6 100644 --- a/src/menu.js +++ b/src/menu.js @@ -299,9 +299,50 @@ const Menus = { downloadType = DOWNLOAD_TYPES.MEDIA; url = info.srcUrl; - if (hasLink && options.preferLinks) { - downloadType = DOWNLOAD_TYPES.LINK; - url = info.linkUrl; + if (hasLink) { + if (options.preferLinks) { + downloadType = DOWNLOAD_TYPES.LINK; + url = info.linkUrl; + + if (options.notifyOnLinkPreferred) { + Notification.createExtensionNotification( + browser.i18n.getMessage("notificationLinkPreferred"), + url + ); + } + } + + if (options.preferLinksFilterEnabled && options.preferLinksFilter) { + let overrideUrls = false; + try { + (options.preferLinksFilter || "") + .split("\n") + .map(s => s.trim()) + .map(s => new RegExp(s)) + .forEach(re => { + if (info.pageUrl.match(re) != null) { + overrideUrls = true; + } + }); + } catch (err) { + Notification.createExtensionNotification( + browser.i18n.getMessage("notificationBadPreferLinksPattern"), + err + ); + } + + if (overrideUrls) { + downloadType = DOWNLOAD_TYPES.LINK; + url = info.linkUrl; + + if (options.notifyOnLinkPreferred) { + Notification.createExtensionNotification( + browser.i18n.getMessage("notificationLinkPreferred"), + url + ); + } + } + } } } else if (hasLink) { downloadType = DOWNLOAD_TYPES.LINK; diff --git a/src/option.js b/src/option.js index 84de38c..3cbd4a5 100644 --- a/src/option.js +++ b/src/option.js @@ -32,10 +32,17 @@ const OptionsManagement = { { name: "keyRoot", type: T.VALUE, default: "e" }, { name: "links", type: T.BOOL, default: true }, { name: "preferLinks", type: T.BOOL, default: false }, + { name: "preferLinksFilterEnabled", type: T.BOOL, default: false }, + { + name: "preferLinksFilter", + type: T.VALUE, + default: ".*commons.wikimedia.org/wiki/File:.*" + }, { name: "notifyDuration", type: T.VALUE, default: 7000 }, { name: "notifyOnFailure", type: T.BOOL, default: true }, { name: "notifyOnRuleMatch", type: T.BOOL, default: true }, { name: "notifyOnSuccess", type: T.BOOL, default: false }, + { name: "notifyOnLinkPreferred", type: T.BOOL, default: true }, { name: "page", type: T.BOOL, default: true }, { name: "paths", diff --git a/src/options/options.html b/src/options/options.html index c8523b2..e3a7137 100644 --- a/src/options/options.html +++ b/src/options/options.html @@ -1,51 +1,69 @@ + + + + + + Settings — Save In + + + + +
+ __MSG_o_lPopout__ + · + __MSG_o_lVariableList__ + · + __MSG_o_lClauseList__ + · + __MSG_o_lGithub__ + · + __MSG_o_lChangelog__ +
- - - - - - Settings — Save In - - - - -
- __MSG_o_lPopout__ - · - __MSG_o_lVariableList__ - · - __MSG_o_lClauseList__ - · - __MSG_o_lGithub__ - · - __MSG_o_lChangelog__ - -
- -
-

__MSG_o_sDownloads__

- -