aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOmar Rizwan <omar@omar.website>2020-12-11 15:45:19 -0800
committerOmar Rizwan <omar@omar.website>2020-12-11 15:45:19 -0800
commit1dd02874a363f031a738d395e15cd0fc1423db64 (patch)
tree0499d033dd514db4c6e85609a827370ce140b511
parentd3b376582f040f4f7620a8c1314b78ff995ce171 (diff)
fence withTab and fromScript into scope
-rw-r--r--extension/background.js59
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))) };