Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
fcb2933d
W
wechaty
项目概览
.Veneno.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fcb2933d
编写于
10月 27, 2017
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix & code clean
上级
21895c88
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
50 addition
and
90 deletion
+50
-90
src/puppet-web/bridge.spec.ts
src/puppet-web/bridge.spec.ts
+8
-20
src/puppet-web/bridge.ts
src/puppet-web/bridge.ts
+42
-70
未找到文件。
src/puppet-web/bridge.spec.ts
浏览文件 @
fcb2933d
...
@@ -70,36 +70,24 @@ test('testBlockedMessage()', async t => {
...
@@ -70,36 +70,24 @@ test('testBlockedMessage()', async t => {
const
profile
=
new
Profile
()
const
profile
=
new
Profile
()
const
bridge
=
new
Bridge
({
profile
})
const
bridge
=
new
Bridge
({
profile
})
try
{
const
msg
=
await
bridge
.
testBlockedMessage
(
'
this is not xml
'
)
await
bridge
.
testBlockedMessage
(
'
this is not xml
'
)
t
.
equal
(
msg
,
false
,
'
should return false when no block message
'
)
t
.
pass
(
'
should not throw when no block message
'
)
}
catch
(
e
)
{
t
.
fail
(
'
should throw when no block message
'
)
}
})
})
test
(
'
zh
'
,
async
t
=>
{
test
(
'
zh
'
,
async
t
=>
{
const
profile
=
new
Profile
()
const
profile
=
new
Profile
()
const
bridge
=
new
Bridge
({
profile
})
const
bridge
=
new
Bridge
({
profile
})
try
{
const
msg
=
await
bridge
.
testBlockedMessage
(
BLOCKED_XML_ZH
)
await
bridge
.
testBlockedMessage
(
BLOCKED_XML_ZH
)
t
.
equal
(
msg
,
BLOCKED_TEXT_ZH
,
'
should get zh blocked message
'
)
t
.
fail
(
'
should throw exception
'
)
}
catch
(
e
)
{
t
.
equal
(
e
.
message
,
BLOCKED_TEXT_ZH
,
'
should get zh blocked message
'
)
}
})
})
test
(
'
en
'
,
async
t
=>
{
test
(
'
en
'
,
async
t
=>
{
const
profile
=
new
Profile
()
const
profile
=
new
Profile
()
const
bridge
=
new
Bridge
({
profile
})
const
bridge
=
new
Bridge
({
profile
})
try
{
const
msg
=
await
bridge
.
testBlockedMessage
(
BLOCKED_XML_EN
)
await
bridge
.
testBlockedMessage
(
BLOCKED_XML_EN
)
t
.
equal
(
msg
,
BLOCKED_TEXT_EN
,
'
should get en blocked message
'
)
t
.
fail
(
'
should throw exception
'
)
}
catch
(
e
)
{
t
.
equal
(
e
.
message
,
BLOCKED_TEXT_EN
,
'
should get en blocked message
'
)
}
})
})
})
})
...
@@ -117,7 +105,7 @@ test('clickSwitchAccount()', async t => {
...
@@ -117,7 +105,7 @@ test('clickSwitchAccount()', async t => {
test
(
'
switch account needed
'
,
async
t
=>
{
test
(
'
switch account needed
'
,
async
t
=>
{
const
browser
=
await
launch
()
const
browser
=
await
launch
()
const
page
=
await
browser
.
newPage
()
const
page
=
await
browser
.
newPage
()
await
page
.
setContent
(
SWITCH_ACCOUNT_HTML
)
await
page
.
setContent
(
SWITCH_ACCOUNT_HTML
)
const
clicked
=
await
bridge
.
clickSwitchAccount
(
page
)
const
clicked
=
await
bridge
.
clickSwitchAccount
(
page
)
...
@@ -130,7 +118,7 @@ test('clickSwitchAccount()', async t => {
...
@@ -130,7 +118,7 @@ test('clickSwitchAccount()', async t => {
test
(
'
switch account not needed
'
,
async
t
=>
{
test
(
'
switch account not needed
'
,
async
t
=>
{
const
browser
=
await
launch
()
const
browser
=
await
launch
()
const
page
=
await
browser
.
newPage
()
const
page
=
await
browser
.
newPage
()
await
page
.
setContent
(
'
<h1>ok</h1>
'
)
await
page
.
setContent
(
'
<h1>ok</h1>
'
)
const
clicked
=
await
bridge
.
clickSwitchAccount
(
page
)
const
clicked
=
await
bridge
.
clickSwitchAccount
(
page
)
...
...
src/puppet-web/bridge.ts
浏览文件 @
fcb2933d
...
@@ -109,10 +109,9 @@ export class Bridge extends EventEmitter {
...
@@ -109,10 +109,9 @@ export class Bridge extends EventEmitter {
public
async
initPage
(
browser
:
Browser
):
Promise
<
Page
>
{
public
async
initPage
(
browser
:
Browser
):
Promise
<
Page
>
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage()
'
)
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage()
'
)
const
page
=
await
browser
.
newPage
()
// set this in time because the following callbacks
// set this in time because the following callbacks
// might be called before initPage() return.
// might be called before initPage() return.
this
.
page
=
page
const
page
=
this
.
page
=
await
browser
.
newPage
()
const
onDialog
=
async
(
dialog
:
Dialog
)
=>
{
const
onDialog
=
async
(
dialog
:
Dialog
)
=>
{
log
.
warn
(
'
PuppetWebBridge
'
,
'
init() page.on(dialog) type:%s message:%s
'
,
log
.
warn
(
'
PuppetWebBridge
'
,
'
init() page.on(dialog) type:%s message:%s
'
,
...
@@ -134,76 +133,29 @@ export class Bridge extends EventEmitter {
...
@@ -134,76 +133,29 @@ export class Bridge extends EventEmitter {
)
=>
{
)
=>
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() on(load) %s
'
,
page
.
url
())
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() on(load) %s
'
,
page
.
url
())
const
stateOffError
=
new
Error
(
'
onLoad() OFF state detected
'
)
if
(
this
.
state
.
off
())
{
if
(
this
.
state
.
off
())
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() OFF state detected. NOP
'
)
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() OFF state detected. NOP
'
)
return
reject
(
stateOffError
)
return
reject
(
new
Error
(
'
onLoad() OFF state detected
'
)
)
}
}
try
{
try
{
await
page
.
exposeFunction
(
'
emit
'
,
this
.
emit
.
bind
(
this
))
const
emitExist
=
await
page
.
evaluate
(()
=>
{
}
catch
(
e
)
{
return
typeof
window
[
'
emit
'
]
===
'
function
'
if
(
this
.
state
.
off
())
{
})
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() OFF state detected. NOP
'
)
if
(
!
emitExist
)
{
return
reject
(
stateOffError
)
await
page
.
exposeFunction
(
'
emit
'
,
this
.
emit
.
bind
(
this
)
)
}
}
// exposed function will stay in the browser after reload the page
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() page.exposeFunction(emit) already exist
'
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() page.exposeFunction(emit) exception: %s
'
,
e
)
}
try
{
await
this
.
readyAngular
(
page
)
await
this
.
readyAngular
(
page
)
}
catch
(
e
)
{
const
text
=
await
this
.
evaluate
(()
=>
{
return
document
.
body
.
innerHTML
})
as
any
as
string
// BUG of Puppet Type Definition
try
{
// Test if Wechat account is blocked
// will throw exception if blocked
await
this
.
testBlockedMessage
(
text
)
}
catch
(
e
)
{
// Wechat Account Blocked
log
.
error
(
'
PuppetWeb
'
,
'
initBridge() Wechat Account Blocked for using Web: %s
'
,
e
.
message
)
this
.
emit
(
'
error
'
,
e
)
return
reject
(
e
)
}
}
try
{
await
this
.
inject
(
page
)
await
this
.
inject
(
page
)
await
this
.
clickSwitchAccount
(
page
)
const
clicked
=
await
this
.
clickSwitchAccount
(
page
)
return
resolve
()
if
(
clicked
)
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() clickSwitchAccount() clicked
'
)
}
else
{
log
.
silly
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() clickSwitchAccount() NOP
'
)
}
}
catch
(
e
)
{
}
catch
(
e
)
{
if
(
this
.
state
.
off
())
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
initPage() onLoad() OFF state detected. NOP
'
)
return
reject
(
stateOffError
)
}
log
.
error
(
'
PuppetWebBridge
'
,
'
init() initPage() onLoad() exception: %s
'
,
e
)
log
.
error
(
'
PuppetWebBridge
'
,
'
init() initPage() onLoad() exception: %s
'
,
e
)
this
.
emit
(
'
error
'
,
e
)
this
.
emit
(
'
error
'
,
e
)
return
reject
(
e
)
return
reject
(
e
)
}
}
return
resolve
()
}
}
page
.
on
(
'
dialog
'
,
onDialog
)
page
.
on
(
'
error
'
,
e
=>
this
.
emit
(
'
error
'
,
e
))
const
loaded
=
new
Promise
((
resolve
,
reject
)
=>
page
.
on
(
'
load
'
,
()
=>
onLoad
(
resolve
,
reject
)))
///////////////////
const
cookieList
=
this
.
options
.
profile
.
get
(
'
cookies
'
)
as
Cookie
[]
const
cookieList
=
this
.
options
.
profile
.
get
(
'
cookies
'
)
as
Cookie
[]
const
url
=
this
.
entryUrl
(
cookieList
)
const
url
=
this
.
entryUrl
(
cookieList
)
...
@@ -214,10 +166,16 @@ export class Bridge extends EventEmitter {
...
@@ -214,10 +166,16 @@ export class Bridge extends EventEmitter {
if
(
cookieList
&&
cookieList
.
length
)
{
if
(
cookieList
&&
cookieList
.
length
)
{
await
page
.
setCookie
(...
cookieList
)
await
page
.
setCookie
(...
cookieList
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
initPage() page.setCookie() %s cookies set back
'
,
cookieList
.
length
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
initPage() page.setCookie() %s cookies set back
'
,
cookieList
.
length
)
await
page
.
reload
()
// reload page to make effect of the new cookie.
}
}
await
loaded
// wait the page on load finish
page
.
on
(
'
dialog
'
,
onDialog
)
page
.
on
(
'
error
'
,
e
=>
this
.
emit
(
'
error
'
,
e
))
const
loaded
=
new
Promise
((
resolve
,
reject
)
=>
{
page
.
on
(
'
load
'
,
()
=>
onLoad
(
resolve
,
reject
))
})
await
page
.
reload
()
// reload page to make effect of the new cookie.
await
loaded
// wait the page on load finish
return
page
return
page
}
}
...
@@ -226,8 +184,22 @@ export class Bridge extends EventEmitter {
...
@@ -226,8 +184,22 @@ export class Bridge extends EventEmitter {
log
.
verbose
(
'
PuppetWebBridge
'
,
'
readyAngular()
'
)
log
.
verbose
(
'
PuppetWebBridge
'
,
'
readyAngular()
'
)
const
TIMEOUT
=
10
*
1000
const
TIMEOUT
=
10
*
1000
await
new
Promise
<
void
>
(
async
(
resolve
,
reject
)
=>
{
await
new
Promise
<
void
>
(
async
(
resolve
,
reject
)
=>
{
const
timer
=
setTimeout
(()
=>
{
const
timer
=
setTimeout
(
async
()
=>
{
const
text
=
await
this
.
evaluate
(()
=>
{
return
document
.
body
.
innerHTML
})
as
any
as
string
// BUG of Puppet Type Definition
const
blockedMessage
=
await
this
.
testBlockedMessage
(
text
)
if
(
blockedMessage
)
{
// Wechat Account Blocked
log
.
error
(
'
PuppetWeb
'
,
'
initBridge() Wechat Account Blocked for using Web: %s
'
,
blockedMessage
)
const
err
=
new
Error
(
blockedMessage
)
this
.
emit
(
'
error
'
,
err
)
return
reject
(
err
)
}
reject
(
`readyAngular() timeout after
${
TIMEOUT
}
`
)
reject
(
`readyAngular() timeout after
${
TIMEOUT
}
`
)
},
TIMEOUT
)
},
TIMEOUT
)
await
page
.
waitForFunction
(
`typeof window.angular !== 'undefined'`
)
await
page
.
waitForFunction
(
`typeof window.angular !== 'undefined'`
)
...
@@ -688,9 +660,9 @@ export class Bridge extends EventEmitter {
...
@@ -688,9 +660,9 @@ export class Bridge extends EventEmitter {
/**
/**
* Throw if there's a blocked message
* Throw if there's a blocked message
*/
*/
public
async
testBlockedMessage
(
text
:
string
):
Promise
<
void
>
{
public
async
testBlockedMessage
(
text
:
string
):
Promise
<
string
|
false
>
{
const
textSnip
=
text
.
substr
(
0
,
50
).
replace
(
/
\n
/
,
''
)
const
textSnip
=
text
.
substr
(
0
,
50
).
replace
(
/
\n
/
,
''
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
testBlockedMessage(%s)
'
,
log
.
verbose
(
'
PuppetWebBridge
'
,
'
testBlockedMessage(%s)
'
,
textSnip
)
textSnip
)
interface
BlockedMessage
{
interface
BlockedMessage
{
...
@@ -700,33 +672,31 @@ export class Bridge extends EventEmitter {
...
@@ -700,33 +672,31 @@ export class Bridge extends EventEmitter {
}
}
}
}
return
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
return
new
Promise
<
string
|
false
>
((
resolve
,
reject
)
=>
{
parseString
(
text
,
{
explicitArray
:
false
},
(
err
,
obj
:
BlockedMessage
)
=>
{
parseString
(
text
,
{
explicitArray
:
false
},
(
err
,
obj
:
BlockedMessage
)
=>
{
if
(
err
)
{
// HTML can not be parsed to JSON
if
(
err
)
{
// HTML can not be parsed to JSON
return
resolve
()
return
resolve
(
false
)
}
}
if
(
!
obj
)
{
if
(
!
obj
)
{
// FIXME: when will this happen?
// FIXME: when will this happen?
log
.
warn
(
'
PuppetWebBridge
'
,
'
testBlockedMessage() parseString(%s) return empty obj
'
,
textSnip
)
log
.
warn
(
'
PuppetWebBridge
'
,
'
testBlockedMessage() parseString(%s) return empty obj
'
,
textSnip
)
return
resolve
()
return
resolve
(
false
)
}
}
if
(
!
obj
.
error
)
{
if
(
!
obj
.
error
)
{
return
resolve
()
return
resolve
(
false
)
}
}
const
ret
=
+
obj
.
error
.
ret
const
ret
=
+
obj
.
error
.
ret
const
message
=
obj
.
error
.
message
const
message
=
obj
.
error
.
message
const
e
=
new
Error
(
message
)
if
(
ret
===
1203
)
{
if
(
ret
===
1203
)
{
// <error>
// <error>
// <ret>1203</ret>
// <ret>1203</ret>
// <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
// <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
// </error>
// </error>
return
re
ject
(
e
)
return
re
solve
(
messag
e
)
}
}
log
.
warn
(
'
PuppetWebBridge
'
,
'
testBlockedMessage() code: %s type: %s
'
,
ret
,
typeof
ret
)
log
.
warn
(
'
PuppetWebBridge
'
,
'
testBlockedMessage() code: %s type: %s
'
,
ret
,
typeof
ret
)
return
re
ject
(
e
)
// other error message
return
re
solve
(
messag
e
)
// other error message
})
})
})
})
}
}
...
@@ -767,10 +737,12 @@ export class Bridge extends EventEmitter {
...
@@ -767,10 +737,12 @@ export class Bridge extends EventEmitter {
await
button
.
click
()
await
button
.
click
()
log
.
silly
(
'
PuppetWebBridge
'
,
'
clickSwitchAccount() clicked!
'
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
clickSwitchAccount() clicked!
'
)
return
true
return
true
}
else
{
}
else
{
// log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found')
// log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found')
return
false
return
false
}
}
}
catch
(
e
)
{
}
catch
(
e
)
{
log
.
silly
(
'
PuppetWebBridge
'
,
'
clickSwitchAccount() exception: %s
'
,
e
)
log
.
silly
(
'
PuppetWebBridge
'
,
'
clickSwitchAccount() exception: %s
'
,
e
)
throw
e
throw
e
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录