Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46011646
wechaty
提交
21871140
W
wechaty
项目概览
weixin_46011646
/
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,发现更多精彩内容 >>
提交
21871140
编写于
8月 24, 2017
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(open) re-init web driver with right order
上级
bf77d5a4
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
54 addition
and
72 deletion
+54
-72
src/puppet-web/browser.ts
src/puppet-web/browser.ts
+54
-72
未找到文件。
src/puppet-web/browser.ts
浏览文件 @
21871140
...
...
@@ -17,6 +17,8 @@
*
*/
import
{
EventEmitter
}
from
'
events
'
import
{
promisify
}
from
'
util
'
const
psTree
=
require
(
'
ps-tree
'
)
const
retryPromise
=
require
(
'
retry-promise
'
).
default
// https://github.com/olalonde/retry-promise
import
{
StateSwitch
}
from
'
state-switch
'
...
...
@@ -131,7 +133,7 @@ export class Browser extends EventEmitter {
}
public
async
open
(
url
?:
string
):
Promise
<
void
>
{
log
.
verbose
(
'
PuppetWebBrowser
'
,
`open(
${
url
}
)`
)
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
open(%s)
'
,
url
)
if
(
!
url
)
{
const
hostname
=
this
.
cookie
.
hostname
()
...
...
@@ -150,48 +152,43 @@ export class Browser extends EventEmitter {
while
(
ttl
--
)
{
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
open() begin for ttl:%d
'
,
ttl
)
try
{
await
new
Promise
(
async
(
resolve
,
reject
)
=>
{
const
timer
=
setTimeout
(
async
_
=>
{
try
{
await
this
.
driver
.
close
()
await
this
.
driver
.
quit
()
await
this
.
driver
.
init
()
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
open() driver.{close,quit,init}() done
'
)
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWebBrowser
'
,
'
open() timeout, close driver exception: %s
'
,
e
.
message
,
)
}
await
new
Promise
<
void
>
(
async
(
resolve
,
reject
)
=>
{
const
timer
=
setTimeout
(
_
=>
{
const
e
=
new
Error
(
'
timeout after
'
+
Math
.
round
(
TIMEOUT
/
1000
)
+
'
seconds
'
+
'
at ttl:
'
+
ttl
,
)
reject
(
e
)
re
turn
re
ject
(
e
)
},
TIMEOUT
)
try
{
await
this
.
driver
.
get
(
openUrl
)
resolve
()
// open successful!
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
open(%s) end at ttl:%d
'
,
openUrl
,
ttl
)
return
resolve
()
}
catch
(
e
)
{
reject
(
e
)
return
reject
(
e
)
}
finally
{
clearTimeout
(
timer
)
}
})
// open successful!
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
open() end at ttl:%d
'
,
ttl
)
return
}
catch
(
e
)
{
log
.
error
(
'
PuppetWebBrowser
'
,
'
open() exception: %s
'
,
e
.
message
)
await
this
.
driver
.
close
()
await
this
.
driver
.
quit
()
await
this
.
driver
.
init
()
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
open() driver.{close,quit,init}() done
'
)
}
}
await
this
.
driver
.
close
()
await
this
.
driver
.
quit
()
throw
new
Error
(
'
open fail because ttl expired
'
)
}
public
async
refresh
():
Promise
<
void
>
{
...
...
@@ -315,64 +312,49 @@ export class Browser extends EventEmitter {
})
}
public
getBrowserPidList
():
Promise
<
number
[]
>
{
public
async
getBrowserPidList
():
Promise
<
number
[]
>
{
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
getBrowserPidList()
'
)
const
head
=
this
.
setting
.
head
return
new
Promise
((
resolve
,
reject
)
=>
{
/**
* Reject
*/
const
timer
=
setTimeout
(()
=>
{
const
e
=
new
Error
(
'
clean() psTree() timeout.
'
)
log
.
error
(
'
PuppetWebBrowser
'
,
e
.
message
)
reject
(
e
)
},
10
*
1000
)
psTree
(
process
.
pid
,
(
err
,
children
)
=>
{
if
(
err
)
{
log
.
error
(
'
PuppetWebBrowser
'
,
'
getBrowserPidList() %s
'
,
err
.
message
||
err
)
reject
(
err
)
return
}
let
browserRe
switch
(
head
)
{
case
'
phantomjs
'
:
browserRe
=
'
phantomjs
'
break
case
'
chrome
'
:
case
'
chrome-headless
'
:
browserRe
=
'
chrome(?!driver)|chromium
'
break
default
:
const
e
=
new
Error
(
'
unsupported head:
'
+
head
)
log
.
warn
(
'
PuppetWebBrowser
'
,
'
getBrowserPids() for %s
'
,
e
.
message
)
throw
e
try
{
const
children
=
await
promisify
(
psTree
)(
process
.
pid
)
let
regexText
:
string
switch
(
head
)
{
case
'
phantomjs
'
:
regexText
=
'
phantomjs
'
break
case
'
chrome
'
:
case
'
chrome-headless
'
:
regexText
=
'
chrome(?!driver)|chromium
'
break
default
:
const
e
=
new
Error
(
'
unsupported head:
'
+
head
)
log
.
warn
(
'
PuppetWebBrowser
'
,
'
getBrowserPids() for %s
'
,
e
.
message
)
throw
e
}
const
matchRegex
=
new
RegExp
(
regexText
,
'
i
'
)
const
pids
:
number
[]
=
children
.
filter
(
child
=>
{
// https://github.com/indexzero/ps-tree/issues/18
if
(
matchRegex
.
test
(
''
+
child
.
COMMAND
+
child
.
COMM
))
{
log
.
silly
(
'
PuppetWebBrowser
'
,
'
getBrowserPids() child: %s
'
,
JSON
.
stringify
(
child
))
return
true
}
return
false
const
matchRegex
=
new
RegExp
(
browserRe
,
'
i
'
)
const
pids
:
number
[]
=
children
.
filter
(
child
=>
{
// https://github.com/indexzero/ps-tree/issues/18
if
(
matchRegex
.
test
(
''
+
child
.
COMMAND
+
child
.
COMM
))
{
log
.
silly
(
'
PuppetWebBrowser
'
,
'
getBrowserPids() child: %s
'
,
JSON
.
stringify
(
child
))
return
true
}
return
false
}).
map
(
child
=>
child
.
PID
)
}).
map
(
child
=>
child
.
PID
)
return
pids
/**
* Resolve
*/
clearTimeout
(
timer
)
resolve
(
pids
)
return
})
})
}
catch
(
e
)
{
log
.
error
(
'
PuppetWebBrowser
'
,
'
getBrowserPidList() exception: %s
'
,
e
.
message
||
e
)
throw
e
}
}
public
async
execute
(
script
,
...
args
):
Promise
<
any
>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录