-
Notifications
You must be signed in to change notification settings - Fork 3
/
hooks.js.sample
59 lines (47 loc) · 1.54 KB
/
hooks.js.sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 如果hooks.js文件放在项目目录下,那么这里仅需要导出这个文件路径字符串即可
// 当HOOKS_FILE这个导出值存在时,本文件内定义的其他编程接口将无效。以HOOKS_FILE指定的hooks.js内容为准
exports.HOOKS_FILE='path/to/hooks.js'
// 若有本地代理,建议手动设置代理地址,因为直接读取系统代理目前可能读取错误
exports.proxy=false /* {
src: 'http://127.0.0.1:8080'
} */
// 网络请求超时,默认10秒
exports.NETWORK_TIMEOUT=10e3
// 根据当前请求的url和响应,选择性的修改响应结果
exports.url2response=async ({
url, method, postData, requestHeaders,
go302,
sleep,
requestPipe,
waitForResponse,
addResponseHeader,
getResponseHeader,
deleteResponseHeader,
setResponseType,
getResponse,
getStatusCode,
setStatusCode,
cacheUrlBy,
cacheData,
cacheClear,
})=>{
if(cacheUrlBy(/xx.js$/)) return cacheData().response
await waitForResponse()
console.log({url, getResponse()})
if(!url.match(/^https\:\/\/www.baidu.com\/$/)) return true
return `<h1>blocked by chrome-dev-tool</h1>`
}
// 是否需要缓存这个请求的结果
// return true 缓存写入默认文件名,return 文件名写入指定文件,return false不写入缓存
exports.url2cachefile=({url})=>{
if(url.match(/jsonp/)) return true
}
// 在 location.href 改变之后被调用(包括historyAPI,hashChange),运行于node上下文
exports.runScriptOnUrlChange=async ({
url,
page,
sleep,
evaluate,
})=>{
console.log(url)
}