From d21c9eace16593446cdedbd85ba780b7f4368b70 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Tue, 15 Dec 2020 12:11:41 -0800 Subject: make URL writable (kinda). allow delete in by-title/. README update --- extension/background.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'extension') diff --git a/extension/background.js b/extension/background.js index a233f27..e92069e 100644 --- a/extension/background.js +++ b/extension/background.js @@ -148,7 +148,7 @@ const defineFile = (getData, setData) => ({ }, async write({path, buf}) { // FIXME: patch - setData(path, buf); return {size: utf8(buf).length}; + setData(path, buf); return { size: utf8(buf).length }; }, async release({fh}) { Cache.removeObjectForHandle(fh); return {}; } }); @@ -177,17 +177,21 @@ router["/tabs/by-id"] = { // TODO: scripts/ (function() { - const withTab = handler => defineFile(async path => { + const withTab = (readHandler, writeHandler) => defineFile(async path => { const tabId = parseInt(pathComponent(path, -2)); const tab = await browser.tabs.get(tabId); - return handler(tab); - }); + return readHandler(tab); + + }, writeHandler ? async (path, buf) => { + const tabId = parseInt(pathComponent(path, -2)); + await browser.tabs.update(tabId, writeHandler(buf)); + } : undefined); const fromScript = code => defineFile(async path => { const tabId = parseInt(pathComponent(path, -2)); return (await browser.tabs.executeScript(tabId, {code}))[0]; }); - router["/tabs/by-id/*/url"] = withTab(tab => tab.url + "\n"); + router["/tabs/by-id/*/url"] = withTab(tab => tab.url + "\n", buf => ({ url: buf })); router["/tabs/by-id/*/title"] = withTab(tab => tab.title + "\n"); router["/tabs/by-id/*/text"] = fromScript(`document.body.innerText`); })(); @@ -259,7 +263,7 @@ router["/tabs/by-id/*/control"] = { const command = buf.trim(); // can use `discard`, `remove`, `reload`, `goForward`, `goBack`... // see https://developer.chrome.com/extensions/tabs - await new Promise(resolve => chrome.tabs[command](tabId, resolve)); + await browser.tabs[command](tabId); return {size: utf8(buf).length}; } }; @@ -280,8 +284,13 @@ router["/tabs/by-title"] = { router["/tabs/by-title/*"] = { // a symbolic link to /tabs/by-id/[id for this tab] async readlink({path}) { - const parts = path.split("_"); const id = parts[parts.length - 1]; - return { buf: "../by-id/" + id }; + const parts = path.split("_"); const tabId = parts[parts.length - 1]; + return { buf: "../by-id/" + tabId }; + }, + async unlink({path}) { + const parts = path.split("_"); const tabId = parseInt(parts[parts.length - 1]); + await browser.tabs.remove(tabId); + return {}; } }; -- cgit v1.2.3