diff options
author | Omar Rizwan <omar@omar.website> | 2021-02-22 15:33:31 -0800 |
---|---|---|
committer | Omar Rizwan <omar@omar.website> | 2021-02-22 15:33:31 -0800 |
commit | dffc41a3e1c433f326ce50e9ec7c47e7bb23c10f (patch) | |
tree | 02a57a334da36aecabddaaad350059bfcc4a55f5 | |
parent | 5c3e851bafcf52364a7dcf2348e10c8686245a09 (diff) | |
download | TabFS-dffc41a3e1c433f326ce50e9ec7c47e7bb23c10f.tar.gz TabFS-dffc41a3e1c433f326ce50e9ec7c47e7bb23c10f.zip |
extension: add TAB/evals!
-rw-r--r-- | extension/background.js | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/extension/background.js b/extension/background.js index ab372e6..4e2afeb 100644 --- a/extension/background.js +++ b/extension/background.js @@ -327,6 +327,41 @@ router["/tabs/by-id/*/execute-script"] = { }, async truncate({path, size}) { return {}; } }; +(function() { + let evals = {}; + router["/tabs/by-id/*/evals"] = { + async readdir({path}) { + const tabId = parseInt(pathComponent(path, -2)); + return { entries: [".", "..", ...Object.keys(evals[tabId] || [])] }; + }, + getattr() { + return { + st_mode: unix.S_IFDIR | 0777, // writable so you can create evals + st_nlink: 3, + st_size: 0, + }; + }, + }; + router["/tabs/by-id/*/evals/*"] = { + // NOTE: eval runs in extension's content script, not in original page JS context + async create({path, mode}) { + const [tabId, expr] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1)]; + evals[tabId] = evals[tabId] || {}; + evals[tabId][expr] = async function() { + return (await browser.tabs.executeScript(tabId, {code: expr}))[0]; + }; + return {}; + }, + + ...defineFile(async path => { + const [tabId, expr] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1)]; + if (!evals[tabId] || !(expr in evals[tabId])) { throw new UnixError(unix.ENOENT); } + return JSON.stringify(await evals[tabId][expr]()) + '\n'; + }) + }; + // TODO: allow deletion of evals +})(); + // TODO: imports // (function() { // const imports = {}; @@ -340,9 +375,6 @@ router["/tabs/by-id/*/execute-script"] = { // } // }; // })(); -// TODO: watches -// router["/tabs/by-id/*/watches"] = { -// }; router["/tabs/by-id/*/window"] = { // a symbolic link to /windows/[id for this window] async readlink({path}) { |