preload.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const electron = require('electron')
  2. electron.contextBridge.exposeInMainWorld("electron", {
  3. ipcRenderer: electron.ipcRenderer,
  4. });
  5. // 拦截请求
  6. (function(xhr) {
  7. var XHR = XMLHttpRequest.prototype;
  8. var open = XHR.open;
  9. var send = XHR.send;
  10. var setRequestHeader = XHR.setRequestHeader;
  11. XHR.open = function(method, url) {
  12. this._method = method;
  13. this._url = url;
  14. this._requestHeaders = {};
  15. this._startTime = (new Date()).toISOString();
  16. return open.apply(this, arguments);
  17. };
  18. XHR.setRequestHeader = function(header, value) {
  19. this._requestHeaders[header] = value;
  20. return setRequestHeader.apply(this, arguments);
  21. };
  22. XHR.send = function(postData) {
  23. this.addEventListener('load', function() {
  24. var endTime = (new Date()).toISOString();
  25. var myUrl = this._url ? this._url.toLowerCase() : this._url;
  26. if(myUrl) {
  27. if (postData) {
  28. if (typeof postData === 'string') {
  29. try {
  30. // here you get the REQUEST HEADERS, in JSON format, so you can also use JSON.parse
  31. this._requestHeaders = postData;
  32. } catch(err) {
  33. console.log('Request Header JSON decode failed, transfer_encoding field could be base64');
  34. console.log(err);
  35. }
  36. } else if (typeof postData === 'object' || typeof postData === 'array' || typeof postData === 'number' || typeof postData === 'boolean') {
  37. // do something if you need
  38. }
  39. }
  40. // here you get the RESPONSE HEADERS
  41. var responseHeaders = this.getAllResponseHeaders();
  42. if ( this.responseType != 'blob' && this.responseText) {
  43. // responseText is string or null
  44. try {
  45. // here you get RESPONSE TEXT (BODY), in JSON format, so you can use JSON.parse
  46. var arr = this.responseText;
  47. // printing url, request headers, response headers, response body, to console
  48. // console.log(this._url);
  49. // console.log(JSON.parse(this._requestHeaders));
  50. // console.log(responseHeaders);
  51. // console.log(myUrl);
  52. if (window.owoHackUrl) {
  53. for (const key in window.owoHackUrl) {
  54. if (Object.hasOwnProperty.call(window.owoHackUrl, key)) {
  55. const element = window.owoHackUrl[key];
  56. if (myUrl.includes(key)) {
  57. if (element) element(arr)
  58. }
  59. }
  60. }
  61. }
  62. if (window.owoHackUrlSend) {
  63. for (const key in window.owoHackUrlSend) {
  64. if (Object.hasOwnProperty.call(window.owoHackUrlSend, key)) {
  65. const element = window.owoHackUrlSend[key];
  66. if (myUrl.includes(key)) {
  67. if (element) element(postData, myUrl)
  68. }
  69. }
  70. }
  71. }
  72. } catch(err) {
  73. console.log("Error in responseType try catch");
  74. console.log(err);
  75. }
  76. }
  77. }
  78. });
  79. return send.apply(this, arguments);
  80. };
  81. })(XMLHttpRequest);