Skip to content

Commit 6b4d217

Browse files
committed
[cdp] properly wait for chobitsu init instead of waiting arbitrary time
1 parent 5c464f0 commit 6b4d217

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

chobitsu_inject/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import chobitsu from "chobitsu";
22
window.$sendToChobitsu = (message) => chobitsu.sendRawMessage(message);
33
chobitsu.setOnMessage(window.$onChobitsuMessage);
4+
window.$onChobitsuInit();

frontend/src/CDP.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ const Scopes = {
209209
params: Protocol.Target.CreateTargetRequest
210210
): Promise<Protocol.Target.CreateTargetResponse> {
211211
console.log("creating new target");
212-
const tab = browser.newTab(new URL("http://127.0.0.1:5014/"));
212+
const tab = browser.newTab(new URL("https://google.com/"));
213213
const sessionid = server.initSession(tab);
214214

215-
await new Promise((resolve) => setTimeout(resolve, 1000));
215+
await tab.waitForChobitsuInit;
216216
server.emit<Protocol.Target.AttachedToTargetEvent>(
217217
"Target.attachedToTarget",
218218
{

frontend/src/Tab.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class Tab extends StatefulClass {
5050

5151
sendToChobitsu: ((message: string) => void) | null = null;
5252
onChobitsuMessage: ((message: string) => void) | null = null;
53+
waitForChobitsuInit: Promise<void>;
5354

5455
constructor(public url: URL = new URL("puter://newtab")) {
5556
super(createState(Object.create(Tab.prototype)));
@@ -73,11 +74,16 @@ export class Tab extends StatefulClass {
7374
this.width = 0;
7475
this.pos = 0;
7576

77+
let resolver: () => void;
78+
this.waitForChobitsuInit = new Promise((resolve) => {
79+
resolver = resolve;
80+
});
81+
7682
addHistoryListeners(frame, this);
7783
let injected = false;
7884
frame.addEventListener("contextInit", (ctx) => {
7985
injectContextMenu(ctx.client, this);
80-
injectChobitsu(ctx.client, this);
86+
injectChobitsu(ctx.client, this, resolver);
8187

8288
// make sure it's top level, ctxInit calls for all frames too
8389
if (ctx.window == frame.frame.contentWindow) {
@@ -171,7 +177,11 @@ export class Tab extends StatefulClass {
171177
}
172178
}
173179

174-
function injectChobitsu(client: ScramjetClient, tab: Tab) {
180+
function injectChobitsu(
181+
client: ScramjetClient,
182+
tab: Tab,
183+
resolver: () => void
184+
) {
175185
console.log("injecting chobitsu");
176186
// the fake origin is defined in sw.js
177187
const devtoolsUrl = "https://fake-devtools.invalid";
@@ -188,6 +198,11 @@ function injectChobitsu(client: ScramjetClient, tab: Tab) {
188198
client.global.$onChobitsuMessage = (message: string) => {
189199
if (tab.onChobitsuMessage) tab.onChobitsuMessage(message);
190200
};
201+
202+
// @ts-expect-error
203+
client.global.$onChobitsuInit = () => {
204+
resolver();
205+
};
191206
tab.sendToChobitsu = (message: string) => {
192207
console.warn(message);
193208
// @ts-expect-error

0 commit comments

Comments
 (0)