From b8258aaa019de66081444dedd3c6020f53a5296f Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Sun, 12 Sep 2021 21:29:11 -0700 Subject: [PATCH] extension: Load bg.js upfront; fix bracket matching in routes.html. --- extension/background.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/extension/background.js b/extension/background.js index f624f35..2b20fc6 100644 --- a/extension/background.js +++ b/extension/background.js @@ -641,23 +641,28 @@ Routes["/runtime/reload"] = { truncate() { return {}; } }; +window.fetch(chrome.runtime.getURL('background.js')) + .then(async r => { window.__backgroundJS = await r.text(); }); + Routes["/runtime/routes.html"] = makeRouteWithContents(async () => { + if (!window.__backgroundJS) throw new UnixError(unix.EIO); + // WIP - if (!window.__backgroundJS) { - window.__backgroundJS = await window.fetch(chrome.runtime.getURL('background.js')) - .then(r => r.text()); - } const jsLines = (window.__backgroundJS).split('\n'); function findRouteLineRange(path) { for (let i = 0; i < jsLines.length; i++) { if (jsLines[i].includes(`Routes["${path}"] = `)) { if (jsLines[i].match(/;/)) { return [i, i]; } // hacky: if it's a one-liner - const [_, startBracket] = jsLines[i].match(/Routes\[[^\]]*\] = [^\(\{]*([\(\{])/); + const result = jsLines[i].match(/Routes\[[^\]]*\] = [^\(\{]*([\(\{])/); + const startBracket = result[1]; + const startBracketIndex = result.index + result[0].length; const endBracket = ({'(': ')', '{': '}'})[startBracket]; let counter = 1; - for (let j = i + 1; j < jsLines.length; j++) { - for (let k = 0; k < jsLines[j].length; k++) { + for (let j = i; j < jsLines.length; j++) { + for (let k = (j === i) ? startBracketIndex + 1 : 0; + k < jsLines[j].length; + k++) { if (jsLines[j][k] === startBracket) { counter++; } else if (jsLines[j][k] === endBracket) { counter--; }