From cf0dcf25226611590d76d263b5a4acdc89510d09 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 7 Jul 2018 17:52:01 +0900 Subject: blacklist as an util --- src/shared/blacklists.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/shared/blacklists.js (limited to 'src/shared') diff --git a/src/shared/blacklists.js b/src/shared/blacklists.js new file mode 100644 index 0000000..21e3e14 --- /dev/null +++ b/src/shared/blacklists.js @@ -0,0 +1,13 @@ +import * as re from 'shared/utils/re'; + +const includes = (blacklist, url) => { + let u = new URL(url) + return blacklist.some((item) => { + if (!item.includes('/')) { + return re.fromWildcard(item).test(u.hostname); + } + return re.fromWildcard(item).test(u.hostname + u.pathname); + }); +} + +export { includes }; -- cgit v1.2.3 From efa1cb396733ac0bedb7c1e86fd1974fbc801135 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 7 Jul 2018 22:05:22 +0900 Subject: Use official redux on content --- src/content/actions/addon.js | 22 +++++++++++-------- src/content/actions/index.js | 4 +--- src/content/actions/operation.js | 4 ++-- src/content/actions/setting.js | 2 +- src/content/components/common/index.js | 35 +++++++----------------------- src/content/components/common/keymapper.js | 4 +++- src/content/index.js | 1 - src/content/reducers/addon.js | 10 ++------- src/shared/blacklists.js | 4 ++-- test/content/actions/addon.test.js | 25 --------------------- test/content/reducers/addon.test.js | 24 ++------------------ 11 files changed, 34 insertions(+), 101 deletions(-) delete mode 100644 test/content/actions/addon.test.js (limited to 'src/shared') 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 a1761fc..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,7 +18,7 @@ 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: window.top.postMessage(JSON.stringify({ type: messages.FIND_NEXT, diff --git a/src/content/actions/setting.js b/src/content/actions/setting.js index 50c09ca..1c15dd7 100644 --- a/src/content/actions/setting.js +++ b/src/content/actions/setting.js @@ -34,4 +34,4 @@ const load = async() => { return set(settings); }; -export { load }; +export { set, load }; diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index e81bf1f..a1e71a1 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -4,7 +4,6 @@ import FollowComponent from './follow'; import * as settingActions from 'content/actions/setting'; import messages from 'shared/messages'; import * as addonActions from '../../actions/addon'; -import * as re from 'shared/utils/re'; import * as blacklists from 'shared/blacklists'; export default class Common { @@ -24,44 +23,26 @@ export default class Common { 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()); - } - } - - update() { - let enabled = this.store.getState().addon.enabled; - if (enabled !== this.prevEnabled) { - this.prevEnabled = enabled; - - browser.runtime.sendMessage({ - type: messages.ADDON_ENABLED_RESPONSE, - enabled, - }); - } - - let blacklist = this.store.getState().setting.blacklist; - let str = JSON.stringify(blacklist) - if (blacklist !== str) { - this.prevBlacklist = str; - if (blacklists.includes(blacklist, this.win.location)) { - this.store.dispatch(addonActions.disable()); - } else { - this.store.dispatch(addonActions.enable()); - } + this.store.dispatch(addonActions.setEnabled(!enabled)); } } reloadSettings() { try { - this.store.dispatch(settingActions.load()); + 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); diff --git a/src/content/components/common/keymapper.js b/src/content/components/common/keymapper.js index 326bdd9..4c294b4 100644 --- a/src/content/components/common/keymapper.js +++ b/src/content/components/common/keymapper.js @@ -48,7 +48,9 @@ export default class KeymapperComponent { return true; } let operation = keymaps.get(matched[0]); - let act = 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/index.js b/src/content/index.js index d6743ce..3b0b49b 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -10,7 +10,6 @@ const store = createStore( applyMiddleware(promise), ); - if (window.self === window.top) { new TopContentComponent(window, store); // eslint-disable-line no-new } else { 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/shared/blacklists.js b/src/shared/blacklists.js index 21e3e14..19ed3f1 100644 --- a/src/shared/blacklists.js +++ b/src/shared/blacklists.js @@ -1,13 +1,13 @@ import * as re from 'shared/utils/re'; const includes = (blacklist, url) => { - let u = new URL(url) + let u = new URL(url); return blacklist.some((item) => { if (!item.includes('/')) { return re.fromWildcard(item).test(u.hostname); } return re.fromWildcard(item).test(u.hostname + u.pathname); }); -} +}; export { includes }; diff --git a/test/content/actions/addon.test.js b/test/content/actions/addon.test.js deleted file mode 100644 index 5f96372..0000000 --- a/test/content/actions/addon.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import actions from 'content/actions'; -import * as addonActions from 'content/actions/addon'; - -describe("addon actions", () => { - describe("enable", () => { - it('create ADDON_ENABLE action', () => { - let action = addonActions.enable(); - expect(action.type).to.equal(actions.ADDON_ENABLE); - }); - }); - - describe("disable", () => { - it('create ADDON_DISABLE action', () => { - let action = addonActions.disable(); - expect(action.type).to.equal(actions.ADDON_DISABLE); - }); - }); - - describe("toggle", () => { - it('create ADDON_TOGGLE_ENABLED action', () => { - let action = addonActions.toggleEnabled(); - expect(action.type).to.equal(actions.ADDON_TOGGLE_ENABLED); - }); - }); -}); diff --git a/test/content/reducers/addon.test.js b/test/content/reducers/addon.test.js index 8c546d2..d4eb845 100644 --- a/test/content/reducers/addon.test.js +++ b/test/content/reducers/addon.test.js @@ -7,31 +7,11 @@ describe("addon reducer", () => { expect(state).to.have.property('enabled', true); }); - it('return next state for ADDON_ENABLE', () => { - let action = { type: actions.ADDON_ENABLE}; + it('return next state for ADDON_SET_ENABLED', () => { + let action = { type: actions.ADDON_SET_ENABLED, enabled: true }; let prev = { enabled: false }; let state = addonReducer(prev, action); expect(state.enabled).is.equal(true); }); - - it('return next state for ADDON_DISABLE', () => { - let action = { type: actions.ADDON_DISABLE}; - let prev = { enabled: true }; - let state = addonReducer(prev, action); - - expect(state.enabled).is.equal(false); - }); - - it('return next state for ADDON_TOGGLE_ENABLED', () => { - let action = { type: actions.ADDON_TOGGLE_ENABLED }; - let state = { enabled: false }; - - state = addonReducer(state, action); - expect(state.enabled).is.equal(true); - - state = addonReducer(state, action); - expect(state.enabled).is.equal(false); - }); - }); -- cgit v1.2.3 From 96649fef63f467d45aefee93dfb599546151c21d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 8 Jul 2018 11:39:56 +0900 Subject: See you my redux --- src/shared/store/index.js | 53 ------------------- src/shared/store/provider.jsx | 15 ------ test/shared/store/index.test.js | 110 ---------------------------------------- 3 files changed, 178 deletions(-) delete mode 100644 src/shared/store/index.js delete mode 100644 src/shared/store/provider.jsx delete mode 100644 test/shared/store/index.test.js (limited to 'src/shared') diff --git a/src/shared/store/index.js b/src/shared/store/index.js deleted file mode 100644 index 2fafdf1..0000000 --- a/src/shared/store/index.js +++ /dev/null @@ -1,53 +0,0 @@ -class Store { - constructor(reducer, catcher) { - this.reducer = reducer; - this.catcher = catcher; - this.subscribers = []; - try { - this.state = this.reducer(undefined, {}); - } catch (e) { - catcher(e); - } - } - - dispatch(action, sender) { - if (action instanceof Promise) { - action.then((a) => { - this.transitNext(a, sender); - }).catch((e) => { - this.catcher(e, sender); - }); - } else { - try { - this.transitNext(action, sender); - } catch (e) { - this.catcher(e, sender); - } - } - return action; - } - - getState() { - return this.state; - } - - subscribe(callback) { - this.subscribers.push(callback); - } - - transitNext(action, sender) { - let newState = this.reducer(this.state, action); - if (JSON.stringify(this.state) !== JSON.stringify(newState)) { - this.state = newState; - this.subscribers.forEach(f => f(sender)); - } - } -} - -const empty = () => {}; - -const createStore = (reducer, catcher = empty) => { - return new Store(reducer, catcher); -}; - -export { createStore }; diff --git a/src/shared/store/provider.jsx b/src/shared/store/provider.jsx deleted file mode 100644 index fe925aa..0000000 --- a/src/shared/store/provider.jsx +++ /dev/null @@ -1,15 +0,0 @@ -import { h, Component } from 'preact'; - -class Provider extends Component { - getChildContext() { - return { store: this.props.store }; - } - - render() { - return
- { this.props.children } -
; - } -} - -export default Provider; diff --git a/test/shared/store/index.test.js b/test/shared/store/index.test.js deleted file mode 100644 index 5b69b40..0000000 --- a/test/shared/store/index.test.js +++ /dev/null @@ -1,110 +0,0 @@ -import { createStore } from 'shared/store'; - -describe("Store class", () => { - const reducer = (state, action) => { - if (state == undefined) { - return 0; - } - return state + action; - }; - - describe("#dispatch", () => { - it('transit status by immediate action', () => { - let store = createStore(reducer); - store.dispatch(10); - expect(store.getState()).to.equal(10); - - store.dispatch(-20); - expect(store.getState()).to.equal(-10); - }); - - it('returns next state by immediate action', () => { - let store = createStore(reducer); - let dispatchedAction = store.dispatch(11); - expect(dispatchedAction).to.equal(11); - }); - - it('transit status by Promise action', () => { - let store = createStore(reducer); - let p1 = Promise.resolve(10); - - return store.dispatch(p1).then(() => { - expect(store.getState()).to.equal(10); - }).then(() => { - store.dispatch(Promise.resolve(-20)); - }).then(() => { - expect(store.getState()).to.equal(-10); - }); - }); - - it('returns next state by promise action', () => { - let store = createStore(reducer); - let dispatchedAction = store.dispatch(Promise.resolve(11)); - return dispatchedAction.then((value) => { - expect(value).to.equal(11); - }); - }); - }); - - describe("#subscribe", () => { - it('invoke callback', (done) => { - let store = createStore(reducer); - store.subscribe(() => { - expect(store.getState()).to.equal(15); - done(); - }); - store.dispatch(15); - }); - - it('propagate sender object', (done) => { - let store = createStore(reducer); - store.subscribe((sender) => { - expect(sender).to.equal('sender'); - done(); - }); - store.dispatch(15, 'sender'); - }); - }) - - describe("catcher", () => { - it('catch an error in reducer on initializing by immediate action', (done) => { - let store = createStore(() => { - throw new Error(); - }, (e) => { - expect(e).to.be.an('error'); - done(); - }); - }); - - it('catch an error in reducer on initializing by immediate action', (done) => { - let store = createStore((state, action) => { - if (state === undefined) return 0; - throw new Error(); - }, (e) => { - expect(e).to.be.an('error'); - done(); - }); - store.dispatch(20); - }); - - it('catch an error in reducer on initializing by promise action', (done) => { - let store = createStore((state, action) => { - if (state === undefined) return 0; - throw new Error(); - }, (e) => { - expect(e).to.be.an('error'); - done(); - }); - store.dispatch(Promise.resolve(20)); - }); - - it('catch an error in promise action', (done) => { - let store = createStore((state, action) => 0, (e) => { - expect(e).to.be.an('error'); - done(); - }); - store.dispatch(new Promise(() => { throw new Error() })); - }); - }) -}); - -- cgit v1.2.3 From 067da88d06fbffca323ecdbaf8b1011f88225219 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 11 Jul 2018 21:01:48 +0900 Subject: Move versions to background --- src/background/index.js | 2 +- src/background/shared/versions/index.js | 38 +++++++++++++++++++++++ src/background/shared/versions/release-notes.js | 8 +++++ src/background/shared/versions/storage.js | 10 +++++++ src/shared/versions/index.js | 38 ----------------------- src/shared/versions/release-notes.js | 8 ----- src/shared/versions/storage.js | 10 ------- test/background/shared/versions/index.test.js | 40 +++++++++++++++++++++++++ test/background/shared/versions/storage.test.js | 28 +++++++++++++++++ test/shared/versions/index.test.js | 40 ------------------------- test/shared/versions/storage.test.js | 28 ----------------- 11 files changed, 125 insertions(+), 125 deletions(-) create mode 100644 src/background/shared/versions/index.js create mode 100644 src/background/shared/versions/release-notes.js create mode 100644 src/background/shared/versions/storage.js delete mode 100644 src/shared/versions/index.js delete mode 100644 src/shared/versions/release-notes.js delete mode 100644 src/shared/versions/storage.js create mode 100644 test/background/shared/versions/index.test.js create mode 100644 test/background/shared/versions/storage.test.js delete mode 100644 test/shared/versions/index.test.js delete mode 100644 test/shared/versions/storage.test.js (limited to 'src/shared') diff --git a/src/background/index.js b/src/background/index.js index 8c4eafc..1e4c078 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -6,7 +6,7 @@ import IndicatorComponent from 'background/components/indicator'; import reducers from 'background/reducers'; import { createStore, applyMiddleware } from 'redux'; import promise from 'redux-promise'; -import * as versions from 'shared/versions'; +import * as versions from './shared/versions'; const store = createStore( reducers, diff --git a/src/background/shared/versions/index.js b/src/background/shared/versions/index.js new file mode 100644 index 0000000..aa09c92 --- /dev/null +++ b/src/background/shared/versions/index.js @@ -0,0 +1,38 @@ +import * as storage from './storage'; +import * as releaseNotes from './release-notes'; +import manifest from '../../../../manifest.json'; + +const NOTIFICATION_ID = 'vimvixen-update'; + +const notificationClickListener = (id) => { + if (id !== NOTIFICATION_ID) { + return; + } + + browser.tabs.create({ url: releaseNotes.url(manifest.version) }); + browser.notifications.onClicked.removeListener(notificationClickListener); +}; + +const checkUpdated = async() => { + let prev = await storage.load(); + if (!prev) { + return true; + } + return manifest.version !== prev; +}; + +const notify = () => { + browser.notifications.onClicked.addListener(notificationClickListener); + return browser.notifications.create(NOTIFICATION_ID, { + 'type': 'basic', + 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), + 'title': 'Vim Vixen ' + manifest.version + ' has been installed', + 'message': 'Click here to see release notes', + }); +}; + +const commit = () => { + storage.save(manifest.version); +}; + +export { checkUpdated, notify, commit }; diff --git a/src/background/shared/versions/release-notes.js b/src/background/shared/versions/release-notes.js new file mode 100644 index 0000000..6ef2335 --- /dev/null +++ b/src/background/shared/versions/release-notes.js @@ -0,0 +1,8 @@ +const url = (version) => { + if (version) { + return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; + } + return 'https://github.com/ueokande/vim-vixen/releases/'; +}; + +export { url }; diff --git a/src/background/shared/versions/storage.js b/src/background/shared/versions/storage.js new file mode 100644 index 0000000..7883258 --- /dev/null +++ b/src/background/shared/versions/storage.js @@ -0,0 +1,10 @@ +const load = async() => { + let { version } = await browser.storage.local.get('version'); + return version; +}; + +const save = (version) => { + return browser.storage.local.set({ version }); +}; + +export { load, save }; diff --git a/src/shared/versions/index.js b/src/shared/versions/index.js deleted file mode 100644 index ba3d183..0000000 --- a/src/shared/versions/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import * as storage from './storage'; -import * as releaseNotes from './release-notes'; -import manifest from '../../../manifest.json'; - -const NOTIFICATION_ID = 'vimvixen-update'; - -const notificationClickListener = (id) => { - if (id !== NOTIFICATION_ID) { - return; - } - - browser.tabs.create({ url: releaseNotes.url(manifest.version) }); - browser.notifications.onClicked.removeListener(notificationClickListener); -}; - -const checkUpdated = async() => { - let prev = await storage.load(); - if (!prev) { - return true; - } - return manifest.version !== prev; -}; - -const notify = () => { - browser.notifications.onClicked.addListener(notificationClickListener); - return browser.notifications.create(NOTIFICATION_ID, { - 'type': 'basic', - 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), - 'title': 'Vim Vixen ' + manifest.version + ' has been installed', - 'message': 'Click here to see release notes', - }); -}; - -const commit = () => { - storage.save(manifest.version); -}; - -export { checkUpdated, notify, commit }; diff --git a/src/shared/versions/release-notes.js b/src/shared/versions/release-notes.js deleted file mode 100644 index 6ef2335..0000000 --- a/src/shared/versions/release-notes.js +++ /dev/null @@ -1,8 +0,0 @@ -const url = (version) => { - if (version) { - return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; - } - return 'https://github.com/ueokande/vim-vixen/releases/'; -}; - -export { url }; diff --git a/src/shared/versions/storage.js b/src/shared/versions/storage.js deleted file mode 100644 index 7883258..0000000 --- a/src/shared/versions/storage.js +++ /dev/null @@ -1,10 +0,0 @@ -const load = async() => { - let { version } = await browser.storage.local.get('version'); - return version; -}; - -const save = (version) => { - return browser.storage.local.set({ version }); -}; - -export { load, save }; diff --git a/test/background/shared/versions/index.test.js b/test/background/shared/versions/index.test.js new file mode 100644 index 0000000..d65dd9a --- /dev/null +++ b/test/background/shared/versions/index.test.js @@ -0,0 +1,40 @@ +import * as versions from 'background/shared/versions'; +import manifest from '../../../../manifest.json'; + +describe("shared/versions/storage", () => { + describe('#checkUpdated', () => { + beforeEach(() => { + return browser.storage.local.remove('version'); + }); + + it('return true if no previous versions', async() => { + let updated = await versions.checkUpdated(); + expect(updated).to.be.true; + }); + + it('return true if updated', async() => { + await browser.storage.local.set({ version: '0.001' }); + let updated = await versions.checkUpdated(); + expect(updated).to.be.true; + }); + + it('return false if not updated', async() => { + await browser.storage.local.set({ version: manifest.version }); + let updated = await versions.checkUpdated(); + expect(updated).to.be.false; + }); + }); + + describe('#commit', () => { + beforeEach(() => { + return browser.storage.local.remove('version'); + }); + + it('saves current version from manifest.json', async() => { + await versions.commit(); + let { version } = await browser.storage.local.get('version'); + expect(version).to.be.a('string'); + expect(version).to.equal(manifest.version); + }); + }); +}); diff --git a/test/background/shared/versions/storage.test.js b/test/background/shared/versions/storage.test.js new file mode 100644 index 0000000..f452516 --- /dev/null +++ b/test/background/shared/versions/storage.test.js @@ -0,0 +1,28 @@ +import * as storage from 'background/shared/versions/storage'; + +describe("shared/versions/storage", () => { + describe('#load', () => { + beforeEach(() => { + return browser.storage.local.remove('version'); + }); + + it('loads saved version', async() => { + await browser.storage.local.set({ version: '1.2.3' }); + let version = await storage.load(); + expect(version).to.equal('1.2.3'); + }); + + it('returns undefined if no versions in storage', async() => { + let version = await storage.load(); + expect(version).to.be.a('undefined'); + }); + }); + + describe('#save', () => { + it('saves version string', async() => { + await storage.save('2.3.4'); + let { version } = await browser.storage.local.get('version'); + expect(version).to.equal('2.3.4'); + }); + }); +}); diff --git a/test/shared/versions/index.test.js b/test/shared/versions/index.test.js deleted file mode 100644 index d90f04c..0000000 --- a/test/shared/versions/index.test.js +++ /dev/null @@ -1,40 +0,0 @@ -import * as versions from 'shared/versions'; -import manifest from '../../../manifest.json'; - -describe("shared/versions/storage", () => { - describe('#checkUpdated', () => { - beforeEach(() => { - return browser.storage.local.remove('version'); - }); - - it('return true if no previous versions', async() => { - let updated = await versions.checkUpdated(); - expect(updated).to.be.true; - }); - - it('return true if updated', async() => { - await browser.storage.local.set({ version: '0.001' }); - let updated = await versions.checkUpdated(); - expect(updated).to.be.true; - }); - - it('return false if not updated', async() => { - await browser.storage.local.set({ version: manifest.version }); - let updated = await versions.checkUpdated(); - expect(updated).to.be.false; - }); - }); - - describe('#commit', () => { - beforeEach(() => { - return browser.storage.local.remove('version'); - }); - - it('saves current version from manifest.json', async() => { - await versions.commit(); - let { version } = await browser.storage.local.get('version'); - expect(version).to.be.a('string'); - expect(version).to.equal(manifest.version); - }); - }); -}); diff --git a/test/shared/versions/storage.test.js b/test/shared/versions/storage.test.js deleted file mode 100644 index f541abf..0000000 --- a/test/shared/versions/storage.test.js +++ /dev/null @@ -1,28 +0,0 @@ -import * as storage from 'shared/versions/storage'; - -describe("shared/versions/storage", () => { - describe('#load', () => { - beforeEach(() => { - return browser.storage.local.remove('version'); - }); - - it('loads saved version', async() => { - await browser.storage.local.set({ version: '1.2.3' }); - let version = await storage.load(); - expect(version).to.equal('1.2.3'); - }); - - it('returns undefined if no versions in storage', async() => { - let version = await storage.load(); - expect(version).to.be.a('undefined'); - }); - }); - - describe('#save', () => { - it('saves version string', async() => { - await storage.save('2.3.4'); - let { version } = await browser.storage.local.get('version'); - expect(version).to.equal('2.3.4'); - }); - }); -}); -- cgit v1.2.3 From a28f6f916d77baf87e3c023abbd6494e009a8d12 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 11 Jul 2018 21:32:28 +0900 Subject: Complete console commands --- src/background/shared/completions/index.js | 41 ++++++++++++++++++------------ src/shared/commands/docs.js | 11 ++++++++ 2 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/shared/commands/docs.js (limited to 'src/shared') diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js index d5875fe..22713e7 100644 --- a/src/background/shared/completions/index.js +++ b/src/background/shared/completions/index.js @@ -1,7 +1,19 @@ +import commandDocs from 'shared/commands/docs'; import * as tabs from './tabs'; import * as histories from './histories'; import * as bookmarks from './bookmarks'; +const completeCommands = (typing) => { + let keys = Object.keys(commandDocs); + return keys + .filter(name => name.startsWith(typing)) + .map(name => ({ + caption: name, + content: name, + url: commandDocs[name], + })); +}; + const getSearchCompletions = (command, keywords, searchConfig) => { let engineNames = Object.keys(searchConfig.engines); let engineItems = engineNames.filter(name => name.startsWith(keywords)) @@ -74,20 +86,21 @@ const getBufferCompletions = async(command, keywords, excludePinned) => { ]; }; -const getCompletions = (line, settings) => { - let typedWords = line.trim().split(/ +/); - let typing = ''; - if (!line.endsWith(' ')) { - typing = typedWords.pop(); - } - - if (typedWords.length === 0) { - return Promise.resolve([]); +const complete = (line, settings) => { + let trimmed = line.trimStart(); + let words = trimmed.split(/ +/); + let name = words[0]; + if (words.length === 1) { + return Promise.resolve([ + { + name: 'Console Command', + items: completeCommands(name), + } + ]); } - let name = typedWords.shift(); - let keywords = typedWords.concat(typing).join(' '); + let keywords = trimmed.slice(name.length).trimStart(); - switch (name) { + switch (words[0]) { case 'o': case 'open': case 't': @@ -112,8 +125,4 @@ const getCompletions = (line, settings) => { return Promise.resolve([]); }; -const complete = (line, settings) => { - return getCompletions(line, settings); -}; - export { complete }; diff --git a/src/shared/commands/docs.js b/src/shared/commands/docs.js new file mode 100644 index 0000000..c73eb71 --- /dev/null +++ b/src/shared/commands/docs.js @@ -0,0 +1,11 @@ +export default { + set: 'Set a value of the property', + open: 'Open a URL or search by keywords in current tab', + tabopen: 'Open a URL or search by keywords in new tab', + winopen: 'Open a URL or search by keywords in new window', + buffer: 'Sekect tabs by matched keywords', + bdelete: 'Close a certain tab matched by keywords', + bdeletes: 'Close all tabs matched by keywords', + quit: 'Close the current tab', + quitall: 'Close all tabs', +}; -- cgit v1.2.3 From 1e39fed6183bf3b10f48eb52868bb5ab3fe3134f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 11 Jul 2018 21:58:41 +0900 Subject: Complete set commands --- src/background/shared/completions/index.js | 37 ++++++++++++++++++++++++++++++ src/shared/settings/properties.js | 8 ++++++- 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'src/shared') diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js index 22713e7..d630f33 100644 --- a/src/background/shared/completions/index.js +++ b/src/background/shared/completions/index.js @@ -2,6 +2,7 @@ import commandDocs from 'shared/commands/docs'; import * as tabs from './tabs'; import * as histories from './histories'; import * as bookmarks from './bookmarks'; +import * as properties from 'shared/settings/properties'; const completeCommands = (typing) => { let keys = Object.keys(commandDocs); @@ -86,6 +87,40 @@ const getBufferCompletions = async(command, keywords, excludePinned) => { ]; }; +const getSetCompletions = (command, keywords) => { + let keys = Object.keys(properties.docs).filter( + name => name.startsWith(keywords) + ); + let items = keys.map((key) => { + if (properties.types[key] === 'boolean') { + return [ + { + caption: key, + content: command + ' ' + key, + url: 'Enable ' + properties.docs[key], + }, { + caption: 'no' + key, + content: command + ' no' + key, + url: 'Disable ' + properties.docs[key], + } + ]; + } + return [ + { + caption: key, + content: command + ' ' + key, + url: 'Set ' + properties.docs[key], + } + ]; + }).flat(); + return Promise.resolve([ + { + name: 'Properties', + items, + } + ]); +}; + const complete = (line, settings) => { let trimmed = line.trimStart(); let words = trimmed.split(/ +/); @@ -121,6 +156,8 @@ const complete = (line, settings) => { case 'bdelete': case 'bdeletes': return getBufferCompletions(name, keywords, true); + case 'set': + return getSetCompletions(name, keywords); } return Promise.resolve([]); }; diff --git a/src/shared/settings/properties.js b/src/shared/settings/properties.js index 4bda8d6..b392cbb 100644 --- a/src/shared/settings/properties.js +++ b/src/shared/settings/properties.js @@ -15,4 +15,10 @@ const defaults = { adjacenttab: true, }; -export { types, defaults }; +const docs = { + hintchars: 'Hint characters on follow mode', + smoothscroll: 'smooth scroll', + adjacenttab: 'open adjacent tabs', +}; + +export { types, defaults, docs }; -- cgit v1.2.3