diff options
Diffstat (limited to 'src/background/presenters')
-rw-r--r-- | src/background/presenters/console.js | 36 | ||||
-rw-r--r-- | src/background/presenters/indicator.js | 12 | ||||
-rw-r--r-- | src/background/presenters/tab.js | 101 | ||||
-rw-r--r-- | src/background/presenters/window.js | 5 |
4 files changed, 154 insertions, 0 deletions
diff --git a/src/background/presenters/console.js b/src/background/presenters/console.js new file mode 100644 index 0000000..8259238 --- /dev/null +++ b/src/background/presenters/console.js @@ -0,0 +1,36 @@ +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 new file mode 100644 index 0000000..5737519 --- /dev/null +++ b/src/background/presenters/indicator.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/tab.js b/src/background/presenters/tab.js new file mode 100644 index 0000000..2a06a5a --- /dev/null +++ b/src/background/presenters/tab.js @@ -0,0 +1,101 @@ +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 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 }); + } + + async selectAt(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 }); + } + + 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); + } + + async createAdjacent(url, { openerTabId, active }) { + let tabs = await browser.tabs.query({ + active: true, currentWindow: true + }); + return browser.tabs.create({ + url, + openerTabId, + active, + index: tabs[0].index + 1 + }); + } + + onSelected(listener) { + browser.tabs.onActivated.addListener(listener); + } +} diff --git a/src/background/presenters/window.js b/src/background/presenters/window.js new file mode 100644 index 0000000..a82c4a2 --- /dev/null +++ b/src/background/presenters/window.js @@ -0,0 +1,5 @@ +export default class WindowPresenter { + create(url) { + return browser.windows.create({ url }); + } +} |