From 177940981ed9c4f096ad7db20f0b7ee044fd7b17 Mon Sep 17 00:00:00 2001 From: Erwan Ameil Date: Thu, 16 Nov 2017 13:03:38 +0000 Subject: Open adjacent tabs and background tabs --- src/background/actions/tab.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/tab.js b/src/background/actions/tab.js index e512b6f..d996b9f 100644 --- a/src/background/actions/tab.js +++ b/src/background/actions/tab.js @@ -1,9 +1,5 @@ -const openNewTab = (url) => { - return browser.tabs.create({ url: url }); -}; - const openToTab = (url, tab) => { return browser.tabs.update(tab.id, { url: url }); }; -export { openToTab, openNewTab }; +export { openToTab }; -- cgit v1.2.3 From 50cc126e08056b4f5191f603c01f0e9951692696 Mon Sep 17 00:00:00 2001 From: Daniel Campoverde Date: Sun, 5 Nov 2017 18:04:46 -0500 Subject: Dummy selectPrevSelTab implementation --- src/background/actions/operation.js | 2 ++ src/background/tabs.js | 18 ++++++++++++++++-- src/shared/operations.js | 1 + src/shared/settings/default.js | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/operation.js b/src/background/actions/operation.js index 1e4990c..cfee868 100644 --- a/src/background/actions/operation.js +++ b/src/background/actions/operation.js @@ -27,6 +27,8 @@ const exec = (operation, tab) => { return tabs.selectFirstTab(); case operations.TAB_LAST: return tabs.selectLastTab(); + case operations.TAB_PREV_SEL: + return tabs.selectPrevSelTab(); case operations.TAB_RELOAD: return tabs.reload(tab, operation.cache); case operations.TAB_PIN: diff --git a/src/background/tabs.js b/src/background/tabs.js index d641616..ce48eda 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -1,3 +1,6 @@ +// var prevSelTab = null; +var prevSelTab = 0; + const closeTab = (id) => { return browser.tabs.remove(id); }; @@ -93,6 +96,17 @@ const selectLastTab = () => { }); }; +const selectPrevSelTab = () => { + if (prevSelTab != null) { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let id = tabs[prevSelTab].id; + return browser.tabs.update(id, { active: true }); + }); + } else { + // some error message + } +}; + const reload = (current, cache) => { return browser.tabs.reload( current.id, @@ -117,6 +131,6 @@ const duplicate = (id) => { export { closeTab, reopenTab, selectAt, selectByKeyword, getCompletions, - selectPrevTab, selectNextTab, selectFirstTab, selectLastTab, reload, - updateTabPinned, toggleTabPinned, duplicate + selectPrevTab, selectNextTab, selectFirstTab, selectLastTab, selectPrevSelTab, + reload, updateTabPinned, toggleTabPinned, duplicate }; diff --git a/src/shared/operations.js b/src/shared/operations.js index 4c221ba..235793a 100644 --- a/src/shared/operations.js +++ b/src/shared/operations.js @@ -38,6 +38,7 @@ export default { TAB_NEXT: 'tabs.next', TAB_FIRST: 'tabs.first', TAB_LAST: 'tabs.last', + TAB_PREV_SEL: 'tabs.prevsel', TAB_RELOAD: 'tabs.reload', TAB_PIN: 'tabs.pin', TAB_UNPIN: 'tabs.unpin', diff --git a/src/shared/settings/default.js b/src/shared/settings/default.js index 69238e3..44ac5f4 100644 --- a/src/shared/settings/default.js +++ b/src/shared/settings/default.js @@ -30,6 +30,7 @@ export default { "J": { "type": "tabs.next", "count": 1 }, "g0": { "type": "tabs.first" }, "g$": { "type": "tabs.last" }, + "gl": { "type": "tabs.prevsel" }, "r": { "type": "tabs.reload", "cache": false }, "R": { "type": "tabs.reload", "cache": true }, "zp": { "type": "tabs.pin.toggle" }, -- cgit v1.2.3 From 6083e70ea089fa2683741a1118be0e4e6b76f858 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 8 Jan 2018 21:54:16 +0900 Subject: separate setting actions and reducers --- src/background/actions/index.js | 4 ++++ src/background/actions/setting.js | 13 +++++++++++++ src/background/components/background.js | 4 ++-- src/background/index.js | 4 ++-- src/background/reducers/index.js | 2 +- src/background/reducers/setting.js | 17 +++++++++++++++++ src/settings/actions/setting.js | 18 +++++++----------- src/shared/settings/storage.js | 31 +++++++++++++++++++++++++++++++ test/background/reducers/setting.test.js | 19 +++++++++++++++++++ 9 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 src/background/actions/index.js create mode 100644 src/background/actions/setting.js create mode 100644 src/background/reducers/setting.js create mode 100644 src/shared/settings/storage.js create mode 100644 test/background/reducers/setting.test.js (limited to 'src/background/actions') diff --git a/src/background/actions/index.js b/src/background/actions/index.js new file mode 100644 index 0000000..8c212c2 --- /dev/null +++ b/src/background/actions/index.js @@ -0,0 +1,4 @@ +export default { + // Settings + SETTING_SET_SETTINGS: 'setting.set.settings', +}; diff --git a/src/background/actions/setting.js b/src/background/actions/setting.js new file mode 100644 index 0000000..0454a68 --- /dev/null +++ b/src/background/actions/setting.js @@ -0,0 +1,13 @@ +import actions from '../actions'; +import * as settingsStorage from 'shared/settings/storage'; + +const load = () => { + return settingsStorage.loadValue().then((value) => { + return { + type: actions.SETTING_SET_SETTINGS, + value, + }; + }); +}; + +export { load }; diff --git a/src/background/components/background.js b/src/background/components/background.js index 2d94310..22c6693 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -1,6 +1,6 @@ import messages from 'shared/messages'; import * as operationActions from 'background/actions/operation'; -import * as settingsActions from 'settings/actions/setting'; +import * as settingActions from 'background/actions/setting'; import * as tabActions from 'background/actions/tab'; import * as commands from 'shared/commands'; @@ -46,7 +46,7 @@ export default class BackgroundComponent { case messages.CONSOLE_QUERY_COMPLETIONS: return commands.complete(message.text, settings.value); case messages.SETTINGS_RELOAD: - this.store.dispatch(settingsActions.load()); + this.store.dispatch(settingActions.load()); return this.broadcastSettingsChanged(); } } diff --git a/src/background/index.js b/src/background/index.js index 8a68767..3ef712f 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,4 +1,4 @@ -import * as settingsActions from 'settings/actions/setting'; +import * as settingActions from 'background/actions/setting'; import messages from 'shared/messages'; import BackgroundComponent from 'background/components/background'; import reducers from 'background/reducers'; @@ -16,4 +16,4 @@ const store = createStore(reducers, (e, sender) => { // eslint-disable-next-line no-unused-vars const backgroundComponent = new BackgroundComponent(store); -store.dispatch(settingsActions.load()); +store.dispatch(settingActions.load()); diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js index 4be8fac..dab0c62 100644 --- a/src/background/reducers/index.js +++ b/src/background/reducers/index.js @@ -1,4 +1,4 @@ -import settingReducer from 'settings/reducers/setting'; +import settingReducer from './setting'; // Make setting reducer instead of re-use const defaultState = { diff --git a/src/background/reducers/setting.js b/src/background/reducers/setting.js new file mode 100644 index 0000000..70bf8ea --- /dev/null +++ b/src/background/reducers/setting.js @@ -0,0 +1,17 @@ +import actions from 'settings/actions'; + +const defaultState = { + value: {}, +}; + +export default function reducer(state = defaultState, action = {}) { + switch (action.type) { + case actions.SETTING_SET_SETTINGS: + return { + value: action.value, + }; + default: + return state; + } +} + diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index 1d01fda..92c9f8a 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -1,26 +1,22 @@ import actions from 'settings/actions'; import messages from 'shared/messages'; import DefaultSettings from 'shared/settings/default'; +import * as settingsStorage from 'shared/settings/storage'; import * as settingsValues from 'shared/settings/values'; const load = () => { - return browser.storage.local.get('settings').then(({ settings }) => { - if (!settings) { - return set(DefaultSettings); - } - return set(Object.assign({}, DefaultSettings, settings)); - }, console.error); + return settingsStorage.loadRaw().then((settings) => { + return set(settings); + }); }; const save = (settings) => { - return browser.storage.local.set({ - settings, - }).then(() => { + return settingsStorage.save(settings).then(() => { return browser.runtime.sendMessage({ type: messages.SETTINGS_RELOAD - }).then(() => { - return set(settings); }); + }).then(() => { + return set(settings); }); }; diff --git a/src/shared/settings/storage.js b/src/shared/settings/storage.js new file mode 100644 index 0000000..1edb441 --- /dev/null +++ b/src/shared/settings/storage.js @@ -0,0 +1,31 @@ +import DefaultSettings from './default'; +import * as settingsValues from './values'; + +const loadRaw = () => { + return browser.storage.local.get('settings').then(({ settings }) => { + if (!settings) { + return DefaultSettings; + } + return Object.assign({}, DefaultSettings, settings); + }); +}; + +const loadValue = () => { + return loadRaw().then((settings) => { + let value = JSON.parse(DefaultSettings.json); + if (settings.source === 'json') { + value = settingsValues.valueFromJson(settings.json); + } else if (settings.source === 'form') { + value = settingsValues.valueFromForm(settings.form); + } + return value; + }); +}; + +const save = (settings) => { + return browser.storage.local.set({ + settings, + }); +}; + +export { loadRaw, loadValue, save }; diff --git a/test/background/reducers/setting.test.js b/test/background/reducers/setting.test.js new file mode 100644 index 0000000..a6a5573 --- /dev/null +++ b/test/background/reducers/setting.test.js @@ -0,0 +1,19 @@ +import { expect } from "chai"; +import actions from 'background/actions'; +import settingReducer from 'background/reducers/setting'; + +describe("setting reducer", () => { + it('return the initial state', () => { + let state = settingReducer(undefined, {}); + expect(state).to.have.deep.property('value', {}); + }); + + it('return next state for SETTING_SET_SETTINGS', () => { + let action = { + type: actions.SETTING_SET_SETTINGS, + value: { key: 123 }, + }; + let state = settingReducer(undefined, action); + expect(state).to.have.deep.property('value', { key: 123 }); + }); +}); -- cgit v1.2.3 From 22c34a0a6f9721fb9d907ab10de91cbbc40d6bbe Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 10 Jan 2018 21:34:40 +0900 Subject: add set property action in background --- src/background/actions/index.js | 1 + src/background/actions/setting.js | 10 +++++++++- src/background/reducers/setting.js | 9 ++++++++- test/background/reducers/setting.test.js | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/index.js b/src/background/actions/index.js index 8c212c2..efe4074 100644 --- a/src/background/actions/index.js +++ b/src/background/actions/index.js @@ -1,4 +1,5 @@ export default { // Settings SETTING_SET_SETTINGS: 'setting.set.settings', + SETTING_SET_PROPERTY: 'setting.set.property', }; diff --git a/src/background/actions/setting.js b/src/background/actions/setting.js index 0454a68..773142f 100644 --- a/src/background/actions/setting.js +++ b/src/background/actions/setting.js @@ -10,4 +10,12 @@ const load = () => { }); }; -export { load }; +const setProperty = (name, value) => { + return { + type: actions.SETTING_SET_PROPERTY, + name, + value, + }; +}; + +export { load, setProperty }; diff --git a/src/background/reducers/setting.js b/src/background/reducers/setting.js index 70bf8ea..045a654 100644 --- a/src/background/reducers/setting.js +++ b/src/background/reducers/setting.js @@ -1,4 +1,4 @@ -import actions from 'settings/actions'; +import actions from 'background/actions'; const defaultState = { value: {}, @@ -10,6 +10,13 @@ export default function reducer(state = defaultState, action = {}) { return { value: action.value, }; + case actions.SETTING_SET_PROPERTY: + return { + value: Object.assign({}, state.value, { + properties: Object.assign({}, state.value.properties, + { [action.name]: action.value }) + }) + }; default: return state; } diff --git a/test/background/reducers/setting.test.js b/test/background/reducers/setting.test.js index a6a5573..2ef98cb 100644 --- a/test/background/reducers/setting.test.js +++ b/test/background/reducers/setting.test.js @@ -16,4 +16,22 @@ describe("setting reducer", () => { let state = settingReducer(undefined, action); expect(state).to.have.deep.property('value', { key: 123 }); }); + + it('return next state for SETTING_SET_PROPERTY', () => { + let state = { + value: { + properties: { smoothscroll: true } + } + } + let action = { + type: actions.SETTING_SET_PROPERTY, + name: 'encoding', + value: 'utf-8', + }; + state = settingReducer(state, action); + + console.log(state); + expect(state.value.properties).to.have.property('smoothscroll', true); + expect(state.value.properties).to.have.property('encoding', 'utf-8'); + }); }); -- cgit v1.2.3 From dda4e7475cdd092d00441c7cd0ceb194ee5dee3d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 11 Jan 2018 20:07:25 +0900 Subject: move commands to background action --- src/background/actions/command.js | 62 ++++++++++++++++++++++++ src/background/components/background.js | 3 +- src/shared/commands/exec.js | 85 --------------------------------- src/shared/commands/index.js | 3 +- src/shared/commands/parsers.js | 59 +++++++++++++++++++++++ src/shared/commands/properties.js | 31 ------------ test/shared/commands/parsers.test.js | 78 ++++++++++++++++++++++++++++++ test/shared/commands/property.test.js | 41 ---------------- 8 files changed, 202 insertions(+), 160 deletions(-) create mode 100644 src/background/actions/command.js delete mode 100644 src/shared/commands/exec.js create mode 100644 src/shared/commands/parsers.js delete mode 100644 src/shared/commands/properties.js create mode 100644 test/shared/commands/parsers.test.js delete mode 100644 test/shared/commands/property.test.js (limited to 'src/background/actions') diff --git a/src/background/actions/command.js b/src/background/actions/command.js new file mode 100644 index 0000000..f11c61b --- /dev/null +++ b/src/background/actions/command.js @@ -0,0 +1,62 @@ +import * as tabs from 'background/tabs'; +import * as parsers from 'shared/commands/parsers'; + +const openCommand = (url) => { + return browser.tabs.query({ + active: true, currentWindow: true + }).then((gotTabs) => { + if (gotTabs.length > 0) { + return browser.tabs.update(gotTabs[0].id, { url: url }); + } + }); +}; + +const tabopenCommand = (url) => { + return browser.tabs.create({ url: url }); +}; + +const winopenCommand = (url) => { + return browser.windows.create({ url }); +}; + +const bufferCommand = (keywords) => { + if (keywords.length === 0) { + return Promise.resolve([]); + } + let keywordsStr = keywords.join(' '); + return browser.tabs.query({ + active: true, currentWindow: true + }).then((gotTabs) => { + if (gotTabs.length > 0) { + if (isNaN(keywordsStr)) { + return tabs.selectByKeyword(gotTabs[0], keywordsStr); + } + let index = parseInt(keywordsStr, 10) - 1; + return tabs.selectAt(index); + } + }); +}; + +const exec = (line, settings) => { + let [name, args] = parsers.parseCommandLine(line); + + switch (name) { + case 'o': + case 'open': + return openCommand(parsers.normalizeUrl(args, settings.search)); + case 't': + case 'tabopen': + return tabopenCommand(parsers.normalizeUrl(args, settings.search)); + case 'w': + case 'winopen': + return winopenCommand(parsers.normalizeUrl(args, settings.search)); + case 'b': + case 'buffer': + return bufferCommand(args); + case '': + return Promise.resolve(); + } + throw new Error(name + ' command is not defined'); +}; + +export { exec }; diff --git a/src/background/components/background.js b/src/background/components/background.js index 22c6693..19bf27f 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -1,5 +1,6 @@ import messages from 'shared/messages'; import * as operationActions from 'background/actions/operation'; +import * as commandActions from 'background/actions/command'; import * as settingActions from 'background/actions/setting'; import * as tabActions from 'background/actions/tab'; import * as commands from 'shared/commands'; @@ -35,7 +36,7 @@ export default class BackgroundComponent { return this.store.dispatch( tabActions.openToTab(message.url, sender.tab), sender); case messages.CONSOLE_ENTER_COMMAND: - return commands.exec(message.text, settings.value).catch((e) => { + return commandActions.exec(message.text, settings.value).catch((e) => { return browser.tabs.sendMessage(sender.tab.id, { type: messages.CONSOLE_SHOW_ERROR, text: e.message, diff --git a/src/shared/commands/exec.js b/src/shared/commands/exec.js deleted file mode 100644 index 7248827..0000000 --- a/src/shared/commands/exec.js +++ /dev/null @@ -1,85 +0,0 @@ -import * as tabs from 'background/tabs'; -import * as histories from 'background/histories'; - -const normalizeUrl = (args, searchConfig) => { - let concat = args.join(' '); - try { - return new URL(concat).href; - } catch (e) { - if (concat.includes('.') && !concat.includes(' ')) { - return 'http://' + concat; - } - let query = concat; - let template = searchConfig.engines[ - searchConfig.default - ]; - for (let key in searchConfig.engines) { - if (args[0] === key) { - query = args.slice(1).join(' '); - template = searchConfig.engines[key]; - } - } - return template.replace('{}', encodeURIComponent(query)); - } -}; - -const openCommand = (url) => { - return browser.tabs.query({ - active: true, currentWindow: true - }).then((gotTabs) => { - if (gotTabs.length > 0) { - return browser.tabs.update(gotTabs[0].id, { url: url }); - } - }); -}; - -const tabopenCommand = (url) => { - return browser.tabs.create({ url: url }); -}; - -const winopenCommand = (url) => { - return browser.windows.create({ url }); -}; - -const bufferCommand = (keywords) => { - if (keywords.length === 0) { - return Promise.resolve([]); - } - let keywordsStr = keywords.join(' '); - return browser.tabs.query({ - active: true, currentWindow: true - }).then((gotTabs) => { - if (gotTabs.length > 0) { - if (isNaN(keywordsStr)) { - return tabs.selectByKeyword(gotTabs[0], keywordsStr); - } - let index = parseInt(keywordsStr, 10) - 1; - return tabs.selectAt(index); - } - }); -}; - -const exec = (line, settings) => { - let words = line.trim().split(/ +/); - let name = words.shift(); - - switch (name) { - case 'o': - case 'open': - return openCommand(normalizeUrl(words, settings.search)); - case 't': - case 'tabopen': - return tabopenCommand(normalizeUrl(words, settings.search)); - case 'w': - case 'winopen': - return winopenCommand(normalizeUrl(words, settings.search)); - case 'b': - case 'buffer': - return bufferCommand(words); - case '': - return Promise.resolve(); - } - throw new Error(name + ' command is not defined'); -}; - -export default exec; diff --git a/src/shared/commands/index.js b/src/shared/commands/index.js index c2cea3e..78cb4df 100644 --- a/src/shared/commands/index.js +++ b/src/shared/commands/index.js @@ -1,4 +1,3 @@ -import exec from './exec'; import complete from './complete'; -export { exec, complete }; +export { complete }; diff --git a/src/shared/commands/parsers.js b/src/shared/commands/parsers.js new file mode 100644 index 0000000..af51338 --- /dev/null +++ b/src/shared/commands/parsers.js @@ -0,0 +1,59 @@ +const normalizeUrl = (args, searchConfig) => { + let concat = args.join(' '); + try { + return new URL(concat).href; + } catch (e) { + if (concat.includes('.') && !concat.includes(' ')) { + return 'http://' + concat; + } + let query = concat; + let template = searchConfig.engines[ + searchConfig.default + ]; + for (let key in searchConfig.engines) { + if (args[0] === key) { + query = args.slice(1).join(' '); + template = searchConfig.engines[key]; + } + } + return template.replace('{}', encodeURIComponent(query)); + } +}; + +const mustNumber = (v) => { + let num = Number(v); + if (isNaN(num)) { + throw new Error('Not number: ' + v); + } + return num; +}; + +const parseSetOption = (word, types) => { + let [key, value] = word.split('='); + if (!value) { + value = !key.startsWith('no'); + key = value ? key : key.slice(2); + } + let type = types[key]; + if (!type) { + throw new Error('Unknown property: ' + key); + } + if (type === 'boolean' && typeof value !== 'boolean' || + type !== 'boolean' && typeof value === 'boolean') { + throw new Error('Invalid argument: ' + word); + } + + switch (type) { + case 'string': return [key, value]; + case 'number': return [key, mustNumber(value)]; + case 'boolean': return [key, value]; + } +}; + +const parseCommandLine = (line) => { + let words = line.trim().split(/ +/); + let name = words.shift(); + return [name, words]; +}; + +export { normalizeUrl, parseCommandLine, parseSetOption }; diff --git a/src/shared/commands/properties.js b/src/shared/commands/properties.js deleted file mode 100644 index 8a3213d..0000000 --- a/src/shared/commands/properties.js +++ /dev/null @@ -1,31 +0,0 @@ -const mustNumber = (v) => { - let num = Number(v); - if (isNaN(num)) { - throw new Error('Not number: ' + v); - } - return num; -}; - -const parseProperty = (word, types) => { - let [key, value] = word.split('='); - if (!value) { - value = !key.startsWith('no'); - key = value ? key : key.slice(2); - } - let type = types[key]; - if (!type) { - throw new Error('Unknown property: ' + key); - } - if (type === 'boolean' && typeof value !== 'boolean' || - type !== 'boolean' && typeof value === 'boolean') { - throw new Error('Invalid argument: ' + word); - } - - switch (type) { - case 'string': return [key, value]; - case 'number': return [key, mustNumber(value)]; - case 'boolean': return [key, value]; - } -}; - -export { parseProperty }; diff --git a/test/shared/commands/parsers.test.js b/test/shared/commands/parsers.test.js new file mode 100644 index 0000000..200323c --- /dev/null +++ b/test/shared/commands/parsers.test.js @@ -0,0 +1,78 @@ +import { expect } from "chai"; +import * as parsers from 'shared/commands/parsers'; + +describe("shared/commands/parsers", () => { + describe("#parsers.parseSetOption", () => { + it('parse set string', () => { + let [key, value] = parsers.parseSetOption('encoding=utf-8', { encoding: 'string' }); + expect(key).to.equal('encoding'); + expect(value).to.equal('utf-8'); + }); + + it('parse set string', () => { + let [key, value] = parsers.parseSetOption('history=50', { history: 'number' }); + expect(key).to.equal('history'); + expect(value).to.equal(50); + }); + + it('parse set boolean', () => { + let [key, value] = parsers.parseSetOption('paste', { paste: 'boolean' }); + expect(key).to.equal('paste'); + expect(value).to.be.true; + + [key, value] = parsers.parseSetOption('nopaste', { paste: 'boolean' }); + expect(key).to.equal('paste'); + expect(value).to.be.false; + }); + + it('throws error on unknown property', () => { + expect(() => parsers.parseSetOption('charset=utf-8', {})).to.throw(Error, 'Unknown'); + expect(() => parsers.parseSetOption('smoothscroll', {})).to.throw(Error, 'Unknown'); + expect(() => parsers.parseSetOption('nosmoothscroll', {})).to.throw(Error, 'Unknown'); + }) + + it('throws error on invalid property', () => { + expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'number' })).to.throw(Error, 'Not number'); + expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'boolean' })).to.throw(Error, 'Invalid'); + expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'string' })).to.throw(Error, 'Invalid'); + expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'number' })).to.throw(Error, 'Invalid'); + }) + }); + + describe('#normalizeUrl', () => { + const config = { + default: 'google', + engines: { + google: 'https://google.com/search?q={}', + yahoo: 'https://yahoo.com/search?q={}', + } + }; + + it('convertes search url', () => { + expect(parsers.normalizeUrl(['google', 'apple'], config)) + .to.equal('https://google.com/search?q=apple'); + expect(parsers.normalizeUrl(['yahoo', 'apple'], config)) + .to.equal('https://yahoo.com/search?q=apple'); + expect(parsers.normalizeUrl(['google', 'apple', 'banana'], config)) + .to.equal('https://google.com/search?q=apple%20banana'); + expect(parsers.normalizeUrl(['yahoo', 'C++CLI'], config)) + .to.equal('https://yahoo.com/search?q=C%2B%2BCLI'); + }); + + it('user default search engine', () => { + expect(parsers.normalizeUrl(['apple', 'banana'], config)) + .to.equal('https://google.com/search?q=apple%20banana'); + }); + }); + + describe('#parseCommandLine', () => { + it('parse command line as name and args', () => { + expect(parsers.parseCommandLine('open google apple')).to.deep.equal(['open', ['google', 'apple']]); + expect(parsers.parseCommandLine(' open google apple ')).to.deep.equal(['open', ['google', 'apple']]); + expect(parsers.parseCommandLine('')).to.deep.equal(['', []]); + expect(parsers.parseCommandLine(' ')).to.deep.equal(['', []]); + expect(parsers.parseCommandLine('exit')).to.deep.equal(['exit', []]); + expect(parsers.parseCommandLine(' exit ')).to.deep.equal(['exit', []]); + }); + }); +}); diff --git a/test/shared/commands/property.test.js b/test/shared/commands/property.test.js deleted file mode 100644 index d949482..0000000 --- a/test/shared/commands/property.test.js +++ /dev/null @@ -1,41 +0,0 @@ -import { expect } from "chai"; -import { parseProperty } from 'shared/commands/properties'; - -describe("shared/commands/properties", () => { - describe("#parseProperty", () => { - it('parse set string', () => { - let [key, value] = parseProperty('encoding=utf-8', { encoding: 'string' }); - expect(key).to.equal('encoding'); - expect(value).to.equal('utf-8'); - }); - - it('parse set string', () => { - let [key, value] = parseProperty('history=50', { history: 'number' }); - expect(key).to.equal('history'); - expect(value).to.equal(50); - }); - - it('parse set boolean', () => { - let [key, value] = parseProperty('paste', { paste: 'boolean' }); - expect(key).to.equal('paste'); - expect(value).to.be.true; - - [key, value] = parseProperty('nopaste', { paste: 'boolean' }); - expect(key).to.equal('paste'); - expect(value).to.be.false; - }); - - it('throws error on unknown property', () => { - expect(() => parseProperty('charset=utf-8', {})).to.throw(Error, 'Unknown'); - expect(() => parseProperty('smoothscroll', {})).to.throw(Error, 'Unknown'); - expect(() => parseProperty('nosmoothscroll', {})).to.throw(Error, 'Unknown'); - }) - - it('throws error on invalid property', () => { - expect(() => parseProperty('charset=utf-8', { charset: 'number' })).to.throw(Error, 'Not number'); - expect(() => parseProperty('charset=utf-8', { charset: 'boolean' })).to.throw(Error, 'Invalid'); - expect(() => parseProperty('smoothscroll', { smoothscroll: 'string' })).to.throw(Error, 'Invalid'); - expect(() => parseProperty('smoothscroll', { smoothscroll: 'number' })).to.throw(Error, 'Invalid'); - }) - }); -}); -- cgit v1.2.3 From fad8f96a663d83792138cc986474ec4228b6c6c9 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 11 Jan 2018 20:22:49 +0900 Subject: implement set option --- src/background/actions/command.js | 17 +++++++++++++++++ src/background/components/background.js | 11 +++++------ 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/command.js b/src/background/actions/command.js index f11c61b..4c52bca 100644 --- a/src/background/actions/command.js +++ b/src/background/actions/command.js @@ -1,5 +1,7 @@ +import actions from '../actions'; import * as tabs from 'background/tabs'; import * as parsers from 'shared/commands/parsers'; +import * as properties from 'shared/settings/properties'; const openCommand = (url) => { return browser.tabs.query({ @@ -37,6 +39,19 @@ const bufferCommand = (keywords) => { }); }; +const setCommand = (args) => { + if (!args[0]) { + return Promise.resolve(); + } + + let [name, value] = parsers.parseSetOption(args[0], properties.types); + return { + type: actions.SETTING_SET_PROPERTY, + name, + value + }; +}; + const exec = (line, settings) => { let [name, args] = parsers.parseCommandLine(line); @@ -53,6 +68,8 @@ const exec = (line, settings) => { case 'b': case 'buffer': return bufferCommand(args); + case 'set': + return setCommand(args); case '': return Promise.resolve(); } diff --git a/src/background/components/background.js b/src/background/components/background.js index 19bf27f..9578e78 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -36,12 +36,11 @@ export default class BackgroundComponent { return this.store.dispatch( tabActions.openToTab(message.url, sender.tab), sender); case messages.CONSOLE_ENTER_COMMAND: - return commandActions.exec(message.text, settings.value).catch((e) => { - return browser.tabs.sendMessage(sender.tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text: e.message, - }); - }); + this.store.dispatch( + commandActions.exec(message.text, settings.value), + sender + ); + return this.broadcastSettingsChanged(); case messages.SETTINGS_QUERY: return Promise.resolve(this.store.getState().setting.value); case messages.CONSOLE_QUERY_COMPLETIONS: -- cgit v1.2.3 From e7dcd7f500f9c139835313a573f861a10aa49d18 Mon Sep 17 00:00:00 2001 From: Cornelius Matějka Date: Wed, 22 Nov 2017 19:22:30 +0100 Subject: Pinned tabs are not closeable by 'd' Added binding 'DD' to force tab close which also closes pinned tabs --- src/background/actions/operation.js | 2 ++ src/background/tabs.js | 15 ++++++++++++--- src/shared/operations.js | 1 + src/shared/settings/default.js | 1 + 4 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/operation.js b/src/background/actions/operation.js index cfee868..1188ea2 100644 --- a/src/background/actions/operation.js +++ b/src/background/actions/operation.js @@ -17,6 +17,8 @@ const exec = (operation, tab) => { switch (operation.type) { case operations.TAB_CLOSE: return tabs.closeTab(tab.id); + case operations.TAB_CLOSE_FORCE: + return tabs.closeTabForce(tab.id); case operations.TAB_REOPEN: return tabs.reopenTab(); case operations.TAB_PREV: diff --git a/src/background/tabs.js b/src/background/tabs.js index d50d8e5..e02932a 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -9,6 +9,14 @@ browser.tabs.onActivated.addListener((activeInfo) => { }); const closeTab = (id) => { + return browser.tabs.get(id).then((tab) => { + if(!tab.pinned) { + return browser.tabs.remove(id); + } + }) +}; + +const closeTabForce = (id) => { return browser.tabs.remove(id); }; @@ -130,7 +138,8 @@ const duplicate = (id) => { }; export { - closeTab, reopenTab, selectAt, selectByKeyword, getCompletions, - selectPrevTab, selectNextTab, selectFirstTab, selectLastTab, selectPrevSelTab, - reload, updateTabPinned, toggleTabPinned, duplicate + closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword, + getCompletions, selectPrevTab, selectNextTab, selectFirstTab, + selectLastTab, selectPrevSelTab, reload, updateTabPinned, + toggleTabPinned, duplicate }; diff --git a/src/shared/operations.js b/src/shared/operations.js index 19466df..4172f8b 100644 --- a/src/shared/operations.js +++ b/src/shared/operations.js @@ -33,6 +33,7 @@ export default { // Tabs TAB_CLOSE: 'tabs.close', + TAB_CLOSE_FORCE: 'tabs.close.force', TAB_REOPEN: 'tabs.reopen', TAB_PREV: 'tabs.prev', TAB_NEXT: 'tabs.next', diff --git a/src/shared/settings/default.js b/src/shared/settings/default.js index e81df2b..826e3f6 100644 --- a/src/shared/settings/default.js +++ b/src/shared/settings/default.js @@ -23,6 +23,7 @@ export default { "G": { "type": "scroll.bottom" }, "$": { "type": "scroll.end" }, "d": { "type": "tabs.close" }, + "DD": { "type": "tabs.close.force" }, "u": { "type": "tabs.reopen" }, "K": { "type": "tabs.prev", "count": 1 }, "J": { "type": "tabs.next", "count": 1 }, -- cgit v1.2.3 From ba2cd311e158152d961e39f84f29fefc872cc594 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 20 Feb 2018 21:09:05 +0900 Subject: set opener tab id --- src/background/actions/tab.js | 4 ++-- src/background/components/background.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/tab.js b/src/background/actions/tab.js index e512b6f..40da55d 100644 --- a/src/background/actions/tab.js +++ b/src/background/actions/tab.js @@ -1,5 +1,5 @@ -const openNewTab = (url) => { - return browser.tabs.create({ url: url }); +const openNewTab = (url, openerTabId) => { + return browser.tabs.create({ url, openerTabId }); }; const openToTab = (url, tab) => { diff --git a/src/background/components/background.js b/src/background/components/background.js index 9578e78..c4f436a 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -31,7 +31,7 @@ export default class BackgroundComponent { case messages.OPEN_URL: if (message.newTab) { return this.store.dispatch( - tabActions.openNewTab(message.url), sender); + tabActions.openNewTab(message.url, sender.tab.id), sender); } return this.store.dispatch( tabActions.openToTab(message.url, sender.tab), sender); -- cgit v1.2.3 From 90b83d7b7b99598834466c97e9c74c82cbd25cf3 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 4 Mar 2018 18:32:24 +0900 Subject: hide console on and --- src/background/actions/operation.js | 4 ++++ src/content/actions/setting.js | 9 ++++++++- src/shared/messages.js | 1 + src/shared/operations.js | 3 +++ test/background/reducers/setting.test.js | 1 - test/content/actions/setting.test.js | 2 ++ 6 files changed, 18 insertions(+), 2 deletions(-) (limited to 'src/background/actions') diff --git a/src/background/actions/operation.js b/src/background/actions/operation.js index 1188ea2..56eb168 100644 --- a/src/background/actions/operation.js +++ b/src/background/actions/operation.js @@ -73,6 +73,10 @@ const exec = (operation, tab) => { return browser.tabs.sendMessage(tab.id, { type: messages.CONSOLE_SHOW_FIND }); + case operations.CANCEL: + return browser.tabs.sendMessage(tab.id, { + type: messages.CONSOLE_HIDE, + }); default: return Promise.resolve(); } diff --git a/src/content/actions/setting.js b/src/content/actions/setting.js index 0238c71..4c1e385 100644 --- a/src/content/actions/setting.js +++ b/src/content/actions/setting.js @@ -1,10 +1,17 @@ import actions from 'content/actions'; import * as keyUtils from 'shared/utils/keys'; +import operations from 'shared/operations'; + +const reservedKeymaps = { + '': { type: operations.CANCEL }, + '': { type: operations.CANCEL }, +}; const set = (value) => { let entries = []; if (value.keymaps) { - entries = Object.entries(value.keymaps).map((entry) => { + let keymaps = Object.assign({}, value.keymaps, reservedKeymaps); + entries = Object.entries(keymaps).map((entry) => { return [ keyUtils.fromMapKeys(entry[0]), entry[1], diff --git a/src/shared/messages.js b/src/shared/messages.js index de00a3f..b7a1a7e 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -32,6 +32,7 @@ export default { CONSOLE_SHOW_ERROR: 'console.show.error', CONSOLE_SHOW_INFO: 'console.show.info', CONSOLE_SHOW_FIND: 'console.show.find', + CONSOLE_HIDE: 'console.hide', FOLLOW_START: 'follow.start', FOLLOW_REQUEST_COUNT_TARGETS: 'follow.request.count.targets', diff --git a/src/shared/operations.js b/src/shared/operations.js index 008e9eb..a2f980f 100644 --- a/src/shared/operations.js +++ b/src/shared/operations.js @@ -1,4 +1,7 @@ export default { + // Hide console, or cancel some user actions + CANCEL: 'cancel', + // Addons ADDON_ENABLE: 'addon.enable', ADDON_DISABLE: 'addon.disable', diff --git a/test/background/reducers/setting.test.js b/test/background/reducers/setting.test.js index 2ef98cb..8df5abe 100644 --- a/test/background/reducers/setting.test.js +++ b/test/background/reducers/setting.test.js @@ -30,7 +30,6 @@ describe("setting reducer", () => { }; state = settingReducer(state, action); - console.log(state); expect(state.value.properties).to.have.property('smoothscroll', true); expect(state.value.properties).to.have.property('encoding', 'utf-8'); }); diff --git a/test/content/actions/setting.test.js b/test/content/actions/setting.test.js index 1248edf..3112b2d 100644 --- a/test/content/actions/setting.test.js +++ b/test/content/actions/setting.test.js @@ -23,6 +23,8 @@ describe("setting actions", () => { let map = new Map(keymaps); expect(map).to.have.deep.all.keys( [ + [{ key: 'Esc', shiftKey: false, ctrlKey: false, altKey: false, metaKey: false }], + [{ key: '[', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false }], [{ key: 'd', shiftKey: false, ctrlKey: false, altKey: false, metaKey: false }, { key: 'd', shiftKey: false, ctrlKey: false, altKey: false, metaKey: false }], [{ key: 'z', shiftKey: false, ctrlKey: false, altKey: false, metaKey: false }, -- cgit v1.2.3 From 24e72aa6e079009d4c24c7f6be6bfb82c21e6643 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 7 Mar 2018 20:54:28 +0900 Subject: add global find keyword --- src/background/actions/find.js | 10 ++++++++++ src/background/actions/index.js | 3 +++ src/background/components/background.js | 8 ++++++++ src/background/reducers/find.js | 16 ++++++++++++++++ src/background/reducers/index.js | 3 +++ src/shared/messages.js | 2 ++ test/background/actions/find.test.js | 13 +++++++++++++ test/background/reducers/find.test.js | 19 +++++++++++++++++++ 8 files changed, 74 insertions(+) create mode 100644 src/background/actions/find.js create mode 100644 src/background/reducers/find.js create mode 100644 test/background/actions/find.test.js create mode 100644 test/background/reducers/find.test.js (limited to 'src/background/actions') diff --git a/src/background/actions/find.js b/src/background/actions/find.js new file mode 100644 index 0000000..8da5572 --- /dev/null +++ b/src/background/actions/find.js @@ -0,0 +1,10 @@ +import actions from './index'; + +const setKeyword = (keyword) => { + return { + type: actions.FIND_SET_KEYWORD, + keyword, + }; +}; + +export { setKeyword }; diff --git a/src/background/actions/index.js b/src/background/actions/index.js index efe4074..2bdaaf2 100644 --- a/src/background/actions/index.js +++ b/src/background/actions/index.js @@ -2,4 +2,7 @@ export default { // Settings SETTING_SET_SETTINGS: 'setting.set.settings', SETTING_SET_PROPERTY: 'setting.set.property', + + // Find + FIND_SET_KEYWORD: 'find.set.keyword', }; diff --git a/src/background/components/background.js b/src/background/components/background.js index c4f436a..fdec8ec 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -2,6 +2,7 @@ import messages from 'shared/messages'; import * as operationActions from 'background/actions/operation'; import * as commandActions from 'background/actions/command'; import * as settingActions from 'background/actions/setting'; +import * as findActions from 'background/actions/find'; import * as tabActions from 'background/actions/tab'; import * as commands from 'shared/commands'; @@ -23,6 +24,8 @@ export default class BackgroundComponent { onMessage(message, sender) { let settings = this.store.getState().setting; + let find = this.store.getState().find; + switch (message.type) { case messages.BACKGROUND_OPERATION: return this.store.dispatch( @@ -48,6 +51,11 @@ export default class BackgroundComponent { case messages.SETTINGS_RELOAD: this.store.dispatch(settingActions.load()); return this.broadcastSettingsChanged(); + case messages.FIND_GET_KEYWORD: + return Promise.resolve(find.keyword); + case messages.FIND_SET_KEYWORD: + this.store.dispatch(findActions.setKeyword(message.keyword)); + return Promise.resolve({}); } } diff --git a/src/background/reducers/find.js b/src/background/reducers/find.js new file mode 100644 index 0000000..4ded801 --- /dev/null +++ b/src/background/reducers/find.js @@ -0,0 +1,16 @@ +import actions from 'content/actions'; + +const defaultState = { + keyword: null, +}; + +export default function reducer(state = defaultState, action = {}) { + switch (action.type) { + case actions.FIND_SET_KEYWORD: + return Object.assign({}, state, { + keyword: action.keyword, + }); + default: + return state; + } +} diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js index dab0c62..63ff0f8 100644 --- a/src/background/reducers/index.js +++ b/src/background/reducers/index.js @@ -1,12 +1,15 @@ import settingReducer from './setting'; +import findReducer from './find'; // Make setting reducer instead of re-use const defaultState = { setting: settingReducer(undefined, {}), + find: findReducer(undefined, {}), }; export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { setting: settingReducer(state.setting, action), + find: findReducer(state.find, action), }); } diff --git a/src/shared/messages.js b/src/shared/messages.js index b7a1a7e..a404658 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -45,6 +45,8 @@ export default { FIND_NEXT: 'find.next', FIND_PREV: 'find.prev', + FIND_GET_KEYWORD: 'find.get.keyword', + FIND_SET_KEYWORD: 'find.set.keyword', OPEN_URL: 'open.url', diff --git a/test/background/actions/find.test.js b/test/background/actions/find.test.js new file mode 100644 index 0000000..467604f --- /dev/null +++ b/test/background/actions/find.test.js @@ -0,0 +1,13 @@ +import { expect } from "chai"; +import actions from 'background/actions'; +import * as findActions from 'background/actions/find'; + +describe("find actions", () => { + describe("setKeyword", () => { + it('create FIND_SET_KEYWORD action', () => { + let action = findActions.setKeyword('banana'); + expect(action.type).to.equal(actions.FIND_SET_KEYWORD); + expect(action.keyword).to.equal('banana'); + }); + }); +}); diff --git a/test/background/reducers/find.test.js b/test/background/reducers/find.test.js new file mode 100644 index 0000000..707d73a --- /dev/null +++ b/test/background/reducers/find.test.js @@ -0,0 +1,19 @@ +import { expect } from "chai"; +import actions from 'background/actions'; +import findReducer from 'background/reducers/find'; + +describe("find reducer", () => { + it('return the initial state', () => { + let state = findReducer(undefined, {}); + expect(state).to.have.deep.property('keyword', null); + }); + + it('return next state for FIND_SET_KEYWORD', () => { + let action = { + type: actions.FIND_SET_KEYWORD, + keyword: 'cherry', + }; + let state = findReducer(undefined, action); + expect(state).to.have.deep.property('keyword', 'cherry') + }); +}); -- cgit v1.2.3