2018-11-12 01:18:21 +01:00
|
|
|
const unix = {
|
|
|
|
EPERM: 1,
|
|
|
|
ENOENT: 2,
|
|
|
|
ESRCH: 3,
|
|
|
|
EINTR: 4,
|
|
|
|
EIO: 5,
|
|
|
|
ENXIO: 6,
|
2019-02-28 08:38:12 +01:00
|
|
|
ENOTSUP: 45,
|
2020-12-25 23:55:35 +01:00
|
|
|
ETIMEDOUT: 110, // FIXME: not on macOS (?)
|
2018-11-12 01:18:21 +01:00
|
|
|
|
|
|
|
// Unix file types
|
|
|
|
S_IFMT: 0170000, // type of file mask
|
|
|
|
S_IFIFO: 010000, // named pipe (fifo)
|
|
|
|
S_IFCHR: 020000, // character special
|
|
|
|
S_IFDIR: 040000, // directory
|
|
|
|
S_IFBLK: 060000, // block special
|
|
|
|
S_IFREG: 0100000, // regular
|
|
|
|
S_IFLNK: 0120000, // symbolic link
|
|
|
|
S_IFSOCK: 0140000, // socket
|
|
|
|
}
|
2020-12-02 20:58:37 +01:00
|
|
|
class UnixError extends Error {
|
2020-12-02 22:45:20 +01:00
|
|
|
constructor(error) { super(); this.name = "UnixError"; this.error = error; }
|
2018-11-22 11:52:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:50:49 +01:00
|
|
|
const sanitize = (function() {
|
|
|
|
// from https://github.com/parshap/node-sanitize-filename/blob/209c39b914c8eb48ee27bcbde64b2c7822fdf3de/index.js
|
|
|
|
|
2021-03-14 23:53:04 +01:00
|
|
|
// I've added ' ' to the list of illegal characters. it's a
|
|
|
|
// decision whether we want to allow spaces in filenames... I think
|
|
|
|
// they're annoying, so I'm sanitizing them out for now.
|
|
|
|
var illegalRe = /[\/\?<>\\:\*\|" ]/g;
|
2021-03-14 23:50:49 +01:00
|
|
|
var controlRe = /[\x00-\x1f\x80-\x9f]/g;
|
|
|
|
var reservedRe = /^\.+$/;
|
|
|
|
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
|
|
|
|
var windowsTrailingRe = /[\. ]+$/;
|
|
|
|
|
|
|
|
function sanitize(input, replacement) {
|
|
|
|
if (typeof input !== 'string') {
|
|
|
|
throw new Error('Input must be string');
|
|
|
|
}
|
|
|
|
var sanitized = input
|
|
|
|
.replace(illegalRe, replacement)
|
|
|
|
.replace(controlRe, replacement)
|
|
|
|
.replace(reservedRe, replacement)
|
|
|
|
.replace(windowsReservedRe, replacement)
|
|
|
|
.replace(windowsTrailingRe, replacement);
|
|
|
|
return sanitized.slice(0, 200);
|
|
|
|
}
|
|
|
|
return input => sanitize(input, '_');
|
|
|
|
})();
|
|
|
|
|
2020-12-20 00:22:44 +01:00
|
|
|
const stringToUtf8Array = (function() {
|
|
|
|
const encoder = new TextEncoder("utf-8");
|
|
|
|
return str => encoder.encode(str);
|
|
|
|
})();
|
|
|
|
const utf8ArrayToString = (function() {
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
return utf8 => decoder.decode(utf8);
|
|
|
|
})();
|
2018-11-24 07:15:29 +01:00
|
|
|
|
2021-03-26 10:21:14 +01:00
|
|
|
// Helper function: generates a full set of file operations that you
|
|
|
|
// can use as a route handler (so clients can read and write
|
|
|
|
// sections of the file, stat it to get its size and see it show up
|
|
|
|
// in ls, etc), given getData and setData functions that define the
|
|
|
|
// contents of the entire file.
|
2021-04-20 14:07:22 +02:00
|
|
|
const routeWithContents = (function() {
|
2021-03-26 10:21:14 +01:00
|
|
|
const Cache = {
|
|
|
|
// used when you open a file to cache the content we got from the
|
|
|
|
// browser until you close that file. (so we can respond to
|
|
|
|
// individual chunk read() and write() requests without doing a
|
|
|
|
// whole new conversation with the browser and regenerating the
|
|
|
|
// content -- important for taking a screenshot, for instance)
|
|
|
|
store: {}, nextHandle: 0,
|
2021-04-06 22:48:48 +02:00
|
|
|
storeObject(path, object) {
|
2021-03-26 10:21:14 +01:00
|
|
|
const handle = ++this.nextHandle;
|
2021-04-06 22:48:48 +02:00
|
|
|
this.store[handle] = {path, object};
|
2021-03-26 10:21:14 +01:00
|
|
|
return handle;
|
|
|
|
},
|
2021-04-06 22:48:48 +02:00
|
|
|
getObjectForHandle(handle) { return this.store[handle].object; },
|
|
|
|
setObjectForHandle(handle, object) { this.store[handle].object = object; },
|
|
|
|
removeObjectForHandle(handle) { delete this.store[handle]; },
|
|
|
|
setObjectForPath(path, object) {
|
|
|
|
for (let storedObject of Object.values(this.store)) {
|
|
|
|
if (storedObject.path === path) {
|
|
|
|
storedObject.object = object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 10:21:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function toUtf8Array(stringOrArray) {
|
|
|
|
if (typeof stringOrArray == 'string') { return stringToUtf8Array(stringOrArray); }
|
|
|
|
else { return stringOrArray; }
|
|
|
|
}
|
2020-12-19 08:54:36 +01:00
|
|
|
|
2021-04-20 14:07:22 +02:00
|
|
|
const routeWithContents = (getData, setData) => ({
|
2021-03-26 10:21:14 +01:00
|
|
|
// getData: (req: Request U Vars) -> Promise<contentsOfFile: String|Uint8Array>
|
|
|
|
// setData [optional]: (req: Request U Vars, newContentsOfFile: String) -> Promise<>
|
|
|
|
|
|
|
|
// You can override file operations (like `truncate` or `getattr`)
|
|
|
|
// in the returned set if you want different behavior from what's
|
|
|
|
// defined here.
|
|
|
|
|
|
|
|
async getattr(req) {
|
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFREG | 0444 | (setData ? 0222 : 0),
|
|
|
|
st_nlink: 1,
|
|
|
|
// you'll want to override this if getData() is slow, because
|
|
|
|
// getattr() gets called a lot more cavalierly than open().
|
|
|
|
st_size: toUtf8Array(await getData(req)).length
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// We call getData() once when the file is opened, then cache that
|
|
|
|
// data for all subsequent reads from that application.
|
|
|
|
async open(req) {
|
2021-04-06 22:48:48 +02:00
|
|
|
const data = await getData(req);
|
|
|
|
return { fh: Cache.storeObject(req.path, toUtf8Array(data)) };
|
2021-03-26 10:21:14 +01:00
|
|
|
},
|
|
|
|
async read({fh, size, offset}) {
|
2021-04-06 22:48:48 +02:00
|
|
|
return { buf: String.fromCharCode(...Cache.getObjectForHandle(fh).slice(offset, offset + size)) };
|
2021-03-26 10:21:14 +01:00
|
|
|
},
|
|
|
|
async write(req) {
|
|
|
|
const {fh, offset, buf} = req;
|
|
|
|
let arr = Cache.getObjectForHandle(fh);
|
|
|
|
const bufarr = stringToUtf8Array(buf);
|
|
|
|
if (offset + bufarr.length > arr.length) {
|
|
|
|
const newArr = new Uint8Array(offset + bufarr.length);
|
|
|
|
newArr.set(arr.slice(0, Math.min(offset, arr.length)));
|
|
|
|
arr = newArr;
|
|
|
|
Cache.setObjectForHandle(fh, arr);
|
|
|
|
}
|
|
|
|
arr.set(bufarr, offset);
|
|
|
|
// I guess caller should override write() if they want to actually
|
|
|
|
// patch and not just re-set the whole string (for example,
|
|
|
|
// if they want to hot-reload just one function the user modified)
|
|
|
|
await setData(req, utf8ArrayToString(arr)); return { size: bufarr.length };
|
|
|
|
},
|
|
|
|
async release({fh}) { Cache.removeObjectForHandle(fh); return {}; },
|
|
|
|
|
|
|
|
async truncate(req) {
|
|
|
|
let arr = toUtf8Array(await getData(req));
|
|
|
|
if (req.size !== arr.length) {
|
|
|
|
const newArr = new Uint8Array(req.size);
|
|
|
|
newArr.set(arr.slice(0, Math.min(req.size, arr.length)));
|
|
|
|
arr = newArr;
|
|
|
|
}
|
2021-04-06 22:48:48 +02:00
|
|
|
Cache.setObjectForPath(req.path, arr);
|
2021-03-26 10:21:14 +01:00
|
|
|
await setData(req, utf8ArrayToString(arr)); return {};
|
2020-12-23 13:18:26 +01:00
|
|
|
}
|
2021-03-26 10:21:14 +01:00
|
|
|
});
|
2021-04-20 14:07:22 +02:00
|
|
|
routeWithContents.Cache = Cache;
|
|
|
|
return routeWithContents;
|
2021-03-26 10:21:14 +01:00
|
|
|
})();
|
2020-12-12 00:45:19 +01:00
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
const Routes = {};
|
|
|
|
|
|
|
|
Routes["/tabs/create"] = {
|
2021-04-20 07:05:05 +02:00
|
|
|
usage: 'echo "https://www.google.com" > $0',
|
2021-03-22 03:42:44 +01:00
|
|
|
async write({buf}) {
|
2020-12-20 00:22:44 +01:00
|
|
|
const url = buf.trim();
|
|
|
|
await browser.tabs.create({url});
|
|
|
|
return {size: stringToUtf8Array(buf).length};
|
|
|
|
},
|
2021-03-22 03:42:44 +01:00
|
|
|
async truncate() { return {}; }
|
|
|
|
};
|
2020-12-20 00:22:44 +01:00
|
|
|
|
2021-04-20 07:05:05 +02:00
|
|
|
Routes["/tabs/by-id"] = {
|
|
|
|
usage: 'ls $0',
|
2020-12-12 00:45:19 +01:00
|
|
|
async readdir() {
|
|
|
|
const tabs = await browser.tabs.query({});
|
2020-12-14 07:02:29 +01:00
|
|
|
return { entries: [".", "..", ...tabs.map(tab => String(tab.id))] };
|
2020-12-12 00:45:19 +01:00
|
|
|
}
|
|
|
|
};
|
2020-12-12 11:36:37 +01:00
|
|
|
|
2020-12-12 00:45:19 +01:00
|
|
|
(function() {
|
2021-04-20 14:07:22 +02:00
|
|
|
const routeForTab = (readHandler, writeHandler) => routeWithContents(async ({tabId}) => {
|
2020-12-12 05:01:11 +01:00
|
|
|
const tab = await browser.tabs.get(tabId);
|
2020-12-15 21:11:41 +01:00
|
|
|
return readHandler(tab);
|
|
|
|
|
2021-03-22 03:42:44 +01:00
|
|
|
}, writeHandler ? async ({tabId}, buf) => {
|
2020-12-15 21:11:41 +01:00
|
|
|
await browser.tabs.update(tabId, writeHandler(buf));
|
|
|
|
} : undefined);
|
2021-03-22 03:42:44 +01:00
|
|
|
|
2021-04-20 14:07:22 +02:00
|
|
|
const routeFromScript = code => routeWithContents(async ({tabId}) => {
|
2020-12-12 05:01:11 +01:00
|
|
|
return (await browser.tabs.executeScript(tabId, {code}))[0];
|
|
|
|
});
|
|
|
|
|
2021-04-20 07:05:05 +02:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/url.txt"] = {
|
|
|
|
usage: ['cat $0',
|
|
|
|
'echo "https://www.google.com" > $0'],
|
|
|
|
...routeForTab(tab => tab.url + "\n",
|
|
|
|
buf => ({ url: buf }))
|
|
|
|
};
|
|
|
|
Routes["/tabs/by-id/#TAB_ID/title.txt"] = {
|
|
|
|
usage: 'cat $0',
|
|
|
|
...routeForTab(tab => tab.title + "\n")
|
|
|
|
};
|
|
|
|
Routes["/tabs/by-id/#TAB_ID/text.txt"] = {
|
|
|
|
usage: 'cat $0',
|
|
|
|
...routeFromScript(`document.body.innerText`)
|
|
|
|
};
|
|
|
|
Routes["/tabs/by-id/#TAB_ID/body.html"] = {
|
|
|
|
usage: 'cat $0',
|
|
|
|
...routeFromScript(`document.body.innerHTML`)
|
|
|
|
};
|
2021-02-19 19:02:39 +01:00
|
|
|
|
|
|
|
// echo true > mnt/tabs/by-id/1644/active
|
|
|
|
// cat mnt/tabs/by-id/1644/active
|
2021-04-20 07:05:05 +02:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/active"] = {
|
|
|
|
usage: ['cat $0',
|
|
|
|
'echo true > $0'],
|
|
|
|
...routeForTab(
|
|
|
|
tab => JSON.stringify(tab.active) + '\n',
|
|
|
|
// WEIRD: we do startsWith because you might end up with buf
|
|
|
|
// being "truee" (if it was "false", then someone wrote "true")
|
|
|
|
buf => ({ active: buf.startsWith("true") })
|
|
|
|
)
|
|
|
|
};
|
2020-12-12 00:45:19 +01:00
|
|
|
})();
|
2021-02-23 00:33:31 +01:00
|
|
|
(function() {
|
2021-02-26 00:41:47 +01:00
|
|
|
const evals = {};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/evals"] = {
|
2021-04-20 07:05:05 +02:00
|
|
|
usage: 'ls $0',
|
2021-03-21 23:35:01 +01:00
|
|
|
async readdir({path, tabId}) {
|
2021-02-26 00:41:47 +01:00
|
|
|
return { entries: [".", "..",
|
|
|
|
...Object.keys(evals[tabId] || {}),
|
|
|
|
...Object.keys(evals[tabId] || {}).map(f => f + '.result')] };
|
2021-02-23 00:33:31 +01:00
|
|
|
},
|
|
|
|
getattr() {
|
|
|
|
return {
|
2021-02-23 01:18:07 +01:00
|
|
|
st_mode: unix.S_IFDIR | 0777, // writable so you can create/rm evals
|
2021-02-23 00:33:31 +01:00
|
|
|
st_nlink: 3,
|
|
|
|
st_size: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/evals/:FILENAME"] = {
|
2021-04-20 07:05:05 +02:00
|
|
|
usage: ['cat $0.result',
|
|
|
|
'echo "2 + 2" > $0'],
|
|
|
|
|
2021-02-23 00:33:31 +01:00
|
|
|
// NOTE: eval runs in extension's content script, not in original page JS context
|
2021-03-22 03:42:44 +01:00
|
|
|
async mknod({tabId, filename, mode}) {
|
2021-02-23 00:33:31 +01:00
|
|
|
evals[tabId] = evals[tabId] || {};
|
2021-03-22 03:42:44 +01:00
|
|
|
evals[tabId][filename] = { code: '' };
|
2021-02-26 00:41:47 +01:00
|
|
|
return {};
|
|
|
|
},
|
2021-03-22 03:42:44 +01:00
|
|
|
async unlink({tabId, filename}) {
|
|
|
|
delete evals[tabId][filename]; // TODO: also delete evals[tabId] if empty
|
2021-02-26 00:41:47 +01:00
|
|
|
return {};
|
|
|
|
},
|
|
|
|
|
2021-04-20 14:07:22 +02:00
|
|
|
...routeWithContents(async ({tabId, filename}) => {
|
2021-02-26 00:41:47 +01:00
|
|
|
const name = filename.replace(/\.result$/, '');
|
|
|
|
if (!evals[tabId] || !(name in evals[tabId])) { throw new UnixError(unix.ENOENT); }
|
|
|
|
|
|
|
|
if (filename.endsWith('.result')) {
|
|
|
|
return evals[tabId][name].result || '';
|
|
|
|
} else {
|
|
|
|
return evals[tabId][name].code;
|
|
|
|
}
|
2021-03-22 03:42:44 +01:00
|
|
|
}, async ({tabId, filename}, buf) => {
|
|
|
|
if (filename.endsWith('.result')) {
|
|
|
|
// FIXME: case where they try to write to .result file
|
2021-02-26 00:41:47 +01:00
|
|
|
|
|
|
|
} else {
|
2021-03-22 03:42:44 +01:00
|
|
|
const name = filename;
|
2021-02-26 00:41:47 +01:00
|
|
|
evals[tabId][name].code = buf;
|
|
|
|
evals[tabId][name].result = JSON.stringify((await browser.tabs.executeScript(tabId, {code: buf}))[0]) + '\n';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
(function() {
|
|
|
|
const watches = {};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/watches"] = {
|
2021-03-22 03:42:44 +01:00
|
|
|
async readdir({tabId}) {
|
2021-02-26 00:41:47 +01:00
|
|
|
return { entries: [".", "..", ...Object.keys(watches[tabId] || [])] };
|
|
|
|
},
|
|
|
|
getattr() {
|
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFDIR | 0777, // writable so you can create/rm watches
|
|
|
|
st_nlink: 3,
|
|
|
|
st_size: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/watches/:EXPR"] = {
|
2021-02-26 00:41:47 +01:00
|
|
|
// NOTE: eval runs in extension's content script, not in original page JS context
|
2021-03-22 03:42:44 +01:00
|
|
|
async mknod({tabId, expr, mode}) {
|
2021-02-26 00:41:47 +01:00
|
|
|
watches[tabId] = watches[tabId] || {};
|
|
|
|
watches[tabId][expr] = async function() {
|
2021-02-23 00:33:31 +01:00
|
|
|
return (await browser.tabs.executeScript(tabId, {code: expr}))[0];
|
|
|
|
};
|
|
|
|
return {};
|
|
|
|
},
|
2021-03-22 03:42:44 +01:00
|
|
|
async unlink({tabId, expr}) {
|
2021-02-26 00:41:47 +01:00
|
|
|
delete watches[tabId][expr]; // TODO: also delete watches[tabId] if empty
|
2021-02-23 01:18:07 +01:00
|
|
|
return {};
|
|
|
|
},
|
2021-02-23 00:33:31 +01:00
|
|
|
|
2021-04-20 14:07:22 +02:00
|
|
|
...routeWithContents(async ({tabId, expr}) => {
|
2021-02-26 00:41:47 +01:00
|
|
|
if (!watches[tabId] || !(expr in watches[tabId])) { throw new UnixError(unix.ENOENT); }
|
|
|
|
return JSON.stringify(await watches[tabId][expr]()) + '\n';
|
2021-03-22 03:42:44 +01:00
|
|
|
|
2021-02-23 01:18:07 +01:00
|
|
|
}, () => {
|
|
|
|
// setData handler -- only providing this so that getattr reports
|
|
|
|
// that the file is writable, so it can be deleted without annoying prompt.
|
|
|
|
throw new UnixError(unix.EPERM);
|
2021-02-23 00:33:31 +01:00
|
|
|
})
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/window"] = {
|
2020-12-26 00:54:53 +01:00
|
|
|
// a symbolic link to /windows/[id for this window]
|
2021-03-22 03:42:44 +01:00
|
|
|
async readlink({tabId}) {
|
|
|
|
const tab = await browser.tabs.get(tabId);
|
2020-12-26 00:54:53 +01:00
|
|
|
return { buf: "../../../windows/" + tab.windowId };
|
|
|
|
}
|
|
|
|
};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/control"] = {
|
2020-12-25 22:59:35 +01:00
|
|
|
// echo remove > mnt/tabs/by-id/1644/control
|
2021-03-22 03:42:44 +01:00
|
|
|
async write({tabId, buf}) {
|
2020-11-06 04:09:44 +01:00
|
|
|
const command = buf.trim();
|
|
|
|
// can use `discard`, `remove`, `reload`, `goForward`, `goBack`...
|
|
|
|
// see https://developer.chrome.com/extensions/tabs
|
2020-12-15 21:11:41 +01:00
|
|
|
await browser.tabs[command](tabId);
|
2020-12-20 00:22:44 +01:00
|
|
|
return {size: stringToUtf8Array(buf).length};
|
2020-12-20 02:11:59 +01:00
|
|
|
},
|
2021-03-22 03:42:44 +01:00
|
|
|
async truncate({size}) { return {}; }
|
2020-10-28 04:53:39 +01:00
|
|
|
};
|
2020-12-25 22:59:35 +01:00
|
|
|
// debugger/ : debugger-API-dependent (Chrome-only)
|
2020-12-25 22:40:18 +01:00
|
|
|
(function() {
|
2021-01-01 03:00:25 +01:00
|
|
|
if (!chrome.debugger) return;
|
2021-03-22 03:42:44 +01:00
|
|
|
|
|
|
|
async function attachDebugger(tabId) {
|
|
|
|
return new Promise((resolve, reject) => chrome.debugger.attach({tabId}, "1.3", () => {
|
|
|
|
if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); }
|
|
|
|
else { resolve(); }
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
async function detachDebugger(tabId) {
|
|
|
|
return new Promise((resolve, reject) => chrome.debugger.detach({tabId}, () => {
|
|
|
|
if (chrome.runtime.lastError) { reject(chrome.runtime.lastError); }
|
|
|
|
else { resolve(); }
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
const TabManager = (function() {
|
|
|
|
if (chrome.debugger) chrome.debugger.onEvent.addListener((source, method, params) => {
|
|
|
|
console.log(source, method, params);
|
|
|
|
if (method === "Page.frameStartedLoading") {
|
|
|
|
// we're gonna assume we're always plugged into both Page and Debugger.
|
|
|
|
TabManager.scriptsForTab[source.tabId] = {};
|
|
|
|
|
|
|
|
} else if (method === "Debugger.scriptParsed") {
|
|
|
|
TabManager.scriptsForTab[source.tabId] = TabManager.scriptsForTab[source.tabId] || {};
|
|
|
|
TabManager.scriptsForTab[source.tabId][params.scriptId] = params;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
scriptsForTab: {},
|
|
|
|
debugTab: async function(tabId) {
|
|
|
|
// meant to be higher-level wrapper for raw attach/detach
|
|
|
|
// TODO: could we remember if we're already attached? idk if it's worth it
|
|
|
|
try { await attachDebugger(tabId); }
|
|
|
|
catch (e) {
|
|
|
|
if (e.message.indexOf('Another debugger is already attached') !== -1) {
|
|
|
|
await detachDebugger(tabId);
|
|
|
|
await attachDebugger(tabId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: detach automatically? some kind of reference counting thing?
|
|
|
|
},
|
|
|
|
enableDomainForTab: async function(tabId, domain) {
|
|
|
|
// TODO: could we remember if we're already enabled? idk if it's worth it
|
|
|
|
if (domain === 'Debugger') { TabManager.scriptsForTab[tabId] = {}; }
|
|
|
|
await sendDebuggerCommand(tabId, `${domain}.enable`, {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
function sendDebuggerCommand(tabId, method, commandParams) {
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
chrome.debugger.sendCommand({tabId}, method, commandParams, result => {
|
|
|
|
if (result) { resolve(result); } else { reject(chrome.runtime.lastError); }
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-01 02:33:11 +01:00
|
|
|
// possible idea: console (using Log API instead of monkey-patching)
|
2020-12-25 22:59:35 +01:00
|
|
|
// resources/
|
|
|
|
// TODO: scripts/ TODO: allow creation, eval immediately
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/debugger/resources"] = {
|
2021-03-22 03:42:44 +01:00
|
|
|
async readdir({tabId}) {
|
2020-12-25 22:40:18 +01:00
|
|
|
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Page");
|
|
|
|
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
2021-03-14 23:50:49 +01:00
|
|
|
return { entries: [".", "..", ...frameTree.resources.map(r => sanitize(String(r.url)))] };
|
2020-12-25 22:40:18 +01:00
|
|
|
}
|
|
|
|
};
|
2021-04-20 14:07:22 +02:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/debugger/resources/:SUFFIX"] = routeWithContents(async ({path, tabId, suffix}) => {
|
2020-12-25 22:40:18 +01:00
|
|
|
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Page");
|
|
|
|
|
|
|
|
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
|
|
|
for (let resource of frameTree.resources) {
|
2021-03-14 23:50:49 +01:00
|
|
|
const resourceSuffix = sanitize(String(resource.url));
|
2020-12-25 22:40:18 +01:00
|
|
|
if (resourceSuffix === suffix) {
|
|
|
|
let {base64Encoded, content} = await sendDebuggerCommand(tabId, "Page.getResourceContent", {
|
|
|
|
frameId: frameTree.frame.id,
|
|
|
|
url: resource.url
|
|
|
|
});
|
|
|
|
if (base64Encoded) { return Uint8Array.from(atob(content), c => c.charCodeAt(0)); }
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new UnixError(unix.ENOENT);
|
|
|
|
});
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/debugger/scripts"] = {
|
2021-03-22 03:42:44 +01:00
|
|
|
async opendir({tabId}) {
|
2020-12-25 22:40:18 +01:00
|
|
|
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Debugger");
|
|
|
|
return { fh: 0 };
|
|
|
|
},
|
2021-03-22 03:42:44 +01:00
|
|
|
async readdir({tabId}) {
|
|
|
|
// it's useful to put the ID first in the script filenames, so
|
|
|
|
// the .js extension stays on the end
|
2020-12-29 13:27:12 +01:00
|
|
|
const scriptFileNames = Object.values(TabManager.scriptsForTab[tabId])
|
2021-03-14 23:50:49 +01:00
|
|
|
.map(params => params.scriptId + "_" + sanitize(params.url));
|
2020-12-29 13:27:12 +01:00
|
|
|
return { entries: [".", "..", ...scriptFileNames] };
|
2020-12-25 22:40:18 +01:00
|
|
|
}
|
|
|
|
};
|
2021-03-22 03:51:31 +01:00
|
|
|
function pathScriptInfo(tabId, filename) {
|
|
|
|
const [scriptId, ...rest] = filename.split("_");
|
2020-12-29 23:16:25 +01:00
|
|
|
const scriptInfo = TabManager.scriptsForTab[tabId][scriptId];
|
2021-03-14 23:50:49 +01:00
|
|
|
if (!scriptInfo || sanitize(scriptInfo.url) !== rest.join("_")) {
|
2020-12-29 23:16:25 +01:00
|
|
|
throw new UnixError(unix.ENOENT);
|
|
|
|
}
|
|
|
|
return scriptInfo;
|
|
|
|
}
|
2021-04-20 14:07:22 +02:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/debugger/scripts/:FILENAME"] = routeWithContents(async ({tabId, filename}) => {
|
2020-12-25 22:40:18 +01:00
|
|
|
await TabManager.debugTab(tabId);
|
|
|
|
await TabManager.enableDomainForTab(tabId, "Page");
|
|
|
|
await TabManager.enableDomainForTab(tabId, "Debugger");
|
|
|
|
|
2021-03-22 03:51:31 +01:00
|
|
|
const {scriptId} = pathScriptInfo(tabId, filename);
|
2020-12-25 22:40:18 +01:00
|
|
|
const {scriptSource} = await sendDebuggerCommand(tabId, "Debugger.getScriptSource", {scriptId});
|
|
|
|
return scriptSource;
|
|
|
|
|
2021-03-22 03:51:31 +01:00
|
|
|
}, async ({tabId, filename}, buf) => {
|
2020-12-25 22:40:18 +01:00
|
|
|
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Debugger");
|
|
|
|
|
2021-03-22 03:51:31 +01:00
|
|
|
const {scriptId} = pathScriptInfo(tabId, filename);
|
2020-12-25 22:40:18 +01:00
|
|
|
await sendDebuggerCommand(tabId, "Debugger.setScriptSource", {scriptId, scriptSource: buf});
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/inputs"] = {
|
2021-03-22 03:51:31 +01:00
|
|
|
async readdir({tabId}) {
|
2021-02-19 20:26:33 +01:00
|
|
|
// TODO: assign new IDs to inputs without them?
|
2021-03-21 23:35:01 +01:00
|
|
|
const code = `Array.from(document.querySelectorAll('textarea, input[type=text]'))
|
|
|
|
.map(e => e.id).filter(id => id)`;
|
2021-01-11 07:55:34 +01:00
|
|
|
const ids = (await browser.tabs.executeScript(tabId, {code}))[0];
|
|
|
|
return { entries: [".", "..", ...ids.map(id => `${id}.txt`)] };
|
|
|
|
}
|
|
|
|
};
|
2021-04-20 14:07:22 +02:00
|
|
|
Routes["/tabs/by-id/#TAB_ID/inputs/:INPUT_ID.txt"] = routeWithContents(async ({tabId, inputId}) => {
|
2021-02-22 05:43:31 +01:00
|
|
|
const code = `document.getElementById('${inputId}').value`;
|
|
|
|
const inputValue = (await browser.tabs.executeScript(tabId, {code}))[0];
|
|
|
|
if (inputValue === null) { throw new UnixError(unix.ENOENT); } /* FIXME: hack to deal with if inputId isn't valid */
|
|
|
|
return inputValue;
|
2021-03-22 03:42:44 +01:00
|
|
|
|
2021-03-22 03:51:31 +01:00
|
|
|
}, async ({tabId, inputId}, buf) => {
|
2021-02-22 05:43:31 +01:00
|
|
|
const code = `document.getElementById('${inputId}').value = unescape('${escape(buf)}')`;
|
2021-01-11 07:55:34 +01:00
|
|
|
await browser.tabs.executeScript(tabId, {code});
|
|
|
|
});
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-title"] = {
|
2020-12-26 00:54:53 +01:00
|
|
|
getattr() {
|
2020-12-14 07:02:29 +01:00
|
|
|
return {
|
2020-12-26 00:54:53 +01:00
|
|
|
st_mode: unix.S_IFDIR | 0777, // writable so you can delete tabs
|
2020-12-14 07:02:29 +01:00
|
|
|
st_nlink: 3,
|
|
|
|
st_size: 0,
|
|
|
|
};
|
|
|
|
},
|
2020-12-01 01:01:56 +01:00
|
|
|
async readdir() {
|
2020-10-28 04:53:39 +01:00
|
|
|
const tabs = await browser.tabs.query({});
|
2021-03-22 03:42:44 +01:00
|
|
|
return { entries: [".", "..", ...tabs.map(tab => sanitize(String(tab.title)) + "." + String(tab.id))] };
|
2020-10-28 04:53:39 +01:00
|
|
|
}
|
|
|
|
};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/by-title/:TAB_TITLE.#TAB_ID"] = {
|
2020-12-26 00:54:53 +01:00
|
|
|
// TODO: date
|
2021-03-22 03:42:44 +01:00
|
|
|
async readlink({tabId}) { // a symbolic link to /tabs/by-id/[id for this tab]
|
2020-12-15 21:11:41 +01:00
|
|
|
return { buf: "../by-id/" + tabId };
|
|
|
|
},
|
2021-03-22 03:42:44 +01:00
|
|
|
async unlink({tabId}) { // you can delete a by-title/TAB to close that tab
|
2020-12-15 21:11:41 +01:00
|
|
|
await browser.tabs.remove(tabId);
|
|
|
|
return {};
|
2020-10-28 04:53:39 +01:00
|
|
|
}
|
|
|
|
};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/tabs/last-focused"] = {
|
2020-11-22 20:52:08 +01:00
|
|
|
// a symbolic link to /tabs/by-id/[id for this tab]
|
2021-03-22 03:42:44 +01:00
|
|
|
async readlink() {
|
2020-12-25 23:55:35 +01:00
|
|
|
const id = (await browser.tabs.query({ active: true, lastFocusedWindow: true }))[0].id;
|
2020-12-01 01:01:56 +01:00
|
|
|
return { buf: "by-id/" + id };
|
2020-11-22 20:52:08 +01:00
|
|
|
}
|
2020-12-25 23:55:35 +01:00
|
|
|
};
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/windows"] = {
|
2020-12-25 23:55:35 +01:00
|
|
|
async readdir() {
|
|
|
|
const windows = await browser.windows.getAll();
|
|
|
|
return { entries: [".", "..", ...windows.map(window => String(window.id))] };
|
|
|
|
}
|
|
|
|
};
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/windows/last-focused"] = {
|
2021-02-22 05:43:31 +01:00
|
|
|
// a symbolic link to /windows/[id for this window]
|
2021-03-22 03:42:44 +01:00
|
|
|
async readlink() {
|
2021-02-22 05:43:31 +01:00
|
|
|
const windowId = (await browser.windows.getLastFocused()).id;
|
|
|
|
return { buf: windowId };
|
|
|
|
}
|
|
|
|
};
|
2021-02-19 19:32:28 +01:00
|
|
|
(function() {
|
2021-04-20 14:07:22 +02:00
|
|
|
const withWindow = (readHandler, writeHandler) => routeWithContents(async ({windowId}) => {
|
2021-02-19 19:32:28 +01:00
|
|
|
const window = await browser.windows.get(windowId);
|
|
|
|
return readHandler(window);
|
|
|
|
|
2021-03-22 03:42:44 +01:00
|
|
|
}, writeHandler ? async ({windowId}, buf) => {
|
2021-02-19 19:32:28 +01:00
|
|
|
await browser.windows.update(windowId, writeHandler(buf));
|
|
|
|
} : undefined);
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/windows/#WINDOW_ID/focused"] =
|
2021-03-22 03:42:44 +01:00
|
|
|
withWindow(window => JSON.stringify(window.focused) + '\n',
|
|
|
|
buf => ({ focused: buf.startsWith('true') }));
|
2021-02-19 19:32:28 +01:00
|
|
|
})();
|
2021-04-20 14:07:22 +02:00
|
|
|
Routes["/windows/#WINDOW_ID/visible-tab.png"] = { ...routeWithContents(async ({windowId}) => {
|
2021-02-22 05:43:31 +01:00
|
|
|
// screen capture is a window thing and not a tab thing because you
|
2020-12-28 19:41:15 +01:00
|
|
|
// can only capture the visible tab for each window anyway; you
|
|
|
|
// can't take a screenshot of just any arbitrary tab
|
2020-12-25 23:55:35 +01:00
|
|
|
const dataUrl = await browser.tabs.captureVisibleTab(windowId, {format: 'png'});
|
2020-12-26 00:13:04 +01:00
|
|
|
return Uint8Array.from(atob(dataUrl.substr(("data:image/png;base64,").length)),
|
|
|
|
c => c.charCodeAt(0));
|
2020-12-25 23:55:35 +01:00
|
|
|
|
2021-03-22 03:42:44 +01:00
|
|
|
}), async getattr() {
|
2020-12-25 23:55:35 +01:00
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFREG | 0444,
|
|
|
|
st_nlink: 1,
|
|
|
|
st_size: 10000000 // hard-code to 10MB for now
|
|
|
|
};
|
|
|
|
} };
|
|
|
|
|
2020-11-22 20:52:08 +01:00
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/extensions"] = {
|
2020-12-23 01:36:09 +01:00
|
|
|
async readdir() {
|
|
|
|
const infos = await browser.management.getAll();
|
2021-03-22 03:42:44 +01:00
|
|
|
return { entries: [".", "..", ...infos.map(info => `${sanitize(info.name)}.${info.id}`)] };
|
2020-12-23 01:36:09 +01:00
|
|
|
}
|
|
|
|
};
|
2021-04-20 14:07:22 +02:00
|
|
|
Routes["/extensions/:EXTENSION_TITLE.:EXTENSION_ID/enabled"] = { ...routeWithContents(async ({extensionId}) => {
|
2020-12-23 05:34:01 +01:00
|
|
|
const info = await browser.management.get(extensionId);
|
2020-12-23 13:18:26 +01:00
|
|
|
return String(info.enabled) + '\n';
|
2020-12-23 05:34:01 +01:00
|
|
|
|
2021-03-22 03:42:44 +01:00
|
|
|
}, async ({extensionId}, buf) => {
|
2020-12-23 13:18:26 +01:00
|
|
|
await browser.management.setEnabled(extensionId, buf.trim() === "true");
|
2020-12-25 08:46:48 +01:00
|
|
|
|
|
|
|
// suppress truncate so it doesn't accidentally flip the state when you do, e.g., `echo true >`
|
|
|
|
}), truncate() { return {}; } };
|
2020-12-23 01:36:09 +01:00
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes["/runtime/reload"] = {
|
2021-03-22 03:42:44 +01:00
|
|
|
async write({buf}) {
|
2020-12-24 22:49:38 +01:00
|
|
|
await browser.runtime.reload();
|
|
|
|
return {size: stringToUtf8Array(buf).length};
|
|
|
|
},
|
2020-12-25 23:55:35 +01:00
|
|
|
truncate() { return {}; }
|
2020-12-24 22:49:38 +01:00
|
|
|
};
|
|
|
|
|
2021-04-20 14:07:22 +02:00
|
|
|
// added at first to make development on Safari less painful: Safari
|
|
|
|
// normally requires you to recompile the whole Xcode project to
|
|
|
|
// deploy any update to background.js.
|
|
|
|
Routes["/runtime/background.js"] = {
|
|
|
|
usage: '',
|
|
|
|
...routeWithContents(
|
|
|
|
async () => {
|
|
|
|
// `window.backgroundJS` is the source code of the file you're
|
|
|
|
// reading right now! it needs to be a global because we want
|
|
|
|
// its value (the changed JS text) to survive even as this whole
|
|
|
|
// module gets re-evaluated.
|
|
|
|
window.backgroundJS = window.backgroundJS ||
|
|
|
|
await window.fetch(chrome.runtime.getURL('background.js'))
|
|
|
|
.then(r => r.text());
|
|
|
|
return window.backgroundJS;
|
|
|
|
},
|
|
|
|
async ({}, buf) => { window.backgroundJS = buf; }
|
|
|
|
),
|
|
|
|
release({fh}) {
|
|
|
|
// Note that we eval on release, not on write.
|
|
|
|
eval(window.backgroundJS);
|
|
|
|
// TODO: would be better if we could call 'super'.release() so
|
|
|
|
// we wouldn't need to involve how Cache works here.
|
|
|
|
routeWithContents.Cache.removeObjectForHandle(fh);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Routes["/runtime/background.js.html"] = routeWithContents(async () => {
|
|
|
|
const js = await window.fetch(chrome.runtime.getURL('background.js'))
|
|
|
|
.then(r => r.text());
|
|
|
|
|
|
|
|
const classes = [
|
|
|
|
[/Routes\["[^\]]+"\] = /, 'route']
|
|
|
|
];
|
|
|
|
|
|
|
|
const classedJs =
|
|
|
|
js.split('\n')
|
|
|
|
.map((line, i) => {
|
|
|
|
const class_ = classes.find(([re, class_]) => re.test(line));
|
|
|
|
line = line
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'");
|
|
|
|
if (!class_) { return `<div class="normal line">${line}</div>`; }
|
|
|
|
return `<div class="${class_[1]} line">${line}</div>`;
|
|
|
|
})
|
|
|
|
.join('');
|
|
|
|
|
|
|
|
return `
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<style>
|
|
|
|
body { overflow-x: hidden; }
|
|
|
|
.route { background-color: rgb(255, 196, 196); }
|
|
|
|
.line { position: absolute; height: 15px; width: 100%; }
|
|
|
|
.line { transition: height 0.5s cubic-bezier(0.64, 0.08, 0.24, 1), transform 0.5s cubic-bezier(0.64, 0.08, 0.24, 1); }
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<!-- <dl>
|
|
|
|
${Object.entries(Routes).map(([a, b]) => `
|
|
|
|
<dt>${a}</dt>
|
|
|
|
<dd>${b}</dd>
|
|
|
|
`).join('\n')}
|
|
|
|
</dl> -->
|
|
|
|
<pre><code>${classedJs}</code></pre>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const lines = [...document.querySelectorAll('div.line')];
|
|
|
|
function render() {
|
|
|
|
let y = 0;
|
|
|
|
for (let line of lines) {
|
|
|
|
if (line.classList.contains('route') || line.dataset.expand == 'true') {
|
|
|
|
line.style.height = '15px';
|
|
|
|
line.style.transform = 'translate(0px, ' + y + 'px)';
|
|
|
|
y += 15;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
line.style.height = '15px';
|
|
|
|
line.style.transform = 'translate(0px, ' + (y - 7.5) + 'px) scaleY(' + 2/15 + ')';
|
|
|
|
y += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
|
|
|
|
for (let line of lines) {
|
|
|
|
function treatNeighborLines(line, expand) {
|
|
|
|
let neighborLine = line;
|
|
|
|
while (neighborLine && !neighborLine.classList.contains('route')) {
|
|
|
|
neighborLine.dataset.expand = expand;
|
|
|
|
neighborLine = neighborLine.nextElementSibling;
|
|
|
|
}
|
|
|
|
neighborLine = line;
|
|
|
|
while (neighborLine && !neighborLine.classList.contains('route')) {
|
|
|
|
neighborLine.dataset.expand = expand;
|
|
|
|
neighborLine = neighborLine.previousElementSibling;
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
line.onmousedown = () => {
|
|
|
|
treatNeighborLines(line, true);
|
|
|
|
document.body.onmouseup = () => { treatNeighborLines(line, false); };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
|
2020-10-29 23:56:57 +01:00
|
|
|
// Ensure that there are routes for all ancestors. This algorithm is
|
2020-12-25 22:40:18 +01:00
|
|
|
// probably not correct, but whatever. Basically, you need to start at
|
|
|
|
// the deepest level, fill in all the parents 1 level up that don't
|
|
|
|
// exist yet, then walk up one level at a time. It's important to go
|
|
|
|
// one level at a time so you know (for each parent) what all the
|
|
|
|
// children will be.
|
2020-12-24 22:49:38 +01:00
|
|
|
for (let i = 10; i >= 0; i--) {
|
2021-03-23 07:25:38 +01:00
|
|
|
for (let path of Object.keys(Routes).filter(key => key.split("/").length === i)) {
|
2020-10-28 04:53:39 +01:00
|
|
|
path = path.substr(0, path.lastIndexOf("/"));
|
2020-10-28 22:14:57 +01:00
|
|
|
if (path == '') path = '/';
|
2020-10-28 04:53:39 +01:00
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
if (!Routes[path]) {
|
2020-12-23 03:32:08 +01:00
|
|
|
function depth(p) { return p === '/' ? 0 : (p.match(/\//g) || []).length; }
|
|
|
|
|
2020-10-28 04:53:39 +01:00
|
|
|
// find all direct children
|
2021-03-23 07:25:38 +01:00
|
|
|
let entries = Object.keys(Routes)
|
2020-12-23 03:32:08 +01:00
|
|
|
.filter(k => k.startsWith(path) && depth(k) === depth(path) + 1)
|
|
|
|
.map(k => k.substr((path === '/' ? 0 : path.length) + 1).split('/')[0]);
|
|
|
|
entries = [".", "..", ...new Set(entries)];
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes[path] = { readdir() { return { entries }; } };
|
2018-11-15 08:52:26 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-25 22:40:18 +01:00
|
|
|
// I also think it would be better to compute this stuff on the fly,
|
|
|
|
// so you could patch more routes in at runtime, but I need to think
|
|
|
|
// a bit about how to make that work with wildcards.
|
2020-10-28 04:53:39 +01:00
|
|
|
}
|
2020-12-02 22:45:20 +01:00
|
|
|
|
2021-03-21 23:35:01 +01:00
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
for (let key in Routes) {
|
2021-03-22 03:42:44 +01:00
|
|
|
// /tabs/by-id/#TAB_ID/url.txt -> RegExp \/tabs\/by-id\/(?<int$TAB_ID>[0-9]+)\/url.txt
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes[key].__regex = new RegExp(
|
2021-03-21 23:35:01 +01:00
|
|
|
'^' + key
|
|
|
|
.split('/')
|
|
|
|
.map(keySegment => keySegment
|
|
|
|
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
|
|
.replace(/([#:])([A-Z_]+)/g, (_, sigil, varName) => {
|
2021-03-22 03:42:44 +01:00
|
|
|
return `(?<${sigil === '#' ? 'int$' : 'string$'}${varName}>` +
|
|
|
|
(sigil === '#' ? '[0-9]+' : '[^/]+') + `)`;
|
2021-03-21 23:35:01 +01:00
|
|
|
}))
|
|
|
|
.join('/') + '$');
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
Routes[key].__match = function(path) {
|
|
|
|
const result = Routes[key].__regex.exec(path);
|
2021-03-21 23:35:01 +01:00
|
|
|
if (!result) { return; }
|
|
|
|
|
|
|
|
const vars = {};
|
2021-03-22 03:42:44 +01:00
|
|
|
for (let [typeAndVarName, value] of Object.entries(result.groups || {})) {
|
2021-03-21 23:35:01 +01:00
|
|
|
let [type_, varName] = typeAndVarName.split('$');
|
|
|
|
// TAB_ID -> tabId
|
|
|
|
varName = varName.toLowerCase();
|
|
|
|
varName = varName.replace(/_([a-z])/g, c => c[1].toUpperCase());
|
|
|
|
vars[varName] = type_ === 'int' ? parseInt(value) : value;
|
|
|
|
}
|
|
|
|
return vars;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fill in default implementations of fs ops:
|
|
|
|
|
2020-12-02 22:45:20 +01:00
|
|
|
// if readdir -> directory -> add getattr, opendir, releasedir
|
2021-03-23 07:25:38 +01:00
|
|
|
if (Routes[key].readdir) {
|
|
|
|
Routes[key] = {
|
2020-12-02 22:45:20 +01:00
|
|
|
getattr() {
|
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFDIR | 0755,
|
|
|
|
st_nlink: 3,
|
|
|
|
st_size: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
opendir({path}) { return { fh: 0 }; },
|
|
|
|
releasedir({path}) { return {}; },
|
2021-03-23 07:25:38 +01:00
|
|
|
...Routes[key]
|
2020-12-02 22:45:20 +01:00
|
|
|
};
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
} else if (Routes[key].readlink) {
|
|
|
|
Routes[key] = {
|
2021-03-22 03:42:44 +01:00
|
|
|
async getattr(req) {
|
|
|
|
const st_size = (await this.readlink(req)).buf.length + 1;
|
2020-12-02 22:45:20 +01:00
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFLNK | 0444,
|
|
|
|
st_nlink: 1,
|
|
|
|
// You _must_ return correct linkee path length from getattr!
|
|
|
|
st_size
|
|
|
|
};
|
|
|
|
},
|
2021-03-23 07:25:38 +01:00
|
|
|
...Routes[key]
|
2020-12-02 22:45:20 +01:00
|
|
|
};
|
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
} else if (Routes[key].read || Routes[key].write) {
|
|
|
|
Routes[key] = {
|
2020-12-02 22:45:20 +01:00
|
|
|
async getattr() {
|
|
|
|
return {
|
2021-03-23 07:25:38 +01:00
|
|
|
st_mode: unix.S_IFREG | ((Routes[key].read && 0444) | (Routes[key].write && 0222)),
|
2020-12-02 22:45:20 +01:00
|
|
|
st_nlink: 1,
|
|
|
|
st_size: 100 // FIXME
|
|
|
|
};
|
|
|
|
},
|
2021-03-21 23:35:01 +01:00
|
|
|
open() { return { fh: 0 }; },
|
|
|
|
release() { return {}; },
|
2021-03-23 07:25:38 +01:00
|
|
|
...Routes[key]
|
2020-12-02 22:45:20 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 03:42:44 +01:00
|
|
|
function tryMatchRoute(path) {
|
2021-03-21 23:35:01 +01:00
|
|
|
if (path.match(/\/\._[^\/]+$/)) {
|
2021-03-22 03:42:44 +01:00
|
|
|
// Apple Double ._whatever file for xattrs
|
|
|
|
throw new UnixError(unix.ENOTSUP);
|
2019-03-02 10:48:56 +01:00
|
|
|
}
|
2018-11-12 01:31:02 +01:00
|
|
|
|
2021-03-23 07:25:38 +01:00
|
|
|
for (let route of Object.values(Routes)) {
|
2021-03-22 03:42:44 +01:00
|
|
|
const vars = route.__match(path);
|
|
|
|
if (vars) { return [route, vars]; }
|
2018-11-12 01:31:02 +01:00
|
|
|
}
|
2021-03-21 23:35:01 +01:00
|
|
|
throw new UnixError(unix.ENOENT);
|
2018-11-22 11:52:54 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 04:53:39 +01:00
|
|
|
let port;
|
|
|
|
async function onMessage(req) {
|
2020-12-03 01:57:06 +01:00
|
|
|
if (req.buf) req.buf = atob(req.buf);
|
2020-12-12 11:36:37 +01:00
|
|
|
console.log('req', req);
|
2018-11-29 18:53:03 +01:00
|
|
|
|
2020-10-28 04:53:39 +01:00
|
|
|
let response = { op: req.op, error: unix.EIO };
|
2020-12-25 08:46:48 +01:00
|
|
|
let didTimeout = false, timeout = setTimeout(() => {
|
|
|
|
// timeout is very useful because some operations just hang
|
|
|
|
// (like trying to take a screenshot, until the tab is focused)
|
|
|
|
didTimeout = true; console.error('timeout');
|
2021-01-03 11:23:20 +01:00
|
|
|
port.postMessage({ id: req.id, op: req.op, error: unix.ETIMEDOUT });
|
2020-12-25 08:46:48 +01:00
|
|
|
}, 1000);
|
|
|
|
|
2020-10-28 04:53:39 +01:00
|
|
|
/* console.time(req.op + ':' + req.path);*/
|
|
|
|
try {
|
2021-03-22 03:42:44 +01:00
|
|
|
const [route, vars] = tryMatchRoute(req.path);
|
|
|
|
response = await route[req.op]({...req, ...vars});
|
2020-10-28 04:53:39 +01:00
|
|
|
response.op = req.op;
|
2020-12-04 18:58:09 +01:00
|
|
|
if (response.buf) { response.buf = btoa(response.buf); }
|
2018-11-29 18:53:03 +01:00
|
|
|
|
2018-11-22 11:52:54 +01:00
|
|
|
} catch (e) {
|
2019-02-26 08:08:52 +01:00
|
|
|
console.error(e);
|
2018-11-11 20:44:36 +01:00
|
|
|
response = {
|
2018-11-22 11:52:54 +01:00
|
|
|
op: req.op,
|
|
|
|
error: e instanceof UnixError ? e.error : unix.EIO
|
2021-03-21 23:35:01 +01:00
|
|
|
};
|
2018-11-12 01:18:21 +01:00
|
|
|
}
|
2018-11-24 19:58:33 +01:00
|
|
|
/* console.timeEnd(req.op + ':' + req.path);*/
|
2018-11-11 14:32:44 +01:00
|
|
|
|
2020-12-25 08:46:48 +01:00
|
|
|
if (!didTimeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
console.log('resp', response);
|
2021-01-03 11:23:20 +01:00
|
|
|
response.id = req.id;
|
2020-12-25 08:46:48 +01:00
|
|
|
port.postMessage(response);
|
|
|
|
}
|
2018-11-11 14:32:44 +01:00
|
|
|
};
|
2018-11-29 18:53:03 +01:00
|
|
|
|
|
|
|
function tryConnect() {
|
2021-04-20 14:07:22 +02:00
|
|
|
// so we don't try to reconnect if we're hot-swapping background.js.
|
|
|
|
if (window.isConnected) return;
|
|
|
|
|
2021-02-03 08:07:24 +01:00
|
|
|
// Safari is very weird -- it has this native app that we have to talk to,
|
2021-02-08 11:32:21 +01:00
|
|
|
// so we poke that app to wake it up, get it to start the TabFS process
|
|
|
|
// and boot a WebSocket, then connect to it.
|
2021-02-03 08:07:24 +01:00
|
|
|
// Is there a better way to do this?
|
|
|
|
if (chrome.runtime.getURL('/').startsWith('safari-web-extension://')) { // Safari-only
|
2021-02-08 11:32:21 +01:00
|
|
|
chrome.runtime.sendNativeMessage('com.rsnous.tabfs', {op: 'safari_did_connect'}, resp => {
|
2021-02-03 08:07:24 +01:00
|
|
|
console.log(resp);
|
2021-02-08 11:32:21 +01:00
|
|
|
|
2021-02-08 22:45:26 +01:00
|
|
|
let socket;
|
|
|
|
function connectSocket(checkAfterTime) {
|
|
|
|
socket = new WebSocket('ws://localhost:9991');
|
|
|
|
socket.addEventListener('message', event => {
|
|
|
|
onMessage(JSON.parse(event.data));
|
|
|
|
});
|
|
|
|
|
|
|
|
port = { postMessage(message) {
|
|
|
|
socket.send(JSON.stringify(message));
|
|
|
|
} };
|
2021-02-08 11:32:21 +01:00
|
|
|
|
2021-02-08 22:45:26 +01:00
|
|
|
setTimeout(() => {
|
2021-04-20 14:07:22 +02:00
|
|
|
if (socket.readyState === 1) {
|
|
|
|
window.isConnected = true;
|
|
|
|
|
|
|
|
} else {
|
2021-02-08 22:45:26 +01:00
|
|
|
console.log('ws connection failed, retrying in', checkAfterTime);
|
|
|
|
connectSocket(checkAfterTime * 2);
|
|
|
|
}
|
|
|
|
}, checkAfterTime);
|
|
|
|
}
|
|
|
|
connectSocket(200);
|
2021-02-03 08:07:24 +01:00
|
|
|
});
|
2021-02-08 11:32:21 +01:00
|
|
|
return;
|
2021-02-03 08:07:24 +01:00
|
|
|
}
|
2021-02-08 11:32:21 +01:00
|
|
|
|
|
|
|
port = chrome.runtime.connectNative('com.rsnous.tabfs');
|
2021-04-20 14:07:22 +02:00
|
|
|
port.onConnect.addListener(() => { window.isConnected = true; });
|
2021-02-08 11:32:21 +01:00
|
|
|
port.onMessage.addListener(onMessage);
|
2021-04-20 14:07:22 +02:00
|
|
|
port.onDisconnect.addListener(p => {
|
|
|
|
window.isConnected = false;
|
|
|
|
console.log('disconnect', p);
|
|
|
|
});
|
2018-11-29 18:53:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-15 05:33:23 +01:00
|
|
|
|
|
|
|
if (typeof process === 'object') {
|
|
|
|
// we're running in node (as part of a test)
|
|
|
|
// return everything they might want to test
|
2021-03-23 07:25:38 +01:00
|
|
|
module.exports = {Routes, tryMatchRoute};
|
2021-03-15 05:33:23 +01:00
|
|
|
|
|
|
|
} else {
|
2018-11-29 18:53:03 +01:00
|
|
|
tryConnect();
|
2020-10-28 04:53:39 +01:00
|
|
|
}
|