Skip to content

Commit

Permalink
Add host blacklist (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
qsniyg committed Dec 22, 2023
1 parent 7a5cc7d commit 73b39f6
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 110 deletions.
165 changes: 108 additions & 57 deletions src/userscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3100,6 +3100,10 @@ var $$IMU_EXPORT$$;
return true;
};

let host_filter:BigImageFilter = function(url:string):boolean {
return true;
};

if (is_interactive || is_extension_bg) {
bigimage_filter = function(url) {
for (var i = 0; i < blacklist_regexes.length; i++) {
Expand All @@ -3108,7 +3112,20 @@ var $$IMU_EXPORT$$;
}

return true;
}
};

host_filter = function(url) {
let 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;
};
}

type BigImageRuleSpecificOptions = {
Expand Down Expand Up @@ -13569,6 +13586,7 @@ var $$IMU_EXPORT$$;
mouseover_zoom_fit_key: ["2"],
mouseover_fullscreen_key: ["f"],
mouseover_apply_blacklist: true,
host_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
Expand Down Expand Up @@ -16350,8 +16368,8 @@ var $$IMU_EXPORT$$;
subcategory: "source"
},
apply_blacklist_host: {
name: "Apply blacklist for host websites",
description: "This option prevents the script from opening any popups to host websites that are in the blacklist. For example, adding `twitter.com` to the blacklist would prevent any popup from opening on twitter.com. If disabled, this option only applies to image URLs (such as twimg.com), not host URLs",
name: "Use media blacklist for host websites",
description: "Overrides the host blacklist to use the media blacklist (specified under Rules)",
category: "popup",
subcategory: "source"
},
Expand Down Expand Up @@ -16859,61 +16877,14 @@ var $$IMU_EXPORT$$;
onupdate: update_rule_setting
},
bigimage_blacklist: {
name: "Blacklist",
name: "Media blacklist",
description: "A list of URLs (one per line) that are blacklisted from being processed",
category: "rules",
type: "textarea",
onupdate: function() {
update_rule_setting();
create_blacklist_regexes();
},
onedit: function() {
var errors = create_blacklist_regexes();

var errordiv;

try {
errordiv = document.querySelector("#option_bigimage_blacklist .error");
errordiv.innerText = "";
errordiv.classList.add("hidden");
} catch (e) {
}

if (errors) {
for (var i = 0; i < errors.length; i++) {
if (errordiv) {
errordiv.innerText += errors[i].message + "\n";
errordiv.classList.remove("hidden");
}

console.error(errors[i]);
}
}
},
documentation: {
title: "Documentation",
value: [
"The examples below are written for the simple (glob) engine, not the regex engine. The glob engine is generally based on the UNIX glob syntax.<br />",
"<ul><br />",
"<li><code>google.com</code> will block https://google.com/, https://www.google.com/, https://abcdef.google.com/, https://def.abc.google.com/, etc.</li>",
"<li><code>abc.google.com</code> will block https://abc.google.com/, https://def.abc.google.com/, etc.</li>",
"<li><code>*.google.com</code> will block https://www.google.com/, https://def.abc.google.com/, etc. but not https://google.com/</li>",
"<li><code>google.*/</code> will block https://google.com/, https://www.google.co.uk, etc.</li>",
"<li><code>http://google.com</code> will block http://google.com/, but not https://google.com/, http://www.google.com/, etc.</li>",
"<li><code>google.com/test</code> will block https://google.com/test, https://www.google.com/test/abcdef, but not https://google.com/, etc.</li>",
"<li><code>google.com/*/test</code> will block https://google.com/abc/test, but not https://google.com/test or https://google.com/abc/def/test</li>",
"<li><code>google.com/**/test</code> will block https://google.com/abc/test, https://google.com/abc/def/test, https://google.com/abc/def/ghi/test, etc. but not https://google.com/test</li>",
"<li><code>g??gle.com</code> will block https://google.com/, https://gaagle.com/, https://goagle.com/, etc.</li>",
"<li><code>google.{com,co.uk}</code> will block https://google.com/ and https://google.co.uk/</li>",
"<li><code>g[oau]ogle.com</code> will block https://google.com/, https://gaogle.com/, and http://www.guogle.com/</li>",
"<li><code>g[0-9]ogle.com</code> will block https://g0ogle.com/, https://g1ogle.com/, etc. (up to https://g9ogle.com/)</li>",
"</ul>"
].join("\n")
}
type: "textarea"
},
bigimage_blacklist_engine: {
name: "Blacklist engine",
description: "How the blacklist should be processed",
name: "Media blacklist engine",
description: "How the media blacklist should be processed",
category: "rules",
options: {
glob: {
Expand All @@ -16924,6 +16895,16 @@ var $$IMU_EXPORT$$;
}
}
},
host_blacklist: {
name: "Host blacklist",
description: "A list of host URLs (one per line) that the popup is blacklisted from. For example, adding `twitter.com` to this blacklist would prevent any popup from opening on twitter.com.",
type: "textarea",
requires: {
mouseover: true
},
category: "popup",
subcategory: "source"
},
filename_format: {
name: "Filename format",
description: "Format string(s) for the filename",
Expand Down Expand Up @@ -17433,6 +17414,66 @@ var $$IMU_EXPORT$$;
}
})();

let blacklist_settings = {
bigimage_blacklist: {
create_regexes() {return create_blacklist_regexes()}
},
host_blacklist: {
create_regexes() {return create_host_blacklist_regexes()}
}
};
for (let bl in blacklist_settings) {
settings_meta[bl].onupdate = function() {
update_rule_setting();
blacklist_settings[bl].create_regexes();
};

settings_meta[bl].onedit = function() {
let errors = blacklist_settings[bl].create_regexes();

let errordiv = null;

try {
errordiv = document.querySelector("#option_" + bl + " .error");
errordiv.innerText = "";
errordiv.classList.add("hidden");
} catch (e) {
}

if (errors) {
for (var i = 0; i < errors.length; i++) {
if (errordiv) {
errordiv.innerText += errors[i].message + "\n";
errordiv.classList.remove("hidden");
}

console.error(errors[i]);
}
}
};

settings_meta[bl].documentation = {
title: "Documentation",
value: [
"The examples below are written for the simple (glob) engine, not the regex engine. The glob engine is generally based on the UNIX glob syntax.<br />",
"<ul><br />",
"<li><code>google.com</code> will block https://google.com/, https://www.google.com/, https://abcdef.google.com/, https://def.abc.google.com/, etc.</li>",
"<li><code>abc.google.com</code> will block https://abc.google.com/, https://def.abc.google.com/, etc.</li>",
"<li><code>*.google.com</code> will block https://www.google.com/, https://def.abc.google.com/, etc. but not https://google.com/</li>",
"<li><code>google.*/</code> will block https://google.com/, https://www.google.co.uk, etc.</li>",
"<li><code>http://google.com</code> will block http://google.com/, but not https://google.com/, http://www.google.com/, etc.</li>",
"<li><code>google.com/test</code> will block https://google.com/test, https://www.google.com/test/abcdef, but not https://google.com/, etc.</li>",
"<li><code>google.com/*/test</code> will block https://google.com/abc/test, but not https://google.com/test or https://google.com/abc/def/test</li>",
"<li><code>google.com/**/test</code> will block https://google.com/abc/test, https://google.com/abc/def/test, https://google.com/abc/def/ghi/test, etc. but not https://google.com/test</li>",
"<li><code>g??gle.com</code> will block https://google.com/, https://gaagle.com/, https://goagle.com/, etc.</li>",
"<li><code>google.{com,co.uk}</code> will block https://google.com/ and https://google.co.uk/</li>",
"<li><code>g[oau]ogle.com</code> will block https://google.com/, https://gaogle.com/, and http://www.guogle.com/</li>",
"<li><code>g[0-9]ogle.com</code> will block https://g0ogle.com/, https://g1ogle.com/, etc. (up to https://g9ogle.com/)</li>",
"</ul>"
].join("\n")
};
}

var orig_settings = deepcopy(settings);

// imu:begin_exclude
Expand Down Expand Up @@ -19065,6 +19106,7 @@ var $$IMU_EXPORT$$;
}

var blacklist_regexes = [];
var host_blacklist_regexes = [];

function update_rule_setting() {
url_cache.clear();
Expand Down Expand Up @@ -19170,12 +19212,21 @@ var $$IMU_EXPORT$$;
try {
blacklist_regexes = parse_blacklist_regexes(settings.bigimage_blacklist, settings.bigimage_blacklist_engine);
} catch (e) {
return [e]
return [e];
}

//console_log(blacklist_regexes);
}

function create_host_blacklist_regexes() {
host_blacklist_regexes = [];
try {
host_blacklist_regexes = parse_blacklist_regexes(settings.host_blacklist, settings.bigimage_blacklist_engine);
} catch (e) {
return [e];
}
}

var parse_headers = function(headerstr) {
var headers = [];

Expand Down Expand Up @@ -118604,7 +118655,7 @@ var $$IMU_EXPORT$$;
var mouseover_base_enabled = function() {
if (!settings.imu_enabled) return false;

if (settings.apply_blacklist_host && !bigimage_filter(window.location.href))
if (!host_filter(window.location.href))
return false;

return true;
Expand Down Expand Up @@ -127501,7 +127552,7 @@ var $$IMU_EXPORT$$;
return;

// TODO: allow this to be automatically updated
if (settings.apply_blacklist_host && !bigimage_filter(window.location.href))
if (!host_filter(window.location.href))
return;

var observer;
Expand Down
Loading

0 comments on commit 73b39f6

Please sign in to comment.