JS XMLhttprequest拦截

  1 // ==UserScript==
  2 // @name         carAuto
  3 // @namespace    http://tampermonkey.net/
  4 // @version      0.1
  5 // @description  try to take over the world!
  6 // @author       You
  7 // @match        https://www.jiakaobaodian.com/mnks/exam/car-kemu1-beijing.html
  8 // @icon         https://www.google.com/s2/favicons?domain=jiakaobaodian.com
  9 // @run-at       document-body
 10 // @grant        unsafeWindow
 11 // @grant        GM_xmlhttpRequest
 12 // ==/UserScript==
 13 
 14 (function () {
 15     'use strict';
 16 
 17     //GM_listValues()
 18 
 19     class XMLHttp {
 20         constructor() {
 21             this.request = function (param) {
 22             };
 23             this.response = function (param) {
 24             };
 25         }
 26 
 27     }
 28 
 29     let http = new XMLHttp();
 30 
 31     // 初始化 拦截XMLHttpRequest
 32     function initXMLHttpRequest() {
 33         let open = XMLHttpRequest.prototype.open;
 34         XMLHttpRequest.prototype.open = function (...args) {
 35             let send = this.send;
 36             let _this = this
 37             let post_data = []
 38             this.send = function (...data) {
 39                 post_data = data;
 40                 return send.apply(_this, data)
 41             }
 42             // 请求前拦截
 43             http.request(args)
 44 
 45             this.addEventListener('readystatechange', function () {
 46                 if (this.readyState === 4) {
 47                     let config = {
 48                         url: args[1],
 49                         status: this.status,
 50                         method: args[0],
 51                         data: post_data
 52                     }
 53                     // 请求后拦截
 54                     http.response({config, response: this.response})
 55                 }
 56             }, false)
 57             return open.apply(this, args);
 58         }
 59     }
 60 
 61     var questionsSequences = [];
 62     var answer = [];
 63     // var answer1 = [];
 64     // var answer2 = [];
 65 
 66     // 初始化页面
 67     function getinfo() {
 68 
 69         // XMLHttpRequest 拦截
 70         http.request = function (param) {
 71             //   console.log(param, "---request");
 72         };
 73         http.response = function (res) {
 74 
 75             console.log(res, "---response");
 76             if (res.config.url.indexOf("do-exam.htm") !== -1) {//证明是sequence请求
 77                 questionsSequences = questionsSequences.concat(JSON.parse(res.response).data);
 78             }
 79             if (res.config.url.indexOf("question-list.htm") !== -1) {//证明是answer请求
 80 
 81                 answer = answer.concat(JSON.parse(res.response).data)
 82                 // let answer = JSON.parse(res.response).data
 83                 // for
 84                 //     if (answer.optionType == 0) {
 85                 //         answer1 = answer1.concat(answer)
 86                 //     }
 87                 // if (answer.optionType == 1){
 88                 //     answer2 = answer2.concat()
 89                 // }
 90             }
 91         }
 92 
 93         // 初始化 XMLHttpRequest
 94         initXMLHttpRequest();
 95 
 96 
 97     }
 98 
 99     getinfo()
100 
101 
102     setTimeout(function () {
103             console.log(questionsSequences, answer)
104             let idx = 0
105             // for (let i = 0; i < answer.length; i++) {
106             //     if (answer[idx].optionType == 0) {
107             //         idx++
108             //     } else {
109             //         break
110             //     }
111             // }
112             // answer1 = answer.slice(0, idx)
113             // answer2 = answer.slice(idx, answer.length)
114 
115 
116             var count = 0
117             var inter = null
118 
119             inter = setInterval(function () {
120                 if (count > 98) {
121                     window.clearInterval(inter)
122                 }
123 
124                 let question = document.querySelector("#ComQuestionInfo_qundefined > div.answer-w.clearfix > div")
125 
126                 let questionId = document.querySelector("#ComQuestionInfo_qundefined > div.answer-w.clearfix > div").getAttribute("data-questionid")
127                 console.log(questionId)
128                 // let dataqindex = document.querySelector("#ComQuestionInfo_qundefined > div.answer-w.clearfix > div").getAttribute("data-q-index")
129 
130                 const clickArea = document.querySelector("body > div > div.layout-article > div > div > div > div.col.col2 > fieldset > div.clearfix > div");
131 
132                 // let optionType = answer[dataqindex].optionType
133                 // let answernum = answer[dataqindex].answer
134 
135                 for (let j  = 0; j < answer.length; j++) {
136                     if (questionId==answer[j].questionId){
137                         let optionType = answer[j].optionType
138                         if (optionType == 0) { //判断题
139                             switch (answer[j].answer) {
140                                 case 16: {
141                                     clickArea.children[1].click()
142                                     break
143                                 }
144                                 case 32: {
145                                     clickArea.children[2].click()
146                                 }
147                             }
148 
149 
150                         }
151 
152                         if (optionType == 1) {
153                             switch (answer[j].answer) {
154                                 case 16: {
155                                     let ansText = answer[j]["optionA"]
156                                     for (let p = 0; p < 4; p++) {
157                                         if (question.children[p].innerText.indexOf(ansText) != -1) {
158                                             clickArea.children[p + 1].click()
159                                             break
160                                         }
161                                     }
162                                     break
163                                 }
164                                 case 32: {
165                                     let ansText = answer[j]["optionB"]
166                                     for (let p = 0; p < 4; p++) {
167                                         if (question.children[p].innerText.indexOf(ansText) != -1) {
168                                             clickArea.children[p + 1].click()
169                                             break
170                                         }
171                                     }
172                                     break
173                                 }
174                                 case 64: {
175                                     let ansText = answer[j]["optionC"]
176                                     for (let p = 0; p < 4; p++) {
177                                         if (question.children[p].innerText.indexOf(ansText) != -1) {
178                                             clickArea.children[p + 1].click()
179                                             break
180                                         }
181                                     }
182                                     break
183                                 }
184                                 case 128: {
185                                     let ansText = answer[j]["optionD"]
186                                     for (let p = 0; p < 4; p++) {
187                                         if (question.children[p].innerText.indexOf(ansText) != -1) {
188                                             clickArea.children[p + 1].click()
189                                             break
190                                         }
191                                     }
192 
193                                 }
194                             }
195                         }
196 
197                     }
198                 }
199 
200 
201 
202 
203 
204 
205                 count++
206 
207             }, 300)
208 
209 
210         }
211         ,
212         3000
213     );
214     // }
215     // let questionId = document.querySelector("#ComQuestionInfo_qundefined > div.answer-w.clearfix > div").getAttribute("data-questionid")
216 
217 
218     // getinfo().then(function (res) {
219     //
220     //
221     //         console.log("q", res.questionsSequences);
222     //         console.log("answer", res.answer)
223     //     }
224     // );
225 
226 
227     //GM_xmlhttpRequest({
228     // method: "GET",
229     // url: "https://api2.jiakaobaodian.com/api/open/exam/do-exam.htm?_r=15483695774327888110&carType=car&cityCode=110000&course=kemu1&_=0.05632082456936094",
230     // onload: function(response) {
231     //这里写处理函数
232     //     console.log(response.response);
233 //  }
234 //});
235     //  debugger;
236     //  unsafeWindow.fetch = (...arg) => {
237     //  console.log('fetch arg', ...arg);
238     //  }
239 
240     // Your code here...
241 })();

 

posted @ 2021-09-20 14:54  星云牛马  阅读(1118)  评论(0)    收藏  举报