Skip to content

Commit 4a66a06

Browse files
leochen-ghuan
andauthored
添加uos请求头支持 (#129)
* 添加uos请求头支持 * Update bridge.ts * fix lint * fix lint * fix lint * fix lint * fix lint * move uos to patch method * 0.27.1 Co-authored-by: Huan LI (李卓桓) <[email protected]>
1 parent f929c2b commit 4a66a06

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wechaty-puppet-wechat",
3-
"version": "0.27.0",
3+
"version": "0.27.1",
44
"description": "Puppet WeChat for Wechaty",
55
"main": "dist/src/mod.js",
66
"typings": "dist/src/mod.d.ts",

src/bridge.ts

+27-33
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ export class Bridge extends EventEmitter {
132132
launchOptions.executablePath = this.options.endpoint
133133
}
134134

135-
const extensionOptionsArgs = [
136-
...this.getWeChromeExtensionOptionsArgs(),
137-
]
138-
139135
const options = {
140136
...launchOptions,
141137
args: [
@@ -149,7 +145,6 @@ export class Bridge extends EventEmitter {
149145
'--mute-audio',
150146
'--no-sandbox',
151147
...launchOptionsArgs,
152-
...extensionOptionsArgs,
153148
],
154149
headless,
155150
}
@@ -230,6 +225,12 @@ export class Bridge extends EventEmitter {
230225
// might be called before initPage() return.
231226
const page = this.page = await browser.newPage()
232227

228+
/**
229+
* Can we support UOS with puppeteer? #127
230+
* https://github.com/wechaty/wechaty-puppet-wechat/issues/127
231+
*/
232+
await this.uosPatch(page)
233+
233234
page.on('error', e => this.emit('error', e))
234235
page.on('dialog', this.onDialog.bind(this))
235236

@@ -268,36 +269,26 @@ export class Bridge extends EventEmitter {
268269
const UOS_PATCH_CLIENT_VERSION = '2.0.0'
269270
const UOS_PATCH_EXTSPAM = 'Gp8ICJkIEpkICggwMDAwMDAwMRAGGoAI1GiJSIpeO1RZTq9QBKsRbPJdi84ropi16EYI10WB6g74sGmRwSNXjPQnYUKYotKkvLGpshucCaeWZMOylnc6o2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wTTFj6qQvQ1a/I+MkTx5UO+yNHl718JWcR3AXGmv/aa9rD1eNP8ioTGlOZwPgmr2sor2iBpKTOrB83QgZXP+xRYkb4zVC+LoAXEoIa1+zArywlgREer7DLePukkU6wHTkuSaF+ge5Of1bXuU4i938WJHj0t3D8uQxkJvoFi/EYN/7u2P1zGRLV4dHVUsZMGCCtnO6BBigFMAA='
270271

271-
const extraHeaders = {
272+
const uosHeaders = {
272273
'client-version' : UOS_PATCH_CLIENT_VERSION,
273274
extspam : UOS_PATCH_EXTSPAM,
274-
referer : 'https://wx.qq.com/?&lang=zh_CN&target=t',
275275
}
276-
await page.setExtraHTTPHeaders(extraHeaders)
277-
}
278-
279-
private getWeChromeExtensionOptionsArgs (): string[] {
280-
/**
281-
* Issue #127: Can we support UOS with puppeteer?
282-
* https://github.com/wechaty/wechaty-puppet-wechat/issues/127
283-
* https://github.com/adamyi/wechrome - Chrome extension to unblock web wechat
284-
* https://stackoverflow.com/a/51434261/1123955
285-
*/
286-
const weChromeExt = path.join(
287-
__dirname,
288-
'..',
289-
'wechrome'
290-
)
291-
if (!fs.existsSync(weChromeExt)) {
292-
throw new Error('WeChrome extension not found.')
293-
}
294-
295-
const extensionOptionsArgs = [
296-
`--load-extension=${weChromeExt}`,
297-
`--disable-extensions-except=${weChromeExt}`,
298-
]
299-
300-
return extensionOptionsArgs
276+
// add RequestInterception
277+
await page.setRequestInterception(true)
278+
page.on('request', async req => {
279+
const url = new URL(req.url())
280+
if (url.pathname === '/cgi-bin/mmwebwx-bin/webwxnewloginpage') {
281+
const override = {
282+
headers: {
283+
...req.headers(),
284+
...uosHeaders,
285+
},
286+
}
287+
await req.continue(override)
288+
return
289+
}
290+
await req.continue()
291+
})
301292
}
302293

303294
public async readyAngular (page: Page): Promise<void> {
@@ -1050,7 +1041,10 @@ export class Bridge extends EventEmitter {
10501041
public entryUrl (cookieList?: Protocol.Network.Cookie[]): string {
10511042
log.verbose('PuppetWeChatBridge', 'cookieDomain(%s)', cookieList)
10521043

1053-
const DEFAULT_URL = 'https://wx.qq.com'
1044+
/**
1045+
* `?target=t` is from https://github.com/wechaty/wechaty-puppet-wechat/pull/129
1046+
*/
1047+
const DEFAULT_URL = 'https://wx.qq.com?target=t'
10541048

10551049
if (!cookieList || cookieList.length === 0) {
10561050
log.silly('PuppetWeChatBridge', 'cookieDomain() no cookie, return default %s', DEFAULT_URL)

0 commit comments

Comments
 (0)