Skip to content

Commit 3d02ec8

Browse files
committed
[cdp] bring puter changes to chobitsu subtree
1 parent 9ecb77c commit 3d02ec8

File tree

9 files changed

+163
-14
lines changed

9 files changed

+163
-14
lines changed

chobitsu/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"scripts": {
1515
"ci": "npm run lint && npm test && npm run build && npm run es5",
1616
"dev": "node script/build.js && concurrently \"tsc -w --inlineSourceMap\" \"webpack-dev-server --mode=development\"",
17-
"build": "rm -rf dist && tsc && webpack --mode=production && node script/build.js",
17+
"build": "rm -rf dist && webpack --mode=development && node script/build.js",
1818
"build:front_end": "cd devtools/devtools-frontend && gn gen out/Default --args=\"is_debug=false\" && autoninja -C out/Default",
1919
"lint": "eslint src/**/*.ts",
2020
"test": "karma start",

chobitsu/src/Chobitsu.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export default class Chobitsu {
6767
try {
6868
resultMsg.result = await this.callMethod(method, params)
6969
} catch (e) {
70+
console.error(e);
7071
if (e instanceof ErrorWithCode) {
7172
resultMsg.error = {
7273
message: e.message,

chobitsu/src/domains/DOM.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import xpath from 'licia/xpath'
1818
import concat from 'licia/concat'
1919
import { setGlobal } from '../lib/evaluate'
2020
import { createId } from '../lib/util'
21+
import { getObj } from '../lib/objManager'
2122
import Protocol from 'devtools-protocol'
2223
import DOM = Protocol.DOM
2324

@@ -93,6 +94,19 @@ function hookAttachShadow() {
9394

9495
hookAttachShadow()
9596

97+
98+
export function describeNode(params: DOM.DescribeNodeRequest): DOM.DescribeNodeResponse {
99+
let domnode = getObj(params.objectId!);
100+
101+
let node = nodeManager.wrap(domnode);
102+
console.error(node);
103+
console.error(getNode(node.nodeId));
104+
105+
return {
106+
node,
107+
}
108+
}
109+
96110
export function getDocument() {
97111
return {
98112
root: nodeManager.wrap(document, {
@@ -258,6 +272,35 @@ export function requestChildNodes(params: DOM.RequestChildNodesRequest) {
258272
})
259273
}
260274

275+
export function scrollIntoViewIfNeeded(
276+
params: DOM.ScrollIntoViewIfNeededRequest
277+
){
278+
let node = getObj(params.objectId!);
279+
node.scrollIntoViewIfNeeded();
280+
}
281+
export function getContentQuads(
282+
params: DOM.GetContentQuadsRequest
283+
): DOM.GetContentQuadsResponse {
284+
const node = getObj(params.objectId!);
285+
const rect = node.getBoundingClientRect()
286+
const { x, y, width, height } = rect
287+
288+
const quads = [
289+
x,
290+
y,
291+
x + width,
292+
y,
293+
x + width,
294+
y + height,
295+
x,
296+
y + height,
297+
]
298+
299+
return {
300+
quads: [quads],
301+
}
302+
}
303+
261304
export function requestNode(
262305
params: DOM.RequestNodeRequest
263306
): DOM.RequestNodeResponse {
@@ -271,7 +314,7 @@ export function requestNode(
271314
export function resolveNode(
272315
params: DOM.ResolveNodeRequest
273316
): DOM.ResolveNodeResponse {
274-
const node = getNode(params.nodeId as number)
317+
const node = getNode(params.nodeId || params.backendNodeId as number)
275318

276319
return {
277320
object: objManager.wrap(node),

chobitsu/src/domains/Input.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,51 @@ export function dispatchMouseEvent(params: Input.DispatchMouseEventRequest) {
6565
}
6666
}
6767

68+
export function dispatchKeyEvent(params: Input.DispatchKeyEventRequest) {
69+
let { type, text, key, code, modifiers } = params
70+
modifiers ??= 0
71+
72+
const event = new KeyboardEvent(type, {
73+
bubbles: true,
74+
cancelable: true,
75+
composed: true,
76+
key,
77+
code,
78+
altKey: !!(modifiers & 1),
79+
ctrlKey: !!(modifiers & 2),
80+
metaKey: !!(modifiers & 4),
81+
shiftKey: !!(modifiers & 8),
82+
})
83+
84+
if (text) {
85+
Object.defineProperty(event, 'data', {
86+
get() {
87+
return text
88+
},
89+
})
90+
}
91+
if (params.type == "keyDown") {
92+
let active = document.activeElement as HTMLInputElement | HTMLTextAreaElement;
93+
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA')) {
94+
if (key === "Enter") {
95+
if (active.form) {
96+
active.form.requestSubmit();
97+
}
98+
}
99+
100+
101+
102+
// If the active element is an input or textarea, set the value
103+
if (text) {
104+
active.value += text;
105+
active.dispatchEvent(new Event('input', { bubbles: true }));
106+
}
107+
}
108+
}
109+
110+
document.dispatchEvent(event)
111+
}
112+
68113
function triggerMouseEvent(type: string, el: Element, x: number, y: number) {
69114
el.dispatchEvent(
70115
new MouseEvent(type, {

chobitsu/src/domains/Page.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,58 @@ import * as resources from '../lib/resources'
1919
import Protocol from 'devtools-protocol'
2020
import Page = Protocol.Page
2121

22+
23+
declare var $chobitsuPageId: string;
24+
export function getFrameTree(): Page.GetFrameTreeResponse {
25+
return {
26+
frameTree: {
27+
frame: {
28+
id: $chobitsuPageId,
29+
mimeType: 'text/html',
30+
securityOrigin: getOrigin(),
31+
url: getUrl(),
32+
loaderId: $chobitsuPageId,
33+
domainAndRegistry: getOrigin(),
34+
secureContextType: 'Secure',
35+
crossOriginIsolatedContextType: 'NotIsolated',
36+
gatedAPIFeatures: [],
37+
},
38+
childFrames: [],
39+
}
40+
}
41+
}
42+
export function createIsolatedWorld(params: Page.CreateIsolatedWorldRequest): Page.CreateIsolatedWorldResponse {
43+
console.log(params);
44+
45+
// fake context
46+
connector.trigger('Runtime.executionContextCreated', {
47+
context: {
48+
id: 2,
49+
origin: getOrigin(),
50+
name: params.worldName,
51+
auxData: {
52+
isDefault: false,
53+
type: 'isolated',
54+
frameId: params.frameId,
55+
},
56+
},
57+
})
58+
return {
59+
executionContextId: 1, // This should be a unique identifier for the execution context
60+
}
61+
}
62+
63+
export function setLifecycleEventsEnabled(params: Page.SetLifecycleEventsEnabledRequest) {
64+
return {}
65+
}
66+
67+
export function addScriptToEvaluateOnNewDocument(params: Page.AddScriptToEvaluateOnNewDocumentRequest): Page.AddScriptToEvaluateOnNewDocumentResponse{
68+
console.log(params);
69+
return {
70+
identifier: '1', // This should be a unique identifier for the script
71+
}
72+
}
73+
2274
let proxy = ''
2375

2476
export function setProxy(params: any) {

chobitsu/src/domains/Runtime.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,35 @@ export async function callFunctionOn(
4141
ctx = objManager.getObj(objectId)
4242
}
4343

44+
let object = await callFn(functionDeclaration, args, ctx);
45+
let result = objManager.wrap(object, {
46+
generatePreview: true,
47+
});
48+
if (params.returnByValue) {
49+
result.value = structuredClone(object);
50+
}
51+
4452
return {
45-
result: objManager.wrap(await callFn(functionDeclaration, args, ctx)),
53+
result,
4654
}
4755
}
4856

4957
let isEnable = false
5058

59+
60+
declare var $chobitsuPageId: string;
5161
export function enable() {
5262
isEnable = true
5363
each(triggers, trigger => trigger())
5464
triggers = []
5565

5666
trigger('Runtime.executionContextCreated', {
5767
context: executionContext,
68+
auxData: {
69+
isDefault: true,
70+
type: 'default',
71+
frameId: $chobitsuPageId,
72+
},
5873
})
5974
}
6075

chobitsu/src/lib/nodeManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function wrap(node: any, { depth = 1 } = {}) {
3838
nodeType: node.nodeType,
3939
localName: node.localName || '',
4040
nodeValue: node.nodeValue || '',
41+
attributes: [],
4142
nodeId,
4243
backendNodeId: nodeId,
4344
}

chobitsu/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"strictPropertyInitialization": false,
88
"removeComments": true,
99
"noUnusedLocals": true,
10-
"target": "ES5",
10+
"target": "ES2020",
1111
"declaration": true,
1212
"esModuleInterop": true,
13-
"lib": ["DOM", "ES2015"]
13+
"lib": ["DOM", "ES2020"]
1414
},
1515
"include": ["src/**/*"]
1616
}

chobitsu/webpack.config.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const banner = pkg.name + ' v' + pkg.version + ' ' + pkg.homepage
77
module.exports = {
88
entry: './src/index.ts',
99
devtool: 'source-map',
10+
target: 'web',
1011
output: {
1112
filename: 'chobitsu.js',
1213
path: path.resolve(__dirname, 'dist'),
@@ -30,15 +31,6 @@ module.exports = {
3031
test: /\.ts$/,
3132
use: ['ts-loader'],
3233
},
33-
{
34-
test: /\.js$/,
35-
use: {
36-
loader: 'babel-loader',
37-
options: {
38-
presets: ['@babel/preset-env'],
39-
},
40-
},
41-
},
4234
{
4335
test: /\.css$/,
4436
use: [

0 commit comments

Comments
 (0)