aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings
diff options
context:
space:
mode:
authorShin'ya UEOKA <ueokande@i-beam.org>2019-10-05 06:39:20 +0000
committerShin'ya UEOKA <ueokande@i-beam.org>2019-10-06 12:58:59 +0000
commitb86b4680b6c2a3f0cceefaaf7c2e35417ec555df (patch)
tree79296d48f79ebba2496de5e8e09a4c8fc9e1ad41 /src/shared/settings
parent574692551a27ea56660bf2061daeaa0d34beaff4 (diff)
Make Blacklist class
Diffstat (limited to 'src/shared/settings')
-rw-r--r--src/shared/settings/Blacklist.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/shared/settings/Blacklist.ts b/src/shared/settings/Blacklist.ts
new file mode 100644
index 0000000..a95b606
--- /dev/null
+++ b/src/shared/settings/Blacklist.ts
@@ -0,0 +1,39 @@
+export type BlacklistJSON = string[];
+
+const fromWildcard = (pattern: string): RegExp => {
+ let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$';
+ return new RegExp(regexStr);
+};
+
+export default class Blacklist {
+ constructor(
+ private blacklist: string[],
+ ) {
+ }
+
+ static fromJSON(json: any): Blacklist {
+ if (!Array.isArray(json)) {
+ throw new TypeError(`"blacklist" is not an array of string`);
+ }
+ for (let x of json) {
+ if (typeof x !== 'string') {
+ throw new TypeError(`"blacklist" is not an array of string`);
+ }
+ }
+ return new Blacklist(json);
+ }
+
+ toJSON(): BlacklistJSON {
+ return this.blacklist;
+ }
+
+ includes(url: string): boolean {
+ let u = new URL(url);
+ return this.blacklist.some((item) => {
+ if (!item.includes('/')) {
+ return fromWildcard(item).test(u.host);
+ }
+ return fromWildcard(item).test(u.host + u.pathname);
+ });
+ }
+}