From b80d0a973afacc22c16295f2ee371f0e24faf423 Mon Sep 17 00:00:00 2001 From: qsniyg Date: Fri, 22 Dec 2023 20:04:45 +0000 Subject: [PATCH] Add options to turn blacklists into whitelists (fixes #302) --- src/userscript.ts | 60 ++++++++++++++++++++++++++++++++++++---------- userscript.user.js | 55 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 93 insertions(+), 22 deletions(-) diff --git a/src/userscript.ts b/src/userscript.ts index 04de2849..56c566b6 100644 --- a/src/userscript.ts +++ b/src/userscript.ts @@ -3096,7 +3096,7 @@ var $$IMU_EXPORT$$; type BigImageFilter = (url:string) => boolean; - var bigimage_filter:BigImageFilter = function(url:string) { + var bigimage_filter:BigImageFilter = function(url:string):boolean { return true; }; @@ -3105,13 +3105,25 @@ var $$IMU_EXPORT$$; }; if (is_interactive || is_extension_bg) { - bigimage_filter = function(url) { - for (var i = 0; i < blacklist_regexes.length; i++) { - if (blacklist_regexes[i].test(url)) - return false; + let blackwhitelist_filter = function(url:string, regexes, option) { + let result = true; + + for (var i = 0; i < regexes.length; i++) { + if (regexes[i].test(url)) { + result = false; + break; + } } - return true; + if (option === "whitelist") { + return !result; + } else { + return result; + } + }; + + bigimage_filter = function(url) { + return blackwhitelist_filter(url, blacklist_regexes, settings.bigimage_blacklist_mode); }; host_filter = function(url) { @@ -3119,12 +3131,7 @@ var $$IMU_EXPORT$$; if (settings.apply_blacklist_host) regexes = blacklist_regexes; - for (var i = 0; i < regexes.length; i++) { - if (regexes[i].test(url)) - return false; - } - - return true; + return blackwhitelist_filter(url, regexes, settings.host_blacklist_mode); }; } @@ -13587,6 +13594,7 @@ var $$IMU_EXPORT$$; mouseover_fullscreen_key: ["f"], mouseover_apply_blacklist: true, host_blacklist: "", + host_blacklist_mode: "blacklist", apply_blacklist_host: false, mouseover_matching_media_types: false, // thanks to ComedicFox on discord for the idea: https://github.com/qsniyg/maxurl/issues/811 @@ -13639,6 +13647,7 @@ var $$IMU_EXPORT$$; twitter_use_ext: false, // thanks to LukasThyWalls on github for the idea: https://github.com/qsniyg/maxurl/issues/75 bigimage_blacklist: "", + bigimage_blacklist_mode: "blacklist", bigimage_blacklist_engine: "glob", filename_format: "{author_username} {filename}", filename_replace_special_underscores: true, @@ -16882,6 +16891,19 @@ var $$IMU_EXPORT$$; category: "rules", type: "textarea" }, + bigimage_blacklist_mode: { + name: "Media blacklist mode", + description: "Whether the media blacklist should act as a blacklist or a whitelist", + category: "rules", + options: { + whitelist: { + name: "Whitelist" + }, + blacklist: { + name: "Blacklist" + } + } + }, bigimage_blacklist_engine: { name: "Blacklist engine", description: "How blacklists should be processed", @@ -16906,6 +16928,20 @@ var $$IMU_EXPORT$$; category: "popup", subcategory: "source" }, + host_blacklist_mode: { + name: "Host blacklist mode", + description: "Whether the host blacklist should act as a blacklist or a whitelist", + category: "popup", + subcategory: "source", + options: { + whitelist: { + name: "Whitelist" + }, + blacklist: { + name: "Blacklist" + } + } + }, filename_format: { name: "Filename format", description: "Format string(s) for the filename", diff --git a/userscript.user.js b/userscript.user.js index c9dc3082..74517d16 100644 --- a/userscript.user.js +++ b/userscript.user.js @@ -2546,22 +2546,28 @@ var $$IMU_EXPORT$$; return true; }; if (is_interactive || is_extension_bg) { - bigimage_filter = function(url) { - for (var i = 0; i < blacklist_regexes.length; i++) { - if (blacklist_regexes[i].test(url)) - return false; + var blackwhitelist_filter_1 = function(url, regexes, option) { + var result = true; + for (var i = 0; i < regexes.length; i++) { + if (regexes[i].test(url)) { + result = false; + break; + } } - return true; + if (option === "whitelist") { + return !result; + } else { + return result; + } + }; + bigimage_filter = function(url) { + return blackwhitelist_filter_1(url, blacklist_regexes, settings.bigimage_blacklist_mode); }; host_filter = function(url) { var regexes = host_blacklist_regexes; if (settings.apply_blacklist_host) regexes = blacklist_regexes; - for (var i = 0; i < regexes.length; i++) { - if (regexes[i].test(url)) - return false; - } - return true; + return blackwhitelist_filter_1(url, regexes, settings.host_blacklist_mode); }; } var default_options = { @@ -12843,6 +12849,7 @@ var $$IMU_EXPORT$$; mouseover_fullscreen_key: ["f"], mouseover_apply_blacklist: true, host_blacklist: "", + host_blacklist_mode: "blacklist", apply_blacklist_host: false, mouseover_matching_media_types: false, // thanks to ComedicFox on discord for the idea: https://github.com/qsniyg/maxurl/issues/811 @@ -12895,6 +12902,7 @@ var $$IMU_EXPORT$$; twitter_use_ext: false, // thanks to LukasThyWalls on github for the idea: https://github.com/qsniyg/maxurl/issues/75 bigimage_blacklist: "", + bigimage_blacklist_mode: "blacklist", bigimage_blacklist_engine: "glob", filename_format: "{author_username} {filename}", filename_replace_special_underscores: true, @@ -16097,6 +16105,19 @@ var $$IMU_EXPORT$$; category: "rules", type: "textarea" }, + bigimage_blacklist_mode: { + name: "Media blacklist mode", + description: "Whether the media blacklist should act as a blacklist or a whitelist", + category: "rules", + options: { + whitelist: { + name: "Whitelist" + }, + blacklist: { + name: "Blacklist" + } + } + }, bigimage_blacklist_engine: { name: "Blacklist engine", description: "How blacklists should be processed", @@ -16121,6 +16142,20 @@ var $$IMU_EXPORT$$; category: "popup", subcategory: "source" }, + host_blacklist_mode: { + name: "Host blacklist mode", + description: "Whether the host blacklist should act as a blacklist or a whitelist", + category: "popup", + subcategory: "source", + options: { + whitelist: { + name: "Whitelist" + }, + blacklist: { + name: "Blacklist" + } + } + }, filename_format: { name: "Filename format", description: "Format string(s) for the filename",