From a26d8a8a1bed48a77e062914c120a23ace7bb8cf Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 24 Feb 2019 20:54:35 +0900 Subject: Capitalize background scripts --- src/background/presenters/IndicatorPresenter.js | 12 +++ src/background/presenters/TabPresenter.js | 102 ++++++++++++++++++++++++ src/background/presenters/WindowPresenter.js | 5 ++ src/background/presenters/console.js | 36 --------- src/background/presenters/indicator.js | 12 --- src/background/presenters/tab.js | 102 ------------------------ src/background/presenters/window.js | 5 -- 7 files changed, 119 insertions(+), 155 deletions(-) create mode 100644 src/background/presenters/IndicatorPresenter.js create mode 100644 src/background/presenters/TabPresenter.js create mode 100644 src/background/presenters/WindowPresenter.js delete mode 100644 src/background/presenters/console.js delete mode 100644 src/background/presenters/indicator.js delete mode 100644 src/background/presenters/tab.js delete mode 100644 src/background/presenters/window.js (limited to 'src/background/presenters') diff --git a/src/background/presenters/IndicatorPresenter.js b/src/background/presenters/IndicatorPresenter.js new file mode 100644 index 0000000..5737519 --- /dev/null +++ b/src/background/presenters/IndicatorPresenter.js @@ -0,0 +1,12 @@ +export default class IndicatorPresenter { + indicate(enabled) { + let path = enabled + ? 'resources/enabled_32x32.png' + : 'resources/disabled_32x32.png'; + return browser.browserAction.setIcon({ path }); + } + + onClick(listener) { + browser.browserAction.onClicked.addListener(listener); + } +} diff --git a/src/background/presenters/TabPresenter.js b/src/background/presenters/TabPresenter.js new file mode 100644 index 0000000..744be39 --- /dev/null +++ b/src/background/presenters/TabPresenter.js @@ -0,0 +1,102 @@ +import MemoryStorage from '../infrastructures/MemoryStorage'; + +const CURRENT_SELECTED_KEY = 'tabs.current.selected'; +const LAST_SELECTED_KEY = 'tabs.last.selected'; + +export default class TabPresenter { + open(url, tabId) { + return browser.tabs.update(tabId, { url }); + } + + create(url, opts) { + return browser.tabs.create({ url, ...opts }); + } + + async getCurrent() { + let tabs = await browser.tabs.query({ + active: true, currentWindow: true + }); + return tabs[0]; + } + + getAll() { + return browser.tabs.query({ currentWindow: true }); + } + + async getLastSelectedId() { + let cache = new MemoryStorage(); + let tabId = await cache.get(LAST_SELECTED_KEY); + if (tabId === null || typeof tabId === 'undefined') { + return; + } + return tabId; + } + + async getByKeyword(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); + }); + } + + select(tabId) { + return browser.tabs.update(tabId, { active: true }); + } + + remove(ids) { + return browser.tabs.remove(ids); + } + + async reopen() { + 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); + } + + reload(tabId, cache) { + return browser.tabs.reload(tabId, { bypassCache: cache }); + } + + setPinned(tabId, pinned) { + return browser.tabs.update(tabId, { pinned }); + } + + duplicate(id) { + return browser.tabs.duplicate(id); + } + + getZoom(tabId) { + return browser.tabs.getZoom(tabId); + } + + setZoom(tabId, factor) { + return browser.tabs.setZoom(tabId, factor); + } + + onSelected(listener) { + browser.tabs.onActivated.addListener(listener); + } +} + +let tabPresenter = new TabPresenter(); +tabPresenter.onSelected((tab) => { + let cache = new MemoryStorage(); + + let lastId = cache.get(CURRENT_SELECTED_KEY); + if (lastId) { + cache.set(LAST_SELECTED_KEY, lastId); + } + cache.set(CURRENT_SELECTED_KEY, tab.tabId); +}); diff --git a/src/background/presenters/WindowPresenter.js b/src/background/presenters/WindowPresenter.js new file mode 100644 index 0000000..a82c4a2 --- /dev/null +++ b/src/background/presenters/WindowPresenter.js @@ -0,0 +1,5 @@ +export default class WindowPresenter { + create(url) { + return browser.windows.create({ url }); + } +} diff --git a/src/background/presenters/console.js b/src/background/presenters/console.js deleted file mode 100644 index 8259238..0000000 --- a/src/background/presenters/console.js +++ /dev/null @@ -1,36 +0,0 @@ -import messages from '../../shared/messages'; - -export default class ConsolePresenter { - showCommand(tabId, command) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_COMMAND, - command, - }); - } - - showFind(tabId) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_FIND - }); - } - - showInfo(tabId, message) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_INFO, - text: message, - }); - } - - showError(tabId, message) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_SHOW_ERROR, - text: message, - }); - } - - hide(tabId) { - return browser.tabs.sendMessage(tabId, { - type: messages.CONSOLE_HIDE, - }); - } -} diff --git a/src/background/presenters/indicator.js b/src/background/presenters/indicator.js deleted file mode 100644 index 5737519..0000000 --- a/src/background/presenters/indicator.js +++ /dev/null @@ -1,12 +0,0 @@ -export default class IndicatorPresenter { - indicate(enabled) { - let path = enabled - ? 'resources/enabled_32x32.png' - : 'resources/disabled_32x32.png'; - return browser.browserAction.setIcon({ path }); - } - - onClick(listener) { - browser.browserAction.onClicked.addListener(listener); - } -} diff --git a/src/background/presenters/tab.js b/src/background/presenters/tab.js deleted file mode 100644 index 69cae1c..0000000 --- a/src/background/presenters/tab.js +++ /dev/null @@ -1,102 +0,0 @@ -import MemoryStorage from '../infrastructures/memory-storage'; - -const CURRENT_SELECTED_KEY = 'tabs.current.selected'; -const LAST_SELECTED_KEY = 'tabs.last.selected'; - -export default class TabPresenter { - open(url, tabId) { - return browser.tabs.update(tabId, { url }); - } - - create(url, opts) { - return browser.tabs.create({ url, ...opts }); - } - - async getCurrent() { - let tabs = await browser.tabs.query({ - active: true, currentWindow: true - }); - return tabs[0]; - } - - getAll() { - return browser.tabs.query({ currentWindow: true }); - } - - async getLastSelectedId() { - let cache = new MemoryStorage(); - let tabId = await cache.get(LAST_SELECTED_KEY); - if (tabId === null || typeof tabId === 'undefined') { - return; - } - return tabId; - } - - async getByKeyword(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); - }); - } - - select(tabId) { - return browser.tabs.update(tabId, { active: true }); - } - - remove(ids) { - return browser.tabs.remove(ids); - } - - async reopen() { - 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); - } - - reload(tabId, cache) { - return browser.tabs.reload(tabId, { bypassCache: cache }); - } - - setPinned(tabId, pinned) { - return browser.tabs.update(tabId, { pinned }); - } - - duplicate(id) { - return browser.tabs.duplicate(id); - } - - getZoom(tabId) { - return browser.tabs.getZoom(tabId); - } - - setZoom(tabId, factor) { - return browser.tabs.setZoom(tabId, factor); - } - - onSelected(listener) { - browser.tabs.onActivated.addListener(listener); - } -} - -let tabPresenter = new TabPresenter(); -tabPresenter.onSelected((tab) => { - let cache = new MemoryStorage(); - - let lastId = cache.get(CURRENT_SELECTED_KEY); - if (lastId) { - cache.set(LAST_SELECTED_KEY, lastId); - } - cache.set(CURRENT_SELECTED_KEY, tab.tabId); -}); diff --git a/src/background/presenters/window.js b/src/background/presenters/window.js deleted file mode 100644 index a82c4a2..0000000 --- a/src/background/presenters/window.js +++ /dev/null @@ -1,5 +0,0 @@ -export default class WindowPresenter { - create(url) { - return browser.windows.create({ url }); - } -} -- cgit v1.2.3