Chrome 谷歌插件开发 监听网页请求和响应数据
admin 阅读: 2024-03-30
后台-插件-广告管理-内容页头部广告(手机) |
manifest.json 中的权限
"permissions": [ "proxy", "unlimitedStorage", "", "declarativeContent", "browsingData", "cookies", "tabs", "storage", "notifications", "webRequest", "webRequestBlocking", "http://*/*", "https://*/*", "debugger", "activeTabs" ],- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
一、使用 webRequest
使用权限 "webRequest","webRequestBlocking"
在背景页中拦截并发出请求获取数据,
webRequest只能拦截到请求,想要获取响应数据可以重发一次请求
- 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
二、使用 debugger
使用权限 "debugger","activeTabs"
背景页使用该方法
- 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
三、使用 ajax_interceptor_manny
该方法只能拦截到 Fetch/XHR 类型 的数据
window.ajax_interceptor_manny = { settings: { switchOn: false, switchQuery:false }, originalXHR: window.XMLHttpRequest, myXHR: function() { let pageScriptEventDispatched = false; const modifyResponse = () => { //this.responseText = overrideTxt; //this.response = overrideTxt; if (pageScriptEventDispatched) { return; } pageScriptEventDispatched = true; ajax_interceptor_manny.download(this.responseText, this.responseURL); } // new 一个原生的 XMLHttpRequest 不需要参数,将 xhr 的属性,都复制给this,暴露到外面 const xhr = new ajax_interceptor_manny.originalXHR(); for (let attr in xhr) { if (attr === 'onreadystatechange') { xhr.onreadystatechange = (...args) => { if (this.readyState == 4 && this.status == 200) { // 请求成功 if (ajax_interceptor_manny.settings.switchOn) { // 开启拦截 modifyResponse(); } } this.onreadystatechange && this.onreadystatechange.apply(this, args); } continue; } else if (attr === 'onload') { xhr.onload = (...args) => { // 请求成功 if (ajax_interceptor_manny.settings.switchOn) { // 开启拦截 modifyResponse(); } this.onload && this.onload.apply(this, args); } continue; } if (typeof xhr[attr] === 'function') { this[attr] = xhr[attr].bind(xhr); } else { if (attr === 'responseText' || attr === 'response') { var k = "_"+attr; Object.defineProperty(this, attr, { get: () => this[k] == undefined ? xhr[attr] : this[k], set: (val) => this[k] = val, }); } else { Object.defineProperty(this, attr, { get: () => xhr[attr], set: (val) => xhr[attr] = val, }); } } } }, originalFetch: window.fetch.bind(window), myFetch: function(...args) { return ajax_interceptor_manny.originalFetch(...args).then((response) => { if (response.ok) { response.clone().text().then((res) => { ajax_interceptor_manny.download(res, response.url); }).catch((e) => { console.warn(e) }); } return response; }); }, download(data, url) { try { if (ajax_interceptor_manny.settings.switchOn) { if (data && typeof data == "string") { data = JSON.parse(data); data.url = url; } if (data){ //遍历需要捕获的接口 captureInterfaces.forEach((item)=>{ if(data.url.indexOf(item) >-1){ console.log('data',data) } } }) } } } catch (e) { console.error("数据获取失败", e); } }, setSetting(data) { if (typeof data !== "object") { return; } //设置环境 for (var i in data) { ajax_interceptor_manny.settings[i] = data[i]; } }, init() { window.XMLHttpRequest = ajax_interceptor_manny.myXHR; window.fetch = ajax_interceptor_manny.myFetch; } } ajax_interceptor_manny.init(); ajax_interceptor_manny.setSetting({ switchOn:true })- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
四、拦截 script 类型 数据
可以使用js注入拦截方法,如未获取到可使用setTimeout等待
let jsonp1 = '' let jsonp2 = '' window.setTimeout(()=>{ window.mtopjsonpCopy1 = window.mtopjsonp1 window.mtopjsonp1 = function(res) { console.log('@@@@@mtopjsonp1',res) jsonp1 = res window.mtopjsonpCopy1(res) window.setTimeout(()=>{ window.mtopjsonpCopy3 = window.mtopjsonp3 window.mtopjsonp3 = function(res) { console.log('@@@@@mtopjsonp3',res) jsonp2 = res window.mtopjsonpCopy3(res) } },200) } },200)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。
在线投稿:投稿 站长QQ:1888636
后台-插件-广告管理-内容页尾部广告(手机) |