diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-04-30 14:00:07 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-02 11:14:19 +0900 |
commit | c60d0e7392fc708e961614d6b756a045de74f458 (patch) | |
tree | 0b9a5fce1879e38a92d5dbb2915779aee0ad22d6 /src/content/components/common/index.ts | |
parent | 257162e5b6b4993e1dff0d705ffa6f0d809033eb (diff) |
Rename .js/.jsx to .ts/.tsx
Diffstat (limited to 'src/content/components/common/index.ts')
-rw-r--r-- | src/content/components/common/index.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/content/components/common/index.ts b/src/content/components/common/index.ts new file mode 100644 index 0000000..bcab4fa --- /dev/null +++ b/src/content/components/common/index.ts @@ -0,0 +1,55 @@ +import InputComponent from './input'; +import FollowComponent from './follow'; +import MarkComponent from './mark'; +import KeymapperComponent from './keymapper'; +import * as settingActions from 'content/actions/setting'; +import messages from 'shared/messages'; +import * as addonActions from '../../actions/addon'; +import * as blacklists from 'shared/blacklists'; + +export default class Common { + constructor(win, store) { + const input = new InputComponent(win.document.body, store); + const follow = new FollowComponent(win, store); + const mark = new MarkComponent(win.document.body, store); + const keymapper = new KeymapperComponent(store); + + input.onKey(key => follow.key(key)); + input.onKey(key => mark.key(key)); + input.onKey(key => keymapper.key(key)); + + this.win = win; + this.store = store; + this.prevEnabled = undefined; + this.prevBlacklist = undefined; + + this.reloadSettings(); + + messages.onMessage(this.onMessage.bind(this)); + } + + onMessage(message) { + let { enabled } = this.store.getState().addon; + switch (message.type) { + case messages.SETTINGS_CHANGED: + return this.reloadSettings(); + case messages.ADDON_TOGGLE_ENABLED: + this.store.dispatch(addonActions.setEnabled(!enabled)); + } + } + + reloadSettings() { + try { + this.store.dispatch(settingActions.load()).then(({ value: settings }) => { + let enabled = !blacklists.includes( + settings.blacklist, this.win.location.href + ); + this.store.dispatch(addonActions.setEnabled(enabled)); + }); + } catch (e) { + // Sometime sendMessage fails when background script is not ready. + console.warn(e); + setTimeout(() => this.reloadSettings(), 500); + } + } +} |