Skip to content

Commit

Permalink
Add option to always prefer links when downloading from pages that ma…
Browse files Browse the repository at this point in the history
…tch a regex list of URLs

#100
  • Loading branch information
gyng committed Feb 2, 2019
1 parent 60eb38c commit b392c0e
Show file tree
Hide file tree
Showing 8 changed files with 729 additions and 445 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
81 changes: 44 additions & 37 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
},

Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand All @@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
47 changes: 44 additions & 3 deletions src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit b392c0e

Please sign in to comment.