You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

39 lines
933 B

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);
});
}
}