diff options
-rw-r--r-- | extension/background.js | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/extension/background.js b/extension/background.js index d2ef8cf..d4841f7 100644 --- a/extension/background.js +++ b/extension/background.js @@ -133,8 +133,27 @@ const BrowserState = { lastFocusedWindowId: null }; view of what the whole filesystem looks like at a glance. */ const router = {}; -function withTab(handler) { - return { +const Cache = { + // used when you open a file to cache the content we got from the browser + // until you close that file. + store: {}, nextHandle: 0, + storeObject(object) { + const handle = ++this.nextHandle; + this.store[handle] = object; + return handle; + }, + getObjectForHandle(handle) { return this.store[handle]; }, + removeObjectForHandle(handle) { delete this.store[handle]; } +}; + +router["/tabs/by-id"] = { + async readdir() { + const tabs = await browser.tabs.query({}); + return { entries: tabs.map(tab => String(tab.id)) }; + } +}; +(function() { + const withTab = handler => ({ async getattr({path}) { const tab = await browser.tabs.get(parseInt(pathComponent(path, -2))); return { @@ -148,10 +167,8 @@ function withTab(handler) { const tab = await browser.tabs.get(parseInt(pathComponent(path, -2))); return { buf: utf8(handler(tab), offset, size) }; } - }; -} -function fromScript(code) { - return { + }); + const fromScript = code => ({ async getattr({path}) { const tabId = parseInt(pathComponent(path, -2)); return { @@ -165,35 +182,15 @@ function fromScript(code) { const tabId = parseInt(pathComponent(path, -2)); return { buf: utf8((await browser.tabs.executeScript(tabId, {code}))[0], offset, size) } } - }; -} -const Cache = { - // used when you open a file to cache the content we got from the browser - // until you close that file. - store: {}, nextHandle: 0, - storeObject(object) { - const handle = ++this.nextHandle; - this.store[handle] = object; - return handle; - }, - getObjectForHandle(handle) { return this.store[handle]; }, - removeObjectForHandle(handle) { delete this.store[handle]; } -}; - -router["/tabs/by-id"] = { - async readdir() { - const tabs = await browser.tabs.query({}); - return { entries: tabs.map(tab => String(tab.id)) }; - } -}; -router["/tabs/by-id/*/url"] = withTab(tab => tab.url + "\n"); -router["/tabs/by-id/*/title"] = withTab(tab => tab.title + "\n"); -router["/tabs/by-id/*/text"] = fromScript(`document.body.innerText`); + }); + router["/tabs/by-id/*/url"] = withTab(tab => tab.url + "\n"); + router["/tabs/by-id/*/title"] = withTab(tab => tab.title + "\n"); + router["/tabs/by-id/*/text"] = fromScript(`document.body.innerText`); +})(); router["/tabs/by-id/*/screenshot.png"] = { async open({path}) { const tabId = parseInt(pathComponent(path, -2)); await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Page"); - // FIXME: cache. const {data} = await sendDebuggerCommand(tabId, "Page.captureScreenshot"); return { fh: Cache.storeObject(Uint8Array.from(atob(data), c => c.charCodeAt(0))) }; |