extension: Eliminate pathComponent entirely.

This commit is contained in:
Omar Rizwan 2021-03-21 19:51:31 -07:00
parent b6f44d95f4
commit 5ef6929ae7

View file

@ -25,10 +25,6 @@ class UnixError extends Error {
constructor(error) { super(); this.name = "UnixError"; this.error = error; } constructor(error) { super(); this.name = "UnixError"; this.error = error; }
} }
function pathComponent(path, i) {
const components = path.split('/');
return components[i >= 0 ? i : components.length + i];
}
const sanitize = (function() { const sanitize = (function() {
// from https://github.com/parshap/node-sanitize-filename/blob/209c39b914c8eb48ee27bcbde64b2c7822fdf3de/index.js // from https://github.com/parshap/node-sanitize-filename/blob/209c39b914c8eb48ee27bcbde64b2c7822fdf3de/index.js
@ -398,34 +394,33 @@ router["/tabs/by-id/#TAB_ID/control"] = {
return { entries: [".", "..", ...scriptFileNames] }; return { entries: [".", "..", ...scriptFileNames] };
} }
}; };
function pathScriptInfo(tabId, path) { function pathScriptInfo(tabId, filename) {
const [scriptId, ...rest] = pathComponent(path, -1).split("_"); const [scriptId, ...rest] = filename.split("_");
const scriptInfo = TabManager.scriptsForTab[tabId][scriptId]; const scriptInfo = TabManager.scriptsForTab[tabId][scriptId];
if (!scriptInfo || sanitize(scriptInfo.url) !== rest.join("_")) { if (!scriptInfo || sanitize(scriptInfo.url) !== rest.join("_")) {
throw new UnixError(unix.ENOENT); throw new UnixError(unix.ENOENT);
} }
return scriptInfo; return scriptInfo;
} }
router["/tabs/by-id/#TAB_ID/debugger/scripts/:SUFFIX"] = defineFile(async ({path, tabId, suffix}) => { router["/tabs/by-id/#TAB_ID/debugger/scripts/:FILENAME"] = defineFile(async ({tabId, filename}) => {
await TabManager.debugTab(tabId); await TabManager.debugTab(tabId);
await TabManager.enableDomainForTab(tabId, "Page"); await TabManager.enableDomainForTab(tabId, "Page");
await TabManager.enableDomainForTab(tabId, "Debugger"); await TabManager.enableDomainForTab(tabId, "Debugger");
const {scriptId} = pathScriptInfo(tabId, path); const {scriptId} = pathScriptInfo(tabId, filename);
const {scriptSource} = await sendDebuggerCommand(tabId, "Debugger.getScriptSource", {scriptId}); const {scriptSource} = await sendDebuggerCommand(tabId, "Debugger.getScriptSource", {scriptId});
return scriptSource; return scriptSource;
}, async ({path, tabId, suffix}, buf) => { }, async ({tabId, filename}, buf) => {
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Debugger"); await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Debugger");
const {scriptId} = pathScriptInfo(tabId, path); const {scriptId} = pathScriptInfo(tabId, filename);
await sendDebuggerCommand(tabId, "Debugger.setScriptSource", {scriptId, scriptSource: buf}); await sendDebuggerCommand(tabId, "Debugger.setScriptSource", {scriptId, scriptSource: buf});
}); });
})(); })();
router["/tabs/by-id/#TAB_ID/inputs"] = { router["/tabs/by-id/#TAB_ID/inputs"] = {
async readdir({path}) { async readdir({tabId}) {
const tabId = parseInt(pathComponent(path, -2));
// TODO: assign new IDs to inputs without them? // TODO: assign new IDs to inputs without them?
const code = `Array.from(document.querySelectorAll('textarea, input[type=text]')) const code = `Array.from(document.querySelectorAll('textarea, input[type=text]'))
.map(e => e.id).filter(id => id)`; .map(e => e.id).filter(id => id)`;
@ -433,13 +428,13 @@ router["/tabs/by-id/#TAB_ID/inputs"] = {
return { entries: [".", "..", ...ids.map(id => `${id}.txt`)] }; return { entries: [".", "..", ...ids.map(id => `${id}.txt`)] };
} }
}; };
router["/tabs/by-id/#TAB_ID/inputs/:INPUT_ID.txt"] = defineFile(async ({path, tabId, inputId}) => { router["/tabs/by-id/#TAB_ID/inputs/:INPUT_ID.txt"] = defineFile(async ({tabId, inputId}) => {
const code = `document.getElementById('${inputId}').value`; const code = `document.getElementById('${inputId}').value`;
const inputValue = (await browser.tabs.executeScript(tabId, {code}))[0]; 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 */ if (inputValue === null) { throw new UnixError(unix.ENOENT); } /* FIXME: hack to deal with if inputId isn't valid */
return inputValue; return inputValue;
}, async ({path, tabId, inputId}, buf) => { }, async ({tabId, inputId}, buf) => {
const code = `document.getElementById('${inputId}').value = unescape('${escape(buf)}')`; const code = `document.getElementById('${inputId}').value = unescape('${escape(buf)}')`;
await browser.tabs.executeScript(tabId, {code}); await browser.tabs.executeScript(tabId, {code});
}); });