aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent574692551a27ea56660bf2061daeaa0d34beaff4 (diff)
Make Blacklist class
Diffstat (limited to 'src')
-rw-r--r--src/content/controllers/SettingController.ts6
-rw-r--r--src/settings/components/index.tsx4
-rw-r--r--src/shared/SettingData.ts13
-rw-r--r--src/shared/Settings.ts21
-rw-r--r--src/shared/blacklists.ts13
-rw-r--r--src/shared/settings/Blacklist.ts39
-rw-r--r--src/shared/utils/re.ts6
7 files changed, 54 insertions, 48 deletions
diff --git a/src/content/controllers/SettingController.ts b/src/content/controllers/SettingController.ts
index 7fb045b..06273a0 100644
--- a/src/content/controllers/SettingController.ts
+++ b/src/content/controllers/SettingController.ts
@@ -1,8 +1,6 @@
import { injectable } from 'tsyringe';
import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase';
import SettingUseCase from '../usecases/SettingUseCase';
-import * as blacklists from '../../shared/blacklists';
-
import * as messages from '../../shared/messages';
@injectable()
@@ -17,9 +15,7 @@ export default class SettingController {
async initSettings(): Promise<void> {
try {
let current = await this.settingUseCase.reload();
- let disabled = blacklists.includes(
- current.blacklist, window.location.href,
- );
+ let disabled = current.blacklist.includes(window.location.href);
if (disabled) {
this.addonEnabledUseCase.disable();
} else {
diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx
index e86d765..bd99d52 100644
--- a/src/settings/components/index.tsx
+++ b/src/settings/components/index.tsx
@@ -11,8 +11,8 @@ import SettingData, {
FormKeymaps, FormSearch, FormSettings, JSONTextSettings,
} from '../../shared/SettingData';
import { State as AppState } from '../reducers/setting';
-import * as settings from '../../shared/Settings';
import Properties from '../../shared/settings/Properties';
+import Blacklist from '../../shared/settings/Blacklist';
const DO_YOU_WANT_TO_CONTINUE =
'Some settings in JSON can be lost when migrating. ' +
@@ -143,7 +143,7 @@ class SettingsComponent extends React.Component<Props> {
let data = new SettingData({
source: this.props.source,
form: (this.props.form as FormSettings).buildWithBlacklist(
- settings.blacklistValueOf(value)),
+ Blacklist.fromJSON(value)),
});
this.props.dispatch(settingActions.set(data));
}
diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts
index aa4e382..8ef8385 100644
--- a/src/shared/SettingData.ts
+++ b/src/shared/SettingData.ts
@@ -3,6 +3,7 @@ import Settings, * as settings from './Settings';
import Keymaps from './settings/Keymaps';
import Search from './settings/Search';
import Properties from './settings/Properties';
+import Blacklist from './settings/Blacklist';
export class FormKeymaps {
private data: {[op: string]: string};
@@ -146,13 +147,13 @@ export class FormSettings {
private properties: Properties;
- private blacklist: string[];
+ private blacklist: Blacklist;
constructor(
keymaps: FormKeymaps,
search: FormSearch,
properties: Properties,
- blacklist: string[],
+ blacklist: Blacklist,
) {
this.keymaps = keymaps;
this.search = search;
@@ -187,7 +188,7 @@ export class FormSettings {
);
}
- buildWithBlacklist(blacklist: string[]): FormSettings {
+ buildWithBlacklist(blacklist: Blacklist): FormSettings {
return new FormSettings(
this.keymaps,
this.search,
@@ -201,7 +202,7 @@ export class FormSettings {
keymaps: this.keymaps.toKeymaps().toJSON(),
search: this.search.toSearchSettings().toJSON(),
properties: this.properties.toJSON(),
- blacklist: this.blacklist,
+ blacklist: this.blacklist.toJSON(),
});
}
@@ -215,7 +216,7 @@ export class FormSettings {
keymaps: this.keymaps.toJSON(),
search: this.search.toJSON(),
properties: this.properties.toJSON(),
- blacklist: this.blacklist,
+ blacklist: this.blacklist.toJSON(),
};
}
@@ -229,7 +230,7 @@ export class FormSettings {
FormKeymaps.valueOf(o.keymaps),
FormSearch.valueOf(o.search),
Properties.fromJSON(o.properties),
- settings.blacklistValueOf(o.blacklist),
+ Blacklist.fromJSON(o.blacklist),
);
}
diff --git a/src/shared/Settings.ts b/src/shared/Settings.ts
index 6250aae..2767820 100644
--- a/src/shared/Settings.ts
+++ b/src/shared/Settings.ts
@@ -1,26 +1,15 @@
import Keymaps from './settings/Keymaps';
import Search from './settings/Search';
import Properties from './settings/Properties';
+import Blacklist from './settings/Blacklist';
export default interface Settings {
keymaps: Keymaps;
search: Search;
properties: Properties;
- blacklist: string[];
+ blacklist: Blacklist;
}
-export const blacklistValueOf = (o: any): string[] => {
- if (!Array.isArray(o)) {
- throw new TypeError(`"blacklist" is not an array of string`);
- }
- for (let x of o) {
- if (typeof x !== 'string') {
- throw new TypeError(`"blacklist" is not an array of string`);
- }
- }
- return o as string[];
-};
-
export const valueOf = (o: any): Settings => {
let settings = { ...DefaultSetting };
for (let key of Object.keys(o)) {
@@ -35,7 +24,7 @@ export const valueOf = (o: any): Settings => {
settings.properties = Properties.fromJSON(o.properties);
break;
case 'blacklist':
- settings.blacklist = blacklistValueOf(o.blacklist);
+ settings.blacklist = Blacklist.fromJSON(o.blacklist);
break;
default:
throw new TypeError('unknown setting: ' + key);
@@ -49,7 +38,7 @@ export const toJSON = (settings: Settings): any => {
keymaps: settings.keymaps.toJSON(),
search: settings.search.toJSON(),
properties: settings.properties.toJSON(),
- blacklist: settings.blacklist,
+ blacklist: settings.blacklist.toJSON(),
};
};
@@ -134,5 +123,5 @@ export const DefaultSetting: Settings = {
smoothscroll: false,
complete: 'sbh'
}),
- blacklist: []
+ blacklist: Blacklist.fromJSON([]),
};
diff --git a/src/shared/blacklists.ts b/src/shared/blacklists.ts
deleted file mode 100644
index 61ee4de..0000000
--- a/src/shared/blacklists.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import * as re from './utils/re';
-
-const includes = (blacklist: string[], url: string): boolean => {
- let u = new URL(url);
- return blacklist.some((item) => {
- if (!item.includes('/')) {
- return re.fromWildcard(item).test(u.host);
- }
- return re.fromWildcard(item).test(u.host + u.pathname);
- });
-};
-
-export { includes };
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);
+ });
+ }
+}
diff --git a/src/shared/utils/re.ts b/src/shared/utils/re.ts
deleted file mode 100644
index 34f4fa6..0000000
--- a/src/shared/utils/re.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-const fromWildcard = (pattern: string): RegExp => {
- let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$';
- return new RegExp(regexStr);
-};
-
-export { fromWildcard };