Skip to content

Commit abbeef4

Browse files
committed
release: v1.3.1
1 parent c795d27 commit abbeef4

File tree

6 files changed

+260
-279
lines changed

6 files changed

+260
-279
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.3.1 (21 Nov 2022)
2+
3+
* fix: css variable and selector with comma #7
4+
15
## 1.3.0 (15 Aug 2022)
26

37
* feat: proxy support

package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
{
22
"name": "chobitsu",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Chrome devtools protocol JavaScript implementation",
55
"main": "dist/chobitsu.js",
6+
"exports": {
7+
".": "./dist/chobitsu.js",
8+
"./Chobitsu": "./dist/cjs/Chobitsu.js",
9+
"./domains/*": "./dist/cjs/domains/*"
10+
},
611
"files": [
712
"dist/*"
813
],
914
"scripts": {
1015
"ci": "npm run lint && npm test && npm run build && npm run es5",
11-
"dev": "webpack --mode=development -w",
12-
"build": "webpack --mode=production && node script/build.js",
16+
"dev": "node script/build.js && concurrently 'tsc -w --inlineSourceMap' 'webpack --mode=development -w'",
17+
"build": "rm -rf dist && tsc && webpack --mode=production && node script/build.js",
1318
"lint": "eslint src/**/*.ts",
1419
"test": "karma start",
15-
"es5": "es-check es5 dist/chobitsu.js",
20+
"es5": "es-check es5 dist/**/*.js",
1621
"format": "lsla prettier 'src/**/*.ts' '*.{js,json}' 'test/*.js' 'script/*.js' --write"
1722
},
1823
"repository": {
@@ -28,27 +33,30 @@
2833
"url": "https://github.com/liriliri/chobitsu/issues"
2934
},
3035
"homepage": "https://github.com/liriliri/chobitsu#readme",
36+
"dependencies": {
37+
"axios": "^0.27.2",
38+
"html2canvas": "^1.4.1",
39+
"licia": "^1.37.0",
40+
"luna-dom-highlighter": "^1.0.0"
41+
},
3142
"devDependencies": {
3243
"@babel/core": "^7.17.10",
3344
"@babel/preset-env": "^7.17.10",
3445
"@jsdevtools/coverage-istanbul-loader": "^3.0.5",
3546
"@types/node": "^14.0.13",
3647
"@typescript-eslint/eslint-plugin": "^5.33.0",
3748
"@typescript-eslint/parser": "^5.33.0",
38-
"axios": "^0.27.2",
3949
"babel-loader": "^8.2.5",
50+
"concurrently": "^7.6.0",
4051
"es-check": "^6.2.1",
4152
"eslint": "^8.21.0",
4253
"eslint-config-prettier": "^8.5.0",
43-
"html2canvas": "^1.4.1",
4454
"karma": "^6.3.19",
4555
"karma-chai-plugins": "^0.9.0",
4656
"karma-chrome-launcher": "^3.1.1",
4757
"karma-coverage-istanbul-reporter": "^3.0.3",
4858
"karma-mocha": "^2.0.1",
4959
"karma-webpack": "^4.0.2",
50-
"licia": "^1.37.0",
51-
"luna-dom-highlighter": "^1.0.0",
5260
"mocha": "^8.0.1",
5361
"raw-loader": "^4.0.2",
5462
"ts-loader": "^7.0.5",

src/Chobitsu.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import connector from './lib/connector'
2+
import noop from 'licia/noop'
3+
import uuid from 'licia/uuid'
4+
import each from 'licia/each'
5+
import Emitter from 'licia/Emitter'
6+
import { ErrorWithCode } from './lib/util'
7+
import types from 'licia/types'
8+
9+
type OnMessage = (message: string) => void
10+
type DomainMethod = (...args: any[]) => any
11+
12+
export default class Chobitsu {
13+
private onMessage: OnMessage
14+
private resolves: Map<string, (value?: any) => void> = new Map()
15+
private domains: Map<string, { [index: string]: DomainMethod }> = new Map()
16+
constructor() {
17+
this.onMessage = noop
18+
connector.on('message', (message: any) => {
19+
const parsedMessage = JSON.parse(message)
20+
21+
const resolve = this.resolves.get(parsedMessage.id)
22+
if (resolve) {
23+
resolve(parsedMessage.result)
24+
}
25+
26+
if (!parsedMessage.id) {
27+
const [name, method] = parsedMessage.method.split('.')
28+
const domain = this.domains.get(name)
29+
if (domain) {
30+
domain.emit(method, parsedMessage.params)
31+
}
32+
}
33+
34+
this.onMessage(message)
35+
})
36+
}
37+
domain(name: string) {
38+
return this.domains.get(name)
39+
}
40+
setOnMessage(onMessage: OnMessage) {
41+
this.onMessage = onMessage
42+
}
43+
sendMessage(method: string, params: any = {}) {
44+
const id = uuid()
45+
46+
this.sendRawMessage(
47+
JSON.stringify({
48+
id,
49+
method,
50+
params,
51+
})
52+
)
53+
54+
return new Promise(resolve => {
55+
this.resolves.set(id, resolve)
56+
})
57+
}
58+
async sendRawMessage(message: string) {
59+
const parsedMessage = JSON.parse(message)
60+
61+
const { method, params, id } = parsedMessage
62+
63+
const resultMsg: any = {
64+
id,
65+
}
66+
67+
try {
68+
resultMsg.result = await this.callMethod(method, params)
69+
} catch (e) {
70+
if (e instanceof ErrorWithCode) {
71+
resultMsg.error = {
72+
message: e.message,
73+
code: e.code,
74+
}
75+
} else if (e instanceof Error) {
76+
resultMsg.error = {
77+
message: e.message,
78+
}
79+
}
80+
}
81+
82+
connector.emit('message', JSON.stringify(resultMsg))
83+
}
84+
register(name: string, methods: types.PlainObj<types.AnyFn>) {
85+
const domains = this.domains
86+
87+
let domain = domains.get(name)!
88+
if (!domain) {
89+
domain = {}
90+
Emitter.mixin(domain)
91+
}
92+
each(methods, (fn: any, method: string) => {
93+
domain[method] = fn
94+
})
95+
domains.set(name, domain)
96+
}
97+
private async callMethod(method: string, params: any) {
98+
const [ domainName, methodName ] = method.split('.')
99+
const domain = this.domain(domainName)
100+
if (domain) {
101+
if (domain[methodName]) {
102+
return domain[methodName](params) || {}
103+
}
104+
}
105+
106+
throw Error(`${method} unimplemented`)
107+
}
108+
}

src/domains/methods.ts

Lines changed: 0 additions & 163 deletions
This file was deleted.

0 commit comments

Comments
 (0)