start adding extensions/ folder. fix up js tests

some hacks to start working on clearing old scriptParsed entries
This commit is contained in:
Omar Rizwan 2020-12-22 16:36:09 -08:00
parent 9e23fc59fc
commit de9a792a21
3 changed files with 25 additions and 11 deletions

View file

@ -83,6 +83,7 @@ function sendDebuggerCommand(tabId, method, commandParams) {
const BrowserState = { lastFocusedWindowId: null, scriptsForTab: {} }; const BrowserState = { lastFocusedWindowId: null, scriptsForTab: {} };
(function() { (function() {
if (TESTING) return;
browser.windows.getLastFocused().then(window => { BrowserState.lastFocusedWindowId = window.id; }); browser.windows.getLastFocused().then(window => { BrowserState.lastFocusedWindowId = window.id; });
browser.windows.onFocusChanged.addListener(windowId => { browser.windows.onFocusChanged.addListener(windowId => {
if (windowId !== -1) BrowserState.lastFocusedWindowId = windowId; if (windowId !== -1) BrowserState.lastFocusedWindowId = windowId;
@ -90,7 +91,11 @@ const BrowserState = { lastFocusedWindowId: null, scriptsForTab: {} };
chrome.debugger.onEvent.addListener((source, method, params) => { chrome.debugger.onEvent.addListener((source, method, params) => {
console.log(source, method, params); console.log(source, method, params);
if (method === "Debugger.scriptParsed") { if (method === "Page.") {
// we're gonna assume we're always plugged into both Page and Debugger.
BrowserState.scriptsForTab[source.tabId] = [];
} else if (method === "Debugger.scriptParsed") {
BrowserState.scriptsForTab[source.tabId] = BrowserState.scriptsForTab[source.tabId] || []; BrowserState.scriptsForTab[source.tabId] = BrowserState.scriptsForTab[source.tabId] || [];
BrowserState.scriptsForTab[source.tabId].push(params); BrowserState.scriptsForTab[source.tabId].push(params);
// FIXME: clear these out when page changes in tab (how?) // FIXME: clear these out when page changes in tab (how?)
@ -251,7 +256,9 @@ router["/tabs/by-id/*/scripts"] = {
}; };
router["/tabs/by-id/*/scripts/*"] = defineFile(async path => { router["/tabs/by-id/*/scripts/*"] = defineFile(async path => {
const [tabId, suffix] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1)]; const [tabId, suffix] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1)];
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Debugger"); await TabManager.debugTab(tabId);
await TabManager.enableDomainForTab(tabId, "Page");
await TabManager.enableDomainForTab(tabId, "Debugger");
const parts = path.split("_"); const scriptId = parts[parts.length - 1]; const parts = path.split("_"); const scriptId = parts[parts.length - 1];
const {scriptSource} = await sendDebuggerCommand(tabId, "Debugger.getScriptSource", {scriptId}); const {scriptSource} = await sendDebuggerCommand(tabId, "Debugger.getScriptSource", {scriptId});
@ -313,6 +320,13 @@ router["/tabs/last-focused"] = {
} }
} }
router["/extensions"] = {
async readdir() {
const infos = await browser.management.getAll();
return { entries: [".", "..", ...infos.map(info => `${sanitize(info.name)}_${info.id}`)] };
}
};
// Ensure that there are routes for all ancestors. This algorithm is // Ensure that there are routes for all ancestors. This algorithm is
// probably not correct, but whatever. I also think it would be // probably not correct, but whatever. I also think it would be
// better to compute this stuff on the fly, so you could patch more // better to compute this stuff on the fly, so you could patch more
@ -340,9 +354,9 @@ for (let key in router) {
if (TESTING) { // I wish I could color this section with... a pink background, or something. if (TESTING) { // I wish I could color this section with... a pink background, or something.
const assert = require('assert'); const assert = require('assert');
(async () => { (async () => {
assert.deepEqual(await router['/tabs/by-id/*'].readdir(), ['url', 'title', 'text', 'control']); assert.deepEqual(await router['/tabs/by-id/*'].readdir(), { entries: ['.', '..', 'url', 'title', 'text', 'screenshot.png', 'resources', 'scripts', 'control'] });
assert.deepEqual(await router['/'].readdir(), ['tabs']); assert.deepEqual(await router['/'].readdir(), { entries: ['.', '..', 'tabs', 'extensions'] });
assert.deepEqual(await router['/tabs'].readdir(), ['by-id', 'by-title']); assert.deepEqual(await router['/tabs'].readdir(), { entries: ['.', '..', 'by-id', 'by-title'] });
assert.deepEqual(findRoute('/tabs/by-id/TABID/url'), router['/tabs/by-id/*/url']); assert.deepEqual(findRoute('/tabs/by-id/TABID/url'), router['/tabs/by-id/*/url']);
})() })()

View file

@ -6,7 +6,7 @@
"version": "1.0", "version": "1.0",
"permissions": [ "permissions": [
"tabs", "tabCapture", "debugger", "nativeMessaging", "tabs", "tabCapture", "debugger", "nativeMessaging", "management",
"unlimitedStorage", "unlimitedStorage",
"*://*/*" "*://*/*"
], ],

10
test.c
View file

@ -8,14 +8,14 @@
#include <assert.h> #include <assert.h>
int file_contents_equal(char* path, char* contents) { int file_contents_equal(char* path, char* contents) {
char command[200]; // hehe: https://twitter.com/ianh_/status/1340450349065244675
snprintf(command, sizeof(command), setenv("path", path, 1);
"[ \"%s\" == \"$(cat %s)\" ]", contents, path); setenv("contents", contents, 1);
return system(command) == 0; return system("[ \"$contents\" == \"$(cat \"$path\")\" ]") == 0;
} }
// integration tests // integration tests
int main(int argc, char** argv) { int main() {
assert(system("echo about:blank > fs/mnt/tabs/create") == 0); assert(system("echo about:blank > fs/mnt/tabs/create") == 0);
assert(file_contents_equal("fs/mnt/tabs/last-focused/url", "about:blank")); assert(file_contents_equal("fs/mnt/tabs/last-focused/url", "about:blank"));
assert(system("file fs/mnt/tabs/last-focused/screenshot.png") == 0); assert(system("file fs/mnt/tabs/last-focused/screenshot.png") == 0);