aboutsummaryrefslogtreecommitdiffstats
path: root/extension
diff options
context:
space:
mode:
authorOmar Rizwan <omar.rizwan@gmail.com>2018-11-14 23:52:26 -0800
committerOmar Rizwan <omar.rizwan@gmail.com>2018-11-14 23:52:26 -0800
commit66041e333c10387a18990f9af20d9c4daec5320e (patch)
tree138842d073aeeab11e0b3ab084ae4ed8f09806fd /extension
parent828a1c33d0a6cb24b01e4210e797fe9449b34c8e (diff)
Route tree!
Diffstat (limited to 'extension')
-rw-r--r--extension/background.js80
-rw-r--r--extension/manifest.json6
2 files changed, 64 insertions, 22 deletions
diff --git a/extension/background.js b/extension/background.js
index dd409dc..b08311c 100644
--- a/extension/background.js
+++ b/extension/background.js
@@ -24,33 +24,75 @@ const unix = {
S_IFSOCK: 0140000, // socket
}
-function readdir(path) {
- if (path === "/") {
- return ["tabs"];
+function queryTabs() {
+ return new Promise((resolve, reject) => chrome.tabs.query({}, resolve));
+}
+
+const router = {
+ "tabs": {
+ "by-id": {
+ async readdir() {
+ const tabs = await queryTabs();
+ return tabs.map(tab => String(tab.id));
+ },
+
+ "*": {
+ async getattr() {
+ return {
+ st_mode: unix.S_IFREG | 0444,
+ st_nlink: 1,
+ st_size: 10 // FIXME
+ };
+ }
+ }
+ }
+ }
+};
- } else if (path === "/tabs") {
- return ["hello.txt"];
+function findRoute(path) {
+ let route = router;
+ for (let segment of path.split("/")) {
+ if (segment === "") continue;
+ route = route[segment] || route["*"];
}
+ return route;
}
-function getattr(path) {
- const response = {};
- if (path === "/" || path === "/tabs") {
- response.st_mode = unix.S_IFDIR | 0755;
- response.st_nlink = 3;
+async function readdir(path) {
+ let route = findRoute(path);
- } else if (path === "/tabs/hello.txt") {
- response.st_mode = unix.S_IFREG | 0444;
- response.st_nlink = 1;
- response.st_size = 10; // FIXME
+ if (route.readdir) {
+ return route.readdir();
+ }
+ return Object.keys(route);
+}
+
+async function getattr(path) {
+ let route = findRoute(path);
+ if (route.getattr) {
+ return route.getattr();
} else {
- response.error = unix.ENOENT;
+ return {
+ st_mode: unix.S_IFDIR | 0755,
+ st_nlink: 3
+ };
}
- return response;
+ /*
+ * const response = {};
+ * if (path === "/" || path === "/tabs" || path === "/tabs/by-title" || path === "/tabs/by-id") {
+ * response.st_mode = unix.S_IFDIR | 0755;
+ * response.st_nlink = 3;
+
+ * } else if (path === "/tabs/hello.txt") {
+ *
+ * } else {
+ * response.error = unix.ENOENT;
+ * }
+ * return response;*/
}
-ws.onmessage = function(event) {
+ws.onmessage = async function(event) {
const req = JSON.parse(event.data);
console.log('req', Object.entries(ops).find(([op, opcode]) => opcode === req.op)[0], req);
@@ -58,7 +100,7 @@ ws.onmessage = function(event) {
if (req.op === ops.READDIR) {
response = {
op: ops.READDIR,
- entries: [".", "..", ...readdir(req.path)]
+ entries: [".", "..", ...(await readdir(req.path))]
};
} else if (req.op === ops.GETATTR) {
@@ -67,7 +109,7 @@ ws.onmessage = function(event) {
st_mode: 0,
st_nlink: 0,
st_size: 0,
- ...getattr(req.path)
+ ...(await getattr(req.path))
};
}
diff --git a/extension/manifest.json b/extension/manifest.json
index 374f21d..b523910 100644
--- a/extension/manifest.json
+++ b/extension/manifest.json
@@ -1,11 +1,11 @@
{
"manifest_version": 2,
- "name": "ChromeFS Extension",
- "description": "Connects to ChromeFS filesystem",
+ "name": "TabFS Extension",
+ "description": "Connects to TabFS filesystem",
"version": "1.0",
- "permissions": ["tabs"],
+ "permissions": ["tabs", "debugger"],
"background": {
"scripts": ["background.js"],