diff options
Diffstat (limited to 'src')
38 files changed, 412 insertions, 481 deletions
diff --git a/src/background/actions/command.js b/src/background/actions/command.js index 6d83450..fb8ff98 100644 --- a/src/background/actions/command.js +++ b/src/background/actions/command.js @@ -5,26 +5,24 @@ import * as bookmarks from '../shared/bookmarks'; import * as parsers from 'shared/commands/parsers'; import * as properties from 'shared/settings/properties'; -const openCommand = (url) => { - return browser.tabs.query({ +const openCommand = async(url) => { + let got = await browser.tabs.query({ active: true, currentWindow: true - }).then((gotTabs) => { - if (gotTabs.length > 0) { - return browser.tabs.update(gotTabs[0].id, { url: url }); - } }); + if (got.length > 0) { + return browser.tabs.update(got[0].id, { url: url }); + } }; const tabopenCommand = (url) => { return browser.tabs.create({ url: url }); }; -const tabcloseCommand = () => { - return browser.tabs.query({ +const tabcloseCommand = async() => { + let got = await browser.tabs.query({ active: true, currentWindow: true - }).then((tabList) => { - return browser.tabs.remove(tabList.map(tab => tab.id)); }); + return browser.tabs.remove(got.map(tab => tab.id)); }; const tabcloseAllCommand = () => { @@ -39,30 +37,39 @@ const winopenCommand = (url) => { return browser.windows.create({ url }); }; -const bufferCommand = (keywords) => { +const bufferCommand = async(keywords) => { if (keywords.length === 0) { return Promise.resolve([]); } let keywordsStr = keywords.join(' '); - return browser.tabs.query({ + let got = await 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); - } }); + if (got.length === 0) { + return; + } + if (isNaN(keywordsStr)) { + return tabs.selectByKeyword(got[0], keywordsStr); + } + let index = parseInt(keywordsStr, 10) - 1; + return tabs.selectAt(index); }; -const addBookmarkCommand = (tab, args) => { +const addbookmarkCommand = async(tab, args) => { if (!args[0]) { - return Promise.resolve(); + return; } - - return bookmarks.create(args.join(' '), tab.url); + let item = await bookmarks.create(args.join(' '), tab.url); + if (!item) { + return browser.tabs.sendMessage(tab.id, { + type: messages.CONSOLE_SHOW_ERROR, + text: 'Could not create a bookmark', + }); + } + return browser.tabs.sendMessage(tab.id, { + type: messages.CONSOLE_SHOW_INFO, + text: 'Saved current page: ' + item.url, + }); }; const setCommand = (args) => { @@ -108,18 +115,7 @@ const exec = (tab, line, settings) => { case 'bdeletes!': return tabs.closeTabsByKeywordsForce(args.join(' ')); case 'addbookmark': - return addBookmarkCommand(tab, args).then((item) => { - if (!item) { - return browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text: 'Could not create a bookmark', - }); - } - return browser.tabs.sendMessage(tab.id, { - type: messages.CONSOLE_SHOW_INFO, - text: 'Saved current page: ' + item.url, - }); - }); + return addbookmarkCommand(tab, args); case 'set': return setCommand(args); case 'q': diff --git a/src/background/actions/setting.js b/src/background/actions/setting.js index 773142f..7eeb5de 100644 --- a/src/background/actions/setting.js +++ b/src/background/actions/setting.js @@ -1,13 +1,12 @@ 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, - }; - }); +const load = async() => { + let value = await settingsStorage.loadValue(); + return { + type: actions.SETTING_SET_SETTINGS, + value, + }; }; const setProperty = (name, value) => { diff --git a/src/background/actions/tab.js b/src/background/actions/tab.js index 0d439fd..5cf1e8c 100644 --- a/src/background/actions/tab.js +++ b/src/background/actions/tab.js @@ -1,19 +1,20 @@ import actions from './index'; -const openNewTab = (url, openerTabId, background = false, adjacent = false) => { - if (adjacent) { - return browser.tabs.query({ - active: true, currentWindow: true - }).then((tabs) => { - return browser.tabs.create({ - url, - openerTabId, - active: !background, - index: tabs[0].index + 1 - }); - }); +const openNewTab = async( + url, openerTabId, background = false, adjacent = false +) => { + if (!adjacent) { + return browser.tabs.create({ url, active: !background }); } - return browser.tabs.create({ url, active: !background }); + let tabs = await browser.tabs.query({ + active: true, currentWindow: true + }); + return browser.tabs.create({ + url, + openerTabId, + active: !background, + index: tabs[0].index + 1 + }); }; const openToTab = (url, tab) => { diff --git a/src/background/components/background.js b/src/background/components/background.js index 29124a6..c7a79a1 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -56,13 +56,12 @@ export default class BackgroundComponent { } } - broadcastSettingsChanged() { - return browser.tabs.query({}).then((tabs) => { - for (let tab of tabs) { - browser.tabs.sendMessage(tab.id, { - type: messages.SETTINGS_CHANGED, - }); - } - }); + async broadcastSettingsChanged() { + let tabs = await browser.tabs.query({}); + for (let tab of tabs) { + browser.tabs.sendMessage(tab.id, { + type: messages.SETTINGS_CHANGED, + }); + } } } diff --git a/src/background/components/indicator.js b/src/background/components/indicator.js index cceb119..1ded329 100644 --- a/src/background/components/indicator.js +++ b/src/background/components/indicator.js @@ -8,19 +8,17 @@ export default class IndicatorComponent { messages.onMessage(this.onMessage.bind(this)); browser.browserAction.onClicked.addListener(this.onClicked); - browser.tabs.onActivated.addListener((info) => { - return browser.tabs.query({ currentWindow: true }).then(() => { - return this.onTabActivated(info); - }); + browser.tabs.onActivated.addListener(async(info) => { + await browser.tabs.query({ currentWindow: true }); + return this.onTabActivated(info); }); } - onTabActivated(info) { - return browser.tabs.sendMessage(info.tabId, { + async onTabActivated(info) { + let { enabled } = await browser.tabs.sendMessage(info.tabId, { type: messages.ADDON_ENABLED_QUERY, - }).then((resp) => { - return this.updateIndicator(resp.enabled); }); + return this.updateIndicator(enabled); } onClicked(tab) { diff --git a/src/background/components/operation.js b/src/background/components/operation.js index 58edb8c..465baf0 100644 --- a/src/background/components/operation.js +++ b/src/background/components/operation.js @@ -28,7 +28,7 @@ export default class BackgroundComponent { } } - // eslint-disable-next-line complexity + // eslint-disable-next-line complexity, max-lines-per-function exec(operation, tab) { let tabState = this.store.getState().tab; diff --git a/src/background/components/tab.js b/src/background/components/tab.js index b273546..6af3fd7 100644 --- a/src/background/components/tab.js +++ b/src/background/components/tab.js @@ -4,10 +4,9 @@ export default class TabComponent { constructor(store) { this.store = store; - browser.tabs.onActivated.addListener((info) => { - return browser.tabs.query({ currentWindow: true }).then(() => { - return this.onTabActivated(info); - }); + browser.tabs.onActivated.addListener(async(info) => { + await browser.tabs.query({ currentWindow: true }); + return this.onTabActivated(info); }); } diff --git a/src/background/index.js b/src/background/index.js index 3f1013c..02de53f 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -18,6 +18,15 @@ const store = createStore(reducers, (e, sender) => { } }); +const checkAndNotifyUpdated = async() => { + let updated = await versions.checkUpdated(); + if (!updated) { + return; + } + await versions.notify(); + await versions.commit(); +}; + /* eslint-disable no-unused-vars */ const backgroundComponent = new BackgroundComponent(store); const operationComponent = new OperationComponent(store); @@ -27,11 +36,4 @@ const indicatorComponent = new IndicatorComponent(store); store.dispatch(settingActions.load()); -versions.checkUpdated().then((updated) => { - if (!updated) { - return; - } - return versions.notify(); -}).then(() => { - return versions.commit(); -}); +checkAndNotifyUpdated(); diff --git a/src/background/reducers/find.js b/src/background/reducers/find.js index 4ded801..bbc6b36 100644 --- a/src/background/reducers/find.js +++ b/src/background/reducers/find.js @@ -7,9 +7,8 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.FIND_SET_KEYWORD: - return Object.assign({}, state, { - keyword: action.keyword, - }); + return { ...state, + keyword: action.keyword, }; default: return state; } diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js index 5729f0a..78f855c 100644 --- a/src/background/reducers/index.js +++ b/src/background/reducers/index.js @@ -10,9 +10,8 @@ const defaultState = { }; export default function reducer(state = defaultState, action = {}) { - return Object.assign({}, state, { + return { ...state, setting: settingReducer(state.setting, action), find: findReducer(state.find, action), - tab: tabReducer(state.tab, action), - }); + tab: tabReducer(state.tab, action), }; } diff --git a/src/background/reducers/setting.js b/src/background/reducers/setting.js index 045a654..8dbc1b4 100644 --- a/src/background/reducers/setting.js +++ b/src/background/reducers/setting.js @@ -12,10 +12,8 @@ export default function reducer(state = defaultState, action = {}) { }; case actions.SETTING_SET_PROPERTY: return { - value: Object.assign({}, state.value, { - properties: Object.assign({}, state.value.properties, - { [action.name]: action.value }) - }) + value: { ...state.value, + properties: { ...state.value.properties, [action.name]: action.value }} }; default: return state; diff --git a/src/background/shared/completions/bookmarks.js b/src/background/shared/completions/bookmarks.js index 1adb350..bd753af 100644 --- a/src/background/shared/completions/bookmarks.js +++ b/src/background/shared/completions/bookmarks.js @@ -1,15 +1,14 @@ -const getCompletions = (keywords) => { - return browser.bookmarks.search({ query: keywords }).then((items) => { - return items.filter((item) => { - let url = undefined; - try { - url = new URL(item.url); - } catch (e) { - return false; - } - return item.type === 'bookmark' && url.protocol !== 'place:'; - }).slice(0, 10); - }); +const getCompletions = async(keywords) => { + let items = await browser.bookmarks.search({ query: keywords }); + return items.filter((item) => { + let url = undefined; + try { + url = new URL(item.url); + } catch (e) { + return false; + } + return item.type === 'bookmark' && url.protocol !== 'place:'; + }).slice(0, 10); }; export { getCompletions }; diff --git a/src/background/shared/completions/histories.js b/src/background/shared/completions/histories.js index a7d3d47..2d35401 100644 --- a/src/background/shared/completions/histories.js +++ b/src/background/shared/completions/histories.js @@ -61,23 +61,22 @@ const reduceByOrigin = (items, min) => { return filtered; }; -const getCompletions = (keyword) => { - return browser.history.search({ +const getCompletions = async(keyword) => { + let historyItems = await browser.history.search({ text: keyword, startTime: 0, - }).then((historyItems) => { - return [historyItems.map(item => [item, new URL(item.url)])] - .map(filterEmptyTitle) - .map(filterHttp) - .map(filterClosedPath) - .map(items => reduceByPathname(items, 10)) - .map(items => reduceByOrigin(items, 10)) - .map(items => items - .sort((x, y) => x[0].visitCount < y[0].visitCount) - .slice(0, 10) - .map(item => item[0]) - )[0]; }); + return [historyItems.map(item => [item, new URL(item.url)])] + .map(filterEmptyTitle) + .map(filterHttp) + .map(filterClosedPath) + .map(items => reduceByPathname(items, 10)) + .map(items => reduceByOrigin(items, 10)) + .map(items => items + .sort((x, y) => x[0].visitCount < y[0].visitCount) + .slice(0, 10) + .map(item => item[0]) + )[0]; }; export { getCompletions }; diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js index 728cee7..d5875fe 100644 --- a/src/background/shared/completions/index.js +++ b/src/background/shared/completions/index.js @@ -12,76 +12,66 @@ const getSearchCompletions = (command, keywords, searchConfig) => { return Promise.resolve(engineItems); }; -const getHistoryCompletions = (command, keywords) => { - return histories.getCompletions(keywords).then((pages) => { - return pages.map((page) => { - return { - caption: page.title, - content: command + ' ' + page.url, - url: page.url - }; - }); +const getHistoryCompletions = async(command, keywords) => { + let items = await histories.getCompletions(keywords); + return items.map((page) => { + return { + caption: page.title, + content: command + ' ' + page.url, + url: page.url + }; }); }; -const getBookmarksCompletions = (command, keywords) => { - return bookmarks.getCompletions(keywords).then((items) => { - return items.map((item) => { - return { - caption: item.title, - content: command + ' ' + item.url, - url: item.url, - }; - }); - }); +const getBookmarksCompletions = async(command, keywords) => { + let items = await bookmarks.getCompletions(keywords); + return items.map(item => ({ + caption: item.title, + content: command + ' ' + item.url, + url: item.url, + })); }; -const getOpenCompletions = (command, keywords, searchConfig) => { - return Promise.all([ - getSearchCompletions(command, keywords, searchConfig), - getHistoryCompletions(command, keywords), - getBookmarksCompletions(command, keywords), - ]).then(([engineItems, historyItems, bookmarkItems]) => { - let completions = []; - if (engineItems.length > 0) { - completions.push({ - name: 'Search Engines', - items: engineItems - }); - } - if (historyItems.length > 0) { - completions.push({ - name: 'History', - items: historyItems - }); - } - if (bookmarkItems.length > 0) { - completions.push({ - name: 'Bookmarks', - items: bookmarkItems - }); - } - return completions; - }); +const getOpenCompletions = async(command, keywords, searchConfig) => { + let engineItems = await getSearchCompletions(command, keywords, searchConfig); + let historyItems = await getHistoryCompletions(command, keywords); + let bookmarkItems = await getBookmarksCompletions(command, keywords); + let completions = []; + if (engineItems.length > 0) { + completions.push({ + name: 'Search Engines', + items: engineItems + }); + } + if (historyItems.length > 0) { + completions.push({ + name: 'History', + items: historyItems + }); + } + if (bookmarkItems.length > 0) { + completions.push({ + name: 'Bookmarks', + items: bookmarkItems + }); + } + return completions; }; -const getBufferCompletions = (command, keywords, excludePinned) => { - return tabs.getCompletions(keywords, excludePinned).then((got) => { - let items = got.map((tab) => { - return { - caption: tab.title, - content: command + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl - }; - }); - return [ - { - name: 'Buffers', - items: items - } - ]; - }); +const getBufferCompletions = async(command, keywords, excludePinned) => { + let items = await tabs.getCompletions(keywords, excludePinned); + items = items.map(tab => ({ + caption: tab.title, + content: command + ' ' + tab.title, + url: tab.url, + icon: tab.favIconUrl + })); + return [ + { + name: 'Buffers', + items: items + } + ]; }; const getCompletions = (line, settings) => { diff --git a/src/background/shared/completions/tabs.js b/src/background/shared/completions/tabs.js index 8c0f1f9..bdb2741 100644 --- a/src/background/shared/completions/tabs.js +++ b/src/background/shared/completions/tabs.js @@ -1,12 +1,8 @@ +import * as tabs from '../tabs'; + const getCompletions = (keyword, excludePinned) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let matched = tabs.filter((t) => { - return t.url.includes(keyword) || t.title && t.title.includes(keyword); - }).filter((t) => { - return !(excludePinned && t.pinned); - }); - return matched; - }); + return tabs.queryByKeyword(keyword, excludePinned); }; + export { getCompletions }; diff --git a/src/background/shared/tabs.js b/src/background/shared/tabs.js index d09f676..26e2e44 100644 --- a/src/background/shared/tabs.js +++ b/src/background/shared/tabs.js @@ -1,141 +1,127 @@ import * as tabCompletions from './completions/tabs'; -const closeTab = (id) => { - return browser.tabs.get(id).then((tab) => { - if (!tab.pinned) { - return browser.tabs.remove(id); - } - }); +const closeTab = async(id) => { + let tab = await browser.tabs.get(id); + if (!tab.pinned) { + return browser.tabs.remove(id); + } }; const closeTabForce = (id) => { return browser.tabs.remove(id); }; -const closeTabByKeywords = (keyword) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let matched = tabs.filter((t) => { - return t.url.includes(keyword) || t.title.includes(keyword); - }).filter(t => !t.pinned); - - if (matched.length === 0) { - throw new Error('No matching buffer for ' + keyword); - } else if (matched.length > 1) { - throw new Error('More than one match for ' + keyword); - } - browser.tabs.remove(matched[0].id); +const queryByKeyword = async(keyword, excludePinned = false) => { + let tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.filter((t) => { + return t.url.toLowerCase().includes(keyword.toLowerCase()) || + t.title && t.title.toLowerCase().includes(keyword.toLowerCase()); + }).filter((t) => { + return !(excludePinned && t.pinned); }); }; -const closeTabByKeywordsForce = (keyword) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let matched = tabs.filter((t) => { - return t.url.includes(keyword) || t.title.includes(keyword); - }); - - if (matched.length === 0) { - throw new Error('No matching buffer for ' + keyword); - } else if (matched.length > 1) { - throw new Error('More than one match for ' + keyword); - } - browser.tabs.remove(matched[0].id); - }); +const closeTabByKeywords = async(keyword) => { + let tabs = await queryByKeyword(keyword, true); + if (tabs.length === 0) { + throw new Error('No matching buffer for ' + keyword); + } else if (tabs.length > 1) { + throw new Error('More than one match for ' + keyword); + } + return browser.tabs.remove(tabs[0].id); }; - -const closeTabsByKeywords = (keyword) => { - tabCompletions.getCompletions(keyword).then((tabs) => { - let tabs2 = tabs.filter(tab => !tab.pinned); - browser.tabs.remove(tabs2.map(tab => tab.id)); - }); +const closeTabByKeywordsForce = async(keyword) => { + let tabs = await queryByKeyword(keyword, false); + if (tabs.length === 0) { + throw new Error('No matching buffer for ' + keyword); + } else if (tabs.length > 1) { + throw new Error('More than one match for ' + keyword); + } + return browser.tabs.remove(tabs[0].id); }; -const closeTabsByKeywordsForce = (keyword) => { - tabCompletions.getCompletions(keyword).then((tabs) => { - browser.tabs.remove(tabs.map(tab => tab.id)); - }); +const closeTabsByKeywords = async(keyword) => { + let tabs = await tabCompletions.getCompletions(keyword); + tabs = tabs.filter(tab => !tab.pinned); + return browser.tabs.remove(tabs.map(tab => tab.id)); }; -const reopenTab = () => { - return browser.sessions.getRecentlyClosed({ - maxResults: 1 - }).then((sessions) => { - if (sessions.length === 0) { - return; - } - let session = sessions[0]; - if (session.tab) { - return browser.sessions.restore(session.tab.sessionId); - } - return browser.sessions.restore(session.window.sessionId); - }); +const closeTabsByKeywordsForce = async(keyword) => { + let tabs = await tabCompletions.getCompletions(keyword); + return browser.tabs.remove(tabs.map(tab => tab.id)); }; -const selectAt = (index) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - if (tabs.length < 2) { - return; - } - if (index < 0 || tabs.length <= index) { - throw new RangeError(`tab ${index + 1} does not exist`); - } - let id = tabs[index].id; - return browser.tabs.update(id, { active: true }); +const reopenTab = async() => { + let window = await browser.windows.getCurrent(); + let sessions = await browser.sessions.getRecentlyClosed(); + let session = sessions.find((s) => { + return s.tab && s.tab.windowId === window.id; }); + if (!session) { + return; + } + if (session.tab) { + return browser.sessions.restore(session.tab.sessionId); + } + return browser.sessions.restore(session.window.sessionId); +}; + +const selectAt = async(index) => { + let tabs = await browser.tabs.query({ currentWindow: true }); + if (tabs.length < 2) { + return; + } + if (index < 0 || tabs.length <= index) { + throw new RangeError(`tab ${index + 1} does not exist`); + } + let id = tabs[index].id; + return browser.tabs.update(id, { active: true }); }; -const selectByKeyword = (current, keyword) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let matched = tabs.filter((t) => { - return t.url.includes(keyword) || t.title.includes(keyword); - }); - - if (matched.length === 0) { - throw new RangeError('No matching buffer for ' + keyword); +const selectByKeyword = async(current, keyword) => { + let tabs = await queryByKeyword(keyword); + if (tabs.length === 0) { + throw new RangeError('No matching buffer for ' + keyword); + } + for (let tab of tabs) { + if (tab.index > current.index) { + return browser.tabs.update(tab.id, { active: true }); } - for (let tab of matched) { - if (tab.index > current.index) { - return browser.tabs.update(tab.id, { active: true }); - } - } - return browser.tabs.update(matched[0].id, { active: true }); - }); + } + return browser.tabs.update(tabs[0].id, { active: true }); }; -const selectPrevTab = (current, count) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - if (tabs.length < 2) { - return; - } - let select = (current - count + tabs.length) % tabs.length; - let id = tabs[select].id; - return browser.tabs.update(id, { active: true }); - }); +const selectPrevTab = async(current, count) => { + let tabs = await browser.tabs.query({ currentWindow: true }); + if (tabs.length < 2) { + return; + } + let select = (current - count + tabs.length) % tabs.length; + let id = tabs[select].id; + return browser.tabs.update(id, { active: true }); }; -const selectNextTab = (current, count) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - if (tabs.length < 2) { - return; - } - let select = (current + count) % tabs.length; - let id = tabs[select].id; - return browser.tabs.update(id, { active: true }); - }); +const selectNextTab = async(current, count) => { + let tabs = await browser.tabs.query({ currentWindow: true }); + if (tabs.length < 2) { + return; + } + let select = (current + count) % tabs.length; + let id = tabs[select].id; + return browser.tabs.update(id, { active: true }); }; -const selectFirstTab = () => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let id = tabs[0].id; - return browser.tabs.update(id, { active: true }); - }); +const selectFirstTab = async() => { + let tabs = await browser.tabs.query({ currentWindow: true }); + let id = tabs[0].id; + return browser.tabs.update(id, { active: true }); }; -const selectLastTab = () => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - let id = tabs[tabs.length - 1].id; - return browser.tabs.update(id, { active: true }); - }); +const selectLastTab = async() => { + let tabs = await browser.tabs.query({ currentWindow: true }); + let id = tabs[tabs.length - 1].id; + return browser.tabs.update(id, { active: true }); }; const selectTab = (id) => { @@ -150,14 +136,11 @@ const reload = (current, cache) => { }; const updateTabPinned = (current, pinned) => { - return browser.tabs.query({ currentWindow: true, active: true }) - .then(() => { - return browser.tabs.update(current.id, { pinned: pinned }); - }); + return browser.tabs.update(current.id, { pinned }); }; const toggleTabPinned = (current) => { - updateTabPinned(current, !current.pinned); + return updateTabPinned(current, !current.pinned); }; const duplicate = (id) => { @@ -165,7 +148,8 @@ const duplicate = (id) => { }; export { - closeTab, closeTabForce, closeTabByKeywords, closeTabByKeywordsForce, + closeTab, closeTabForce, + queryByKeyword, closeTabByKeywords, closeTabByKeywordsForce, closeTabsByKeywords, closeTabsByKeywordsForce, reopenTab, selectAt, selectByKeyword, selectPrevTab, selectNextTab, selectFirstTab, diff --git a/src/background/shared/zooms.js b/src/background/shared/zooms.js index e3e2aa6..17b28fa 100644 --- a/src/background/shared/zooms.js +++ b/src/background/shared/zooms.js @@ -9,26 +9,20 @@ const ZOOM_SETTINGS = [ 1.10, 1.25, 1.50, 1.75, 2.00, 2.50, 3.00 ]; -const zoomIn = (tabId = undefined) => { - return browser.tabs.getZoom(tabId).then((factor) => { - for (let f of ZOOM_SETTINGS) { - if (f > factor) { - browser.tabs.setZoom(tabId, f); - break; - } - } - }); +const zoomIn = async(tabId = undefined) => { + let current = await browser.tabs.getZoom(tabId); + let factor = ZOOM_SETTINGS.find(f => f > current); + if (factor) { + return browser.tabs.setZoom(tabId, factor); + } }; -const zoomOut = (tabId = undefined) => { - return browser.tabs.getZoom(tabId).then((factor) => { - for (let f of [].concat(ZOOM_SETTINGS).reverse()) { - if (f < factor) { - browser.tabs.setZoom(tabId, f); - break; - } - } - }); +const zoomOut = async(tabId = undefined) => { + let current = await browser.tabs.getZoom(tabId); + let factor = [].concat(ZOOM_SETTINGS).reverse().find(f => f < current); + if (factor) { + return browser.tabs.setZoom(tabId, factor); + } }; const neutral = (tabId = undefined) => { diff --git a/src/console/components/console.js b/src/console/components/console.js index a9ae4ed..417c9f6 100644 --- a/src/console/components/console.js +++ b/src/console/components/console.js @@ -107,16 +107,15 @@ export default class ConsoleComponent { } } - onInput(e) { + async onInput(e) { this.store.dispatch(consoleActions.setConsoleText(e.target.value)); let source = e.target.value; - return browser.runtime.sendMessage({ + let completions = await browser.runtime.sendMessage({ type: messages.CONSOLE_QUERY_COMPLETIONS, text: source, - }).then((completions) => { - this.store.dispatch(consoleActions.setCompletions(source, completions)); }); + this.store.dispatch(consoleActions.setCompletions(source, completions)); } onInputShown(state) { diff --git a/src/console/reducers/index.js b/src/console/reducers/index.js index 2aec55c..71b0776 100644 --- a/src/console/reducers/index.js +++ b/src/console/reducers/index.js @@ -51,68 +51,61 @@ const nextConsoleText = (completions, group, item, defaults) => { return completions[group].items[item].content; }; +// eslint-disable-next-line max-lines-per-function export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.CONSOLE_HIDE: - return Object.assign({}, state, { - mode: '', - }); + return { ...state, + mode: '', }; case actions.CONSOLE_SHOW_COMMAND: - return Object.assign({}, state, { + return { ...state, mode: 'command', consoleText: action.text, - completions: [] - }); + completions: []}; case actions.CONSOLE_SHOW_FIND: - return Object.assign({}, state, { + return { ...state, mode: 'find', consoleText: '', - completions: [] - }); + completions: []}; case actions.CONSOLE_SHOW_ERROR: - return Object.assign({}, state, { + return { ...state, mode: 'error', - messageText: action.text, - }); + messageText: action.text, }; case actions.CONSOLE_SHOW_INFO: - return Object.assign({}, state, { + return { ...state, mode: 'info', - messageText: action.text, - }); + messageText: action.text, }; case actions.CONSOLE_HIDE_COMMAND: - return Object.assign({}, state, { + return { + ...state, mode: state.mode === 'command' || state.mode === 'find' ? '' : state.mode, - }); + }; case actions.CONSOLE_SET_CONSOLE_TEXT: - return Object.assign({}, state, { - consoleText: action.consoleText, - }); + return { ...state, + consoleText: action.consoleText, }; case actions.CONSOLE_SET_COMPLETIONS: - return Object.assign({}, state, { + return { ...state, completions: action.completions, completionSource: action.completionSource, groupSelection: -1, - itemSelection: -1, - }); + itemSelection: -1, }; case actions.CONSOLE_COMPLETION_NEXT: { let next = nextSelection(state); - return Object.assign({}, state, { + return { ...state, groupSelection: next[0], itemSelection: next[1], consoleText: nextConsoleText( state.completions, next[0], next[1], - state.completionSource), - }); + state.completionSource), }; } case actions.CONSOLE_COMPLETION_PREV: { let next = prevSelection(state); - return Object.assign({}, state, { + return { ...state, groupSelection: next[0], itemSelection: next[1], consoleText: nextConsoleText( state.completions, next[0], next[1], - state.completionSource), - }); + state.completionSource), }; } default: return state; diff --git a/src/content/actions/find.js b/src/content/actions/find.js index c7345cc..b3d7e30 100644 --- a/src/content/actions/find.js +++ b/src/content/actions/find.js @@ -22,6 +22,12 @@ const postPatternFound = (pattern) => { ); }; +const postNoPrevious = () => { + return consoleFrames.postError( + window.document, + 'No previous search keywords'); +}; + const find = (string, backwards) => { let caseSensitive = false; let wrapScan = true; @@ -29,44 +35,45 @@ const find = (string, backwards) => { // NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work // because of same origin policy + let found = window.find(string, caseSensitive, backwards, wrapScan); + if (found) { + return found; + } + window.getSelection().removeAllRanges(); return window.find(string, caseSensitive, backwards, wrapScan); }; -const findNext = (currentKeyword, reset, backwards) => { +const findNext = async(currentKeyword, reset, backwards) => { if (reset) { window.getSelection().removeAllRanges(); } - let promise = Promise.resolve(currentKeyword); + let keyword = currentKeyword; if (currentKeyword) { browser.runtime.sendMessage({ type: messages.FIND_SET_KEYWORD, keyword: currentKeyword, }); } else { - promise = browser.runtime.sendMessage({ + keyword = await browser.runtime.sendMessage({ type: messages.FIND_GET_KEYWORD, }); } + if (!keyword) { + return postNoPrevious(); + } + let found = find(keyword, backwards); + if (found) { + postPatternFound(keyword); + } else { + postPatternNotFound(keyword); + } - return promise.then((keyword) => { - let found = find(keyword, backwards); - if (!found) { - window.getSelection().removeAllRanges(); - found = find(keyword, backwards); - } - if (found) { - postPatternFound(keyword); - } else { - postPatternNotFound(keyword); - } - - return { - type: actions.FIND_SET_KEYWORD, - keyword, - found, - }; - }); + return { + type: actions.FIND_SET_KEYWORD, + keyword, + found, + }; }; const next = (currentKeyword, reset) => { diff --git a/src/content/actions/operation.js b/src/content/actions/operation.js index 9171766..40ac52d 100644 --- a/src/content/actions/operation.js +++ b/src/content/actions/operation.js @@ -8,7 +8,7 @@ import * as consoleFrames from 'content/console-frames'; import * as addonActions from './addon'; import * as properties from 'shared/settings/properties'; -// eslint-disable-next-line complexity +// eslint-disable-next-line complexity, max-lines-per-function const exec = (operation, repeat, settings) => { let smoothscroll = settings.properties.smoothscroll || properties.defaults.smoothscroll; diff --git a/src/content/actions/setting.js b/src/content/actions/setting.js index 4c1e385..e34b6e0 100644 --- a/src/content/actions/setting.js +++ b/src/content/actions/setting.js @@ -10,7 +10,7 @@ const reservedKeymaps = { const set = (value) => { let entries = []; if (value.keymaps) { - let keymaps = Object.assign({}, value.keymaps, reservedKeymaps); + let keymaps = { ...value.keymaps, ...reservedKeymaps }; entries = Object.entries(keymaps).map((entry) => { return [ keyUtils.fromMapKeys(entry[0]), @@ -21,9 +21,8 @@ const set = (value) => { return { type: actions.SETTING_SET, - value: Object.assign({}, value, { - keymaps: entries, - }) + value: { ...value, + keymaps: entries, } }; }; diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index 9b7b083..6437011 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -44,15 +44,16 @@ export default class Common { } } - reloadSettings() { - browser.runtime.sendMessage({ - type: messages.SETTINGS_QUERY, - }).then((settings) => { + async reloadSettings() { + try { + let settings = await browser.runtime.sendMessage({ + type: messages.SETTINGS_QUERY, + }); this.store.dispatch(settingActions.set(settings)); - }).catch((e) => { + } catch (e) { // Sometime sendMessage fails when background script is not ready. console.warn(e); setTimeout(() => this.reloadSettings(), 500); - }); + } } } diff --git a/src/content/reducers/addon.js b/src/content/reducers/addon.js index 8cc5ef1..b881ca0 100644 --- a/src/content/reducers/addon.js +++ b/src/content/reducers/addon.js @@ -7,17 +7,14 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.ADDON_ENABLE: - return Object.assign({}, state, { - enabled: true, - }); + return { ...state, + enabled: true, }; case actions.ADDON_DISABLE: - return Object.assign({}, state, { - enabled: false, - }); + return { ...state, + enabled: false, }; case actions.ADDON_TOGGLE_ENABLED: - return Object.assign({}, state, { - enabled: !state.enabled, - }); + return { ...state, + enabled: !state.enabled, }; default: return state; } diff --git a/src/content/reducers/find.js b/src/content/reducers/find.js index 8d63ee5..4560e2c 100644 --- a/src/content/reducers/find.js +++ b/src/content/reducers/find.js @@ -8,10 +8,9 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.FIND_SET_KEYWORD: - return Object.assign({}, state, { + return { ...state, keyword: action.keyword, - found: action.found, - }); + found: action.found, }; default: return state; } diff --git a/src/content/reducers/follow-controller.js b/src/content/reducers/follow-controller.js index 78fd848..5869c47 100644 --- a/src/content/reducers/follow-controller.js +++ b/src/content/reducers/follow-controller.js @@ -10,24 +10,20 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.FOLLOW_CONTROLLER_ENABLE: - return Object.assign({}, state, { + return { ...state, enabled: true, newTab: action.newTab, background: action.background, - keys: '', - }); + keys: '', }; case actions.FOLLOW_CONTROLLER_DISABLE: - return Object.assign({}, state, { - enabled: false, - }); + return { ...state, + enabled: false, }; case actions.FOLLOW_CONTROLLER_KEY_PRESS: - return Object.assign({}, state, { - keys: state.keys + action.key, - }); + return { ...state, + keys: state.keys + action.key, }; case actions.FOLLOW_CONTROLLER_BACKSPACE: - return Object.assign({}, state, { - keys: state.keys.slice(0, -1), - }); + return { ...state, + keys: state.keys.slice(0, -1), }; default: return state; } diff --git a/src/content/reducers/index.js b/src/content/reducers/index.js index 2487d85..c3a474e 100644 --- a/src/content/reducers/index.js +++ b/src/content/reducers/index.js @@ -14,11 +14,12 @@ const defaultState = { }; export default function reducer(state = defaultState, action = {}) { - return Object.assign({}, state, { + 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), - }); + }; } diff --git a/src/content/reducers/input.js b/src/content/reducers/input.js index 134aa95..23e7dd2 100644 --- a/src/content/reducers/input.js +++ b/src/content/reducers/input.js @@ -7,13 +7,11 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.INPUT_KEY_PRESS: - return Object.assign({}, state, { - keys: state.keys.concat([action.key]), - }); + return { ...state, + keys: state.keys.concat([action.key]), }; case actions.INPUT_CLEAR_KEYS: - return Object.assign({}, state, { - keys: [], - }); + return { ...state, + keys: [], }; default: return state; } diff --git a/src/content/reducers/setting.js b/src/content/reducers/setting.js index a23027f..a49db6d 100644 --- a/src/content/reducers/setting.js +++ b/src/content/reducers/setting.js @@ -8,7 +8,7 @@ const defaultState = { export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.SETTING_SET: - return Object.assign({}, action.value); + return { ...action.value }; default: return state; } diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index 92c9f8a..1219ba5 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -4,20 +4,17 @@ import DefaultSettings from 'shared/settings/default'; import * as settingsStorage from 'shared/settings/storage'; import * as settingsValues from 'shared/settings/values'; -const load = () => { - return settingsStorage.loadRaw().then((settings) => { - return set(settings); - }); +const load = async() => { + let settings = await settingsStorage.loadRaw(); + return set(settings); }; -const save = (settings) => { - return settingsStorage.save(settings).then(() => { - return browser.runtime.sendMessage({ - type: messages.SETTINGS_RELOAD - }); - }).then(() => { - return set(settings); +const save = async(settings) => { + await settingsStorage.save(settings); + await browser.runtime.sendMessage({ + type: messages.SETTINGS_RELOAD }); + return set(settings); }; const set = (settings) => { diff --git a/src/settings/components/form/keymaps-form.jsx b/src/settings/components/form/keymaps-form.jsx index f0f69cf..dcf65d9 100644 --- a/src/settings/components/form/keymaps-form.jsx +++ b/src/settings/components/form/keymaps-form.jsx @@ -100,7 +100,7 @@ class KeymapsForm extends Component { return; } - let next = Object.assign({}, this.props.value); + let next = { ...this.props.value }; next[e.target.name] = e.target.value; this.props.onChange(next); diff --git a/src/settings/components/form/properties-form.jsx b/src/settings/components/form/properties-form.jsx index 55c8512..ceb79d7 100644 --- a/src/settings/components/form/properties-form.jsx +++ b/src/settings/components/form/properties-form.jsx @@ -44,7 +44,7 @@ class PropertiesForm extends Component { } let name = e.target.name; - let next = Object.assign({}, this.props.value); + let next = { ...this.props.value }; if (e.target.type.toLowerCase() === 'checkbox') { next[name] = e.target.checked; } else if (e.target.type.toLowerCase() === 'number') { diff --git a/src/settings/components/form/search-form.jsx b/src/settings/components/form/search-form.jsx index e85761f..2d5f01b 100644 --- a/src/settings/components/form/search-form.jsx +++ b/src/settings/components/form/search-form.jsx @@ -53,10 +53,10 @@ class SearchForm extends Component { let value = this.props.value; let name = e.target.name; let index = e.target.getAttribute('data-index'); - let next = Object.assign({}, { + let next = { default: value.default, engines: value.engines ? value.engines.slice() : [], - }); + }; if (name === 'name') { next.engines[index][0] = e.target.value; diff --git a/src/settings/components/index.jsx b/src/settings/components/index.jsx index e13bfa1..c479986 100644 --- a/src/settings/components/index.jsx +++ b/src/settings/components/index.jsx @@ -134,7 +134,7 @@ class SettingsComponent extends Component { } validateValue(e) { - let next = Object.assign({}, this.state); + let next = { ...this.state }; next.errors.json = ''; try { @@ -146,18 +146,16 @@ class SettingsComponent extends Component { } bindForm(name, value) { - let next = Object.assign({}, this.state, { - settings: Object.assign({}, this.state.settings, { - form: Object.assign({}, this.state.settings.form) - }) - }); + let next = { ...this.state, + settings: { ...this.state.settings, + form: { ...this.state.settings.form }}}; next.settings.form[name] = value; this.setState(next); this.context.store.dispatch(settingActions.save(next.settings)); } bindValue(e) { - let next = Object.assign({}, this.state); + let next = { ...this.state }; let error = false; next.errors.json = ''; @@ -190,7 +188,7 @@ class SettingsComponent extends Component { let form = settingsValues.formFromJson( this.state.settings.json, KeymapsForm.AllowdOps); - let next = Object.assign({}, this.state); + let next = { ...this.state }; next.settings.form = form; next.settings.source = 'form'; next.errors.json = ''; @@ -201,7 +199,7 @@ class SettingsComponent extends Component { migrateToJson() { let json = settingsValues.jsonFromForm(this.state.settings.form); - let next = Object.assign({}, this.state); + let next = { ...this.state }; next.settings.json = json; next.settings.source = 'json'; next.errors.json = ''; diff --git a/src/shared/settings/storage.js b/src/shared/settings/storage.js index 9b8045d..5dce3b0 100644 --- a/src/shared/settings/storage.js +++ b/src/shared/settings/storage.js @@ -1,30 +1,26 @@ 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 loadRaw = async() => { + let { settings } = await browser.storage.local.get('settings'); + if (!settings) { + return DefaultSettings; + } + return { ...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); - } - if (!value.properties) { - value.properties = {}; - } - return Object.assign({}, - settingsValues.valueFromJson(DefaultSettings.json), - value); - }); +const loadValue = async() => { + let settings = await loadRaw(); + 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); + } + if (!value.properties) { + value.properties = {}; + } + return { ...settingsValues.valueFromJson(DefaultSettings.json), ...value }; }; const save = (settings) => { diff --git a/src/shared/settings/values.js b/src/shared/settings/values.js index bd03be2..9828af6 100644 --- a/src/shared/settings/values.js +++ b/src/shared/settings/values.js @@ -6,12 +6,12 @@ const operationFromFormName = (name) => { if (argStr) { args = JSON.parse(argStr); } - return Object.assign({ type }, args); + return { type, ...args }; }; const operationToFormName = (op) => { let type = op.type; - let args = Object.assign({}, op); + let args = { ...op }; delete args.type; if (Object.keys(args).length === 0) { @@ -83,7 +83,7 @@ const formFromValue = (value, allowedOps) => { } } - let formProperties = Object.assign({}, properties.defaults, value.properties); + let formProperties = { ...properties.defaults, ...value.properties }; return { keymaps, diff --git a/src/shared/versions/index.js b/src/shared/versions/index.js index ee9f3b5..ba3d183 100644 --- a/src/shared/versions/index.js +++ b/src/shared/versions/index.js @@ -13,13 +13,12 @@ const notificationClickListener = (id) => { browser.notifications.onClicked.removeListener(notificationClickListener); }; -const checkUpdated = () => { - return storage.load().then((prev) => { - if (!prev) { - return true; - } - return manifest.version !== prev; - }); +const checkUpdated = async() => { + let prev = await storage.load(); + if (!prev) { + return true; + } + return manifest.version !== prev; }; const notify = () => { diff --git a/src/shared/versions/storage.js b/src/shared/versions/storage.js index 37603c8..7883258 100644 --- a/src/shared/versions/storage.js +++ b/src/shared/versions/storage.js @@ -1,7 +1,6 @@ -const load = () => { - return browser.storage.local.get('version').then(({ version }) => { - return version; - }); +const load = async() => { + let { version } = await browser.storage.local.get('version'); + return version; }; const save = (version) => { |