diff options
Diffstat (limited to 'src/background/shared')
-rw-r--r-- | src/background/shared/completions/bookmarks.js | 23 | ||||
-rw-r--r-- | src/background/shared/completions/histories.js | 27 | ||||
-rw-r--r-- | src/background/shared/completions/index.js | 116 | ||||
-rw-r--r-- | src/background/shared/tabs.js | 209 | ||||
-rw-r--r-- | src/background/shared/zooms.js | 30 |
5 files changed, 184 insertions, 221 deletions
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/tabs.js b/src/background/shared/tabs.js index 5c67a9f..33a4fac 100644 --- a/src/background/shared/tabs.js +++ b/src/background/shared/tabs.js @@ -1,143 +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 queryByKeyword = (keyword, excludePinned = false) => { - return browser.tabs.query({ currentWindow: true }).then((tabs) => { - 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 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 closeTabByKeywords = (keyword) => { - return queryByKeyword(keyword, false).then((tabs) => { - 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); - } - browser.tabs.remove(tabs[0].id); - }); -}; - -const closeTabByKeywordsForce = (keyword) => { - return queryByKeyword(keyword, true).then((tabs) => { - 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); - } - browser.tabs.remove(tabs[0].id); - }); +const closeTabByKeywords = 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 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, 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 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 = () => { - let window = null; - return browser.windows.getCurrent().then().then((w) => { - window = w; - return browser.sessions.getRecentlyClosed(); - }).then((sessions) => { - 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 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 queryByKeyword(keyword).then((tabs) => { - if (tabs.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 tabs) { - if (tab.index > current.index) { - return browser.tabs.update(tab.id, { active: true }); - } - } - return browser.tabs.update(tabs[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) => { @@ -152,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) => { 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) => { |