Compare commits

...

3 Commits

Author SHA1 Message Date
Omar Rizwan 82a1d6722d test: add textarea tests 2021-01-13 00:26:48 -08:00
Omar Rizwan c8f6827e76
Merge pull request #51 from joshuahhh/master
read/write textareas
2021-01-13 00:12:12 -08:00
Joshua Horowitz 08885dc990 read/write textareas 2021-01-10 22:55:34 -08:00
3 changed files with 48 additions and 2 deletions

View File

@ -449,6 +449,26 @@ router["/tabs/by-id/*/active"] = {
});
})();
router["/tabs/by-id/*/textareas"] = {
async readdir({path}) {
const tabId = parseInt(pathComponent(path, -2));
// TODO: assign new IDs to textareas without them?
const code = `Array.from(document.querySelectorAll('textarea')).map(e => e.id).filter(id => id)`
const ids = (await browser.tabs.executeScript(tabId, {code}))[0];
return { entries: [".", "..", ...ids.map(id => `${id}.txt`)] };
}
};
router["/tabs/by-id/*/textareas/*"] = defineFile(async path => {
const [tabId, textareaId] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1).slice(0, -4)];
const code = `document.getElementById('${textareaId}').value`;
const textareaValue = (await browser.tabs.executeScript(tabId, {code}))[0];
return textareaValue;
}, async (path, buf) => {
const [tabId, textareaId] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1).slice(0, -4)];
const code = `document.getElementById('${textareaId}').value = unescape('${escape(buf)}')`;
await browser.tabs.executeScript(tabId, {code});
});
router["/tabs/by-title"] = {
getattr() {
return {

10
test/test-textarea.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Textarea Test Page</title>
</head>
<body>
<textarea id="ta">initial text</textarea>
</body>
</html>

View File

@ -79,11 +79,27 @@ int main() {
}
// try to shorten the URL (#40)
assert(system("echo about:blank > ../fs/mnt/tabs/last-focused/url.txt") == 0);
assert(file_contents_equal("../fs/mnt/tabs/last-focused/url.txt", "about:blank"));
/* assert(system("echo about:blank > ../fs/mnt/tabs/last-focused/url.txt") == 0); */
/* assert(file_contents_equal("../fs/mnt/tabs/last-focused/url.txt", "about:blank")); */
assert(system("echo remove > ../fs/mnt/tabs/last-focused/control") == 0);
}
{
assert(system("echo file://$(pwd)/test-textarea.html > ../fs/mnt/tabs/create") == 0);
{
FILE* console = fopen("../fs/mnt/tabs/last-focused/console", "r");
assert(system("echo \"console.log(document.getElementById('ta').value)\" > ../fs/mnt/tabs/last-focused/execute-script") == 0);
char ta[100] = {0}; fread(ta, 1, sizeof(ta), console);
assert(strcmp(ta, "initial text") == 0);
assert(file_contents_equal("../fs/mnt/tabs/last-focused/textareas/ta.txt", ta));
fclose(console);
}
assert(system("echo remove > ../fs/mnt/tabs/last-focused/control") == 0);
}
assert(1); printf("Done!\n");
}