Skip to content

Commit a252f95

Browse files
committed
[frontend] add chobitsu injector
1 parent 7fa00c1 commit a252f95

File tree

9 files changed

+91
-2217
lines changed

9 files changed

+91
-2217
lines changed

chobitsu_inject/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import chobitsu from "chobitsu";

chobitsu_inject/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "chobitsu_inject",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"type": "module",
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"packageManager": "[email protected]",
14+
"dependencies": {
15+
"chobitsu": "workspace:*"
16+
},
17+
"devDependencies": {
18+
"@rollup/plugin-node-resolve": "^16.0.1",
19+
"rollup": "^4.44.2"
20+
}
21+
}

chobitsu_inject/rollup.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { nodeResolve } from "@rollup/plugin-node-resolve";
2+
3+
export default {
4+
input: "index.js",
5+
output: {
6+
file: "output/chobitsu_inject.js",
7+
format: "umd",
8+
},
9+
plugins: [
10+
nodeResolve({
11+
preferBuiltins: true,
12+
browser: true,
13+
}),
14+
],
15+
};

frontend/public/sw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ scramjet.addEventListener("request", (e) => {
3131
if (e.url.href.startsWith("https://fake-devtools.invalid")) {
3232
// route the fake origin devtools requests to the local static files
3333
e.response = (async () => {
34-
let response = await fetch("/chi/" + e.url.pathname);
34+
let response = await fetch("/" + e.url.pathname);
3535

3636
let rawHeaders = {};
3737
for (const [key, value] of response.headers.entries()) {

frontend/src/CDP.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CDPServer {
2727
const resultMsg: any = {
2828
id: msg.id,
2929
};
30+
console.log(msg);
3031

3132
try {
3233
resultMsg.result = await this.callMethod(msg.method, msg.params);
@@ -65,6 +66,9 @@ class CDPServer {
6566
}
6667
}
6768

69+
if (params.sessionId) {
70+
}
71+
6872
throw Error(`${method} unimplemented`);
6973
}
7074
}
@@ -103,7 +107,7 @@ const Scopes = {
103107
server.emit<Protocol.Target.AttachedToTargetEvent>(
104108
"Target.attachedToTarget",
105109
{
106-
sessionId: "0",
110+
sessionId: String(tab.id),
107111
waitingForDebugger: false,
108112
targetInfo: {
109113
browserContextId: "0",

frontend/src/Tab.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export class Tab extends StatefulClass {
4848
devtoolsOpen: boolean = false;
4949
devtoolsWidth = 200;
5050

51+
sendToChobitsu: ((message: string) => void) | null = null;
52+
onChobitsuMessage: ((message: string) => void) | null = null;
53+
5154
constructor(public url: URL = new URL("puter://newtab")) {
5255
super(createState(Object.create(Tab.prototype)));
5356

@@ -74,6 +77,7 @@ export class Tab extends StatefulClass {
7477
let injected = false;
7578
frame.addEventListener("contextInit", (ctx) => {
7679
injectContextMenu(ctx.client, this);
80+
injectChobitsu(ctx.client, this);
7781

7882
// make sure it's top level, ctxInit calls for all frames too
7983
if (ctx.window == frame.frame.contentWindow) {
@@ -167,6 +171,23 @@ export class Tab extends StatefulClass {
167171
}
168172
}
169173

174+
function injectChobitsu(client: ScramjetClient, tab: Tab) {
175+
// the fake origin is defined in sw.js
176+
const devtoolsUrl = "https://fake-devtools.invalid";
177+
// make sure to create the element through the proxied document
178+
let devtoolsScript = client.global.document.createElement("script");
179+
devtoolsScript.setAttribute("src", devtoolsUrl + "/chobitsu_inject.js");
180+
181+
// @ts-expect-error
182+
client.global.$onChobitsuMessage = (message: string) => {
183+
tab.onChobitsuMessage(message);
184+
};
185+
tab.sendToChobitsu = (message: string) => {
186+
// @ts-expect-error
187+
client.global.$sendToChobitsu(message);
188+
};
189+
}
190+
170191
function injectDevtools(client: ScramjetClient, tab: Tab) {
171192
// right here there's three contexts, the main window, the page frame, and the empty devtools frame
172193
// chobitsu is injected into the page frame (loaded from https://fake-devtools.invalid/target.js), and then will try and inject

frontend/vite.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export default defineConfig({
1212
src: scramjetPath + "/*",
1313
dest: "scram/",
1414
},
15+
{
16+
src: "../chobitsu_inject/output/chobitsu_inject.js",
17+
dest: ".",
18+
},
1519
],
1620
}),
1721
],

0 commit comments

Comments
 (0)