diff options
Diffstat (limited to 'src/content')
-rw-r--r-- | src/content/actions/addon.js | 22 | ||||
-rw-r--r-- | src/content/actions/index.js | 4 | ||||
-rw-r--r-- | src/content/actions/operation.js | 62 | ||||
-rw-r--r-- | src/content/actions/setting.js | 10 | ||||
-rw-r--r-- | src/content/components/common/index.js | 29 | ||||
-rw-r--r-- | src/content/components/common/keymapper.js | 7 | ||||
-rw-r--r-- | src/content/components/top-content/index.js | 29 | ||||
-rw-r--r-- | src/content/index.js | 8 | ||||
-rw-r--r-- | src/content/reducers/addon.js | 10 | ||||
-rw-r--r-- | src/content/reducers/index.js | 33 |
10 files changed, 97 insertions, 117 deletions
diff --git a/src/content/actions/addon.js b/src/content/actions/addon.js index 8d38025..b30cf16 100644 --- a/src/content/actions/addon.js +++ b/src/content/actions/addon.js @@ -1,15 +1,19 @@ +import messages from 'shared/messages'; import actions from 'content/actions'; -const enable = () => { - return { type: actions.ADDON_ENABLE }; -}; +const enable = () => setEnabled(true); -const disable = () => { - return { type: actions.ADDON_DISABLE }; -}; +const disable = () => setEnabled(false); -const toggleEnabled = () => { - return { type: actions.ADDON_TOGGLE_ENABLED }; +const setEnabled = async(enabled) => { + await browser.runtime.sendMessage({ + type: messages.ADDON_ENABLED_RESPONSE, + enabled, + }); + return { + type: actions.ADDON_SET_ENABLED, + enabled, + }; }; -export { enable, disable, toggleEnabled }; +export { enable, disable, setEnabled }; diff --git a/src/content/actions/index.js b/src/content/actions/index.js index 7e32e12..1c51ab0 100644 --- a/src/content/actions/index.js +++ b/src/content/actions/index.js @@ -1,8 +1,6 @@ export default { // Enable/disable - ADDON_ENABLE: 'addon.enable', - ADDON_DISABLE: 'addon.disable', - ADDON_TOGGLE_ENABLED: 'addon.toggle.enabled', + ADDON_SET_ENABLED: 'addon.set.enabled', // Settings SETTING_SET: 'setting.set', diff --git a/src/content/actions/operation.js b/src/content/actions/operation.js index 40ac52d..c1bd1c8 100644 --- a/src/content/actions/operation.js +++ b/src/content/actions/operation.js @@ -9,7 +9,7 @@ import * as addonActions from './addon'; import * as properties from 'shared/settings/properties'; // eslint-disable-next-line complexity, max-lines-per-function -const exec = (operation, repeat, settings) => { +const exec = (operation, repeat, settings, addonEnabled) => { let smoothscroll = settings.properties.smoothscroll || properties.defaults.smoothscroll; switch (operation.type) { @@ -18,60 +18,80 @@ const exec = (operation, repeat, settings) => { case operations.ADDON_DISABLE: return addonActions.disable(); case operations.ADDON_TOGGLE_ENABLED: - return addonActions.toggleEnabled(); + return addonActions.setEnabled(!addonEnabled); case operations.FIND_NEXT: - return window.top.postMessage(JSON.stringify({ + window.top.postMessage(JSON.stringify({ type: messages.FIND_NEXT, }), '*'); + break; case operations.FIND_PREV: - return window.top.postMessage(JSON.stringify({ + window.top.postMessage(JSON.stringify({ type: messages.FIND_PREV, }), '*'); + break; case operations.SCROLL_VERTICALLY: - return scrolls.scrollVertically(operation.count, smoothscroll, repeat); + scrolls.scrollVertically(operation.count, smoothscroll, repeat); + break; case operations.SCROLL_HORIZONALLY: - return scrolls.scrollHorizonally(operation.count, smoothscroll, repeat); + scrolls.scrollHorizonally(operation.count, smoothscroll, repeat); + break; case operations.SCROLL_PAGES: - return scrolls.scrollPages(operation.count, smoothscroll, repeat); + scrolls.scrollPages(operation.count, smoothscroll, repeat); + break; case operations.SCROLL_TOP: - return scrolls.scrollTop(smoothscroll, repeat); + scrolls.scrollTop(smoothscroll, repeat); + break; case operations.SCROLL_BOTTOM: - return scrolls.scrollBottom(smoothscroll, repeat); + scrolls.scrollBottom(smoothscroll, repeat); + break; case operations.SCROLL_HOME: - return scrolls.scrollHome(smoothscroll, repeat); + scrolls.scrollHome(smoothscroll, repeat); + break; case operations.SCROLL_END: - return scrolls.scrollEnd(smoothscroll, repeat); + scrolls.scrollEnd(smoothscroll, repeat); + break; case operations.FOLLOW_START: - return window.top.postMessage(JSON.stringify({ + window.top.postMessage(JSON.stringify({ type: messages.FOLLOW_START, newTab: operation.newTab, background: operation.background, }), '*'); + break; case operations.NAVIGATE_HISTORY_PREV: - return navigates.historyPrev(window); + navigates.historyPrev(window); + break; case operations.NAVIGATE_HISTORY_NEXT: - return navigates.historyNext(window); + navigates.historyNext(window); + break; case operations.NAVIGATE_LINK_PREV: - return navigates.linkPrev(window); + navigates.linkPrev(window); + break; case operations.NAVIGATE_LINK_NEXT: - return navigates.linkNext(window); + navigates.linkNext(window); + break; case operations.NAVIGATE_PARENT: - return navigates.parent(window); + navigates.parent(window); + break; case operations.NAVIGATE_ROOT: - return navigates.root(window); + navigates.root(window); + break; case operations.FOCUS_INPUT: - return focuses.focusInput(); + focuses.focusInput(); + break; case operations.URLS_YANK: urls.yank(window); - return consoleFrames.postInfo(window.document, 'Current url yanked'); + consoleFrames.postInfo(window.document, 'Current url yanked'); + break; case operations.URLS_PASTE: - return urls.paste(window, operation.newTab ? operation.newTab : false); + urls.paste(window, operation.newTab ? operation.newTab : false); + break; default: browser.runtime.sendMessage({ type: messages.BACKGROUND_OPERATION, operation, }); } + return { type: '' }; }; export { exec }; diff --git a/src/content/actions/setting.js b/src/content/actions/setting.js index e34b6e0..1c15dd7 100644 --- a/src/content/actions/setting.js +++ b/src/content/actions/setting.js @@ -1,6 +1,7 @@ import actions from 'content/actions'; import * as keyUtils from 'shared/utils/keys'; import operations from 'shared/operations'; +import messages from 'shared/messages'; const reservedKeymaps = { '<Esc>': { type: operations.CANCEL }, @@ -26,4 +27,11 @@ const set = (value) => { }; }; -export { set }; +const load = async() => { + let settings = await browser.runtime.sendMessage({ + type: messages.SETTINGS_QUERY, + }); + return set(settings); +}; + +export { set, load }; diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index 6437011..a1e71a1 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -4,6 +4,7 @@ import FollowComponent from './follow'; 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) { @@ -14,42 +15,34 @@ export default class Common { input.onKey(key => follow.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)); - store.subscribe(() => this.update()); } onMessage(message) { + let { enabled } = this.store.getState().addon; switch (message.type) { case messages.SETTINGS_CHANGED: return this.reloadSettings(); case messages.ADDON_TOGGLE_ENABLED: - return this.store.dispatch(addonActions.toggleEnabled()); + this.store.dispatch(addonActions.setEnabled(!enabled)); } } - update() { - let enabled = this.store.getState().addon.enabled; - if (enabled !== this.prevEnabled) { - this.prevEnabled = enabled; - - browser.runtime.sendMessage({ - type: messages.ADDON_ENABLED_RESPONSE, - enabled, - }); - } - } - - async reloadSettings() { + reloadSettings() { try { - let settings = await browser.runtime.sendMessage({ - type: messages.SETTINGS_QUERY, + this.store.dispatch(settingActions.load()).then(({ value: settings }) => { + let enabled = !blacklists.includes( + settings.blacklist, this.win.location.href + ); + this.store.dispatch(addonActions.setEnabled(enabled)); }); - this.store.dispatch(settingActions.set(settings)); } catch (e) { // Sometime sendMessage fails when background script is not ready. console.warn(e); diff --git a/src/content/components/common/keymapper.js b/src/content/components/common/keymapper.js index d9d1b2f..4c294b4 100644 --- a/src/content/components/common/keymapper.js +++ b/src/content/components/common/keymapper.js @@ -20,6 +20,7 @@ export default class KeymapperComponent { this.store = store; } + // eslint-disable-next-line max-statements key(key) { this.store.dispatch(inputActions.keyPress(key)); @@ -47,8 +48,10 @@ export default class KeymapperComponent { return true; } let operation = keymaps.get(matched[0]); - this.store.dispatch(operationActions.exec( - operation, key.repeat, state.setting)); + let act = operationActions.exec( + operation, key.repeat, state.setting, state.addon.enabled + ); + this.store.dispatch(act); this.store.dispatch(inputActions.clearKeys()); return true; } diff --git a/src/content/components/top-content/index.js b/src/content/components/top-content/index.js index a0d0480..e22e957 100644 --- a/src/content/components/top-content/index.js +++ b/src/content/components/top-content/index.js @@ -2,16 +2,13 @@ import CommonComponent from '../common'; import FollowController from './follow-controller'; import FindComponent from './find'; import * as consoleFrames from '../../console-frames'; -import * as addonActions from '../../actions/addon'; import messages from 'shared/messages'; -import * as re from 'shared/utils/re'; export default class TopContent { constructor(win, store) { this.win = win; this.store = store; - this.prevBlacklist = undefined; new CommonComponent(win, store); // eslint-disable-line no-new new FollowController(win, store); // eslint-disable-line no-new @@ -21,32 +18,6 @@ export default class TopContent { consoleFrames.initialize(this.win.document); messages.onMessage(this.onMessage.bind(this)); - - this.store.subscribe(() => this.update()); - } - - update() { - let blacklist = this.store.getState().setting.blacklist; - if (JSON.stringify(this.prevBlacklist) !== JSON.stringify(blacklist)) { - this.disableIfBlack(blacklist); - this.prevBlacklist = blacklist; - } - } - - disableIfBlack(blacklist) { - let loc = this.win.location; - let partial = loc.host + loc.pathname; - let matched = blacklist - .map((item) => { - let pattern = item.includes('/') ? item : item + '/*'; - return re.fromWildcard(pattern); - }) - .some(regex => regex.test(partial)); - if (matched) { - this.store.dispatch(addonActions.disable()); - } else { - this.store.dispatch(addonActions.enable()); - } } onMessage(message) { diff --git a/src/content/index.js b/src/content/index.js index 97a8b30..3b0b49b 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,10 +1,14 @@ import './console-frame.scss'; -import { createStore } from 'shared/store'; +import { createStore, applyMiddleware } from 'redux'; +import promise from 'redux-promise'; import reducers from 'content/reducers'; import TopContentComponent from './components/top-content'; import FrameContentComponent from './components/frame-content'; -const store = createStore(reducers); +const store = createStore( + reducers, + applyMiddleware(promise), +); if (window.self === window.top) { new TopContentComponent(window, store); // eslint-disable-line no-new diff --git a/src/content/reducers/addon.js b/src/content/reducers/addon.js index b881ca0..0def55a 100644 --- a/src/content/reducers/addon.js +++ b/src/content/reducers/addon.js @@ -6,15 +6,9 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { - case actions.ADDON_ENABLE: + case actions.ADDON_SET_ENABLED: return { ...state, - enabled: true, }; - case actions.ADDON_DISABLE: - return { ...state, - enabled: false, }; - case actions.ADDON_TOGGLE_ENABLED: - return { ...state, - enabled: !state.enabled, }; + enabled: action.enabled, }; default: return state; } diff --git a/src/content/reducers/index.js b/src/content/reducers/index.js index c3a474e..6e6a147 100644 --- a/src/content/reducers/index.js +++ b/src/content/reducers/index.js @@ -1,25 +1,10 @@ -import addonReducer from './addon'; -import findReducer from './find'; -import settingReducer from './setting'; -import inputReducer from './input'; -import followControllerReducer from './follow-controller'; +import { combineReducers } from 'redux'; +import addon from './addon'; +import find from './find'; +import setting from './setting'; +import input from './input'; +import followController from './follow-controller'; -// Make setting reducer instead of re-use -const defaultState = { - addon: addonReducer(undefined, {}), - find: findReducer(undefined, {}), - setting: settingReducer(undefined, {}), - input: inputReducer(undefined, {}), - followController: followControllerReducer(undefined, {}), -}; - -export default function reducer(state = defaultState, action = {}) { - return { - ...state, - addon: addonReducer(state.addon, action), - find: findReducer(state.find, action), - setting: settingReducer(state.setting, action), - input: inputReducer(state.input, action), - followController: followControllerReducer(state.followController, action), - }; -} +export default combineReducers({ + addon, find, setting, input, followController, +}); |