保存fetch和xhr

自动保存/备份 fetch/xhr/ajax请求响应的脚本,导出为json文件,可以用于多种用途,比如说自动保存浏览过的b站视频评论、收藏夹、知乎回答、知乎评论、知乎收藏、小红书评论、百度网盘文件列表等等。初始目的为弥补https://archive.org/无法保存动态加载的网页的问题。而且f12 network export har无法仅保存某些请求,且步骤比较繁琐,无法自动。

  1. // ==UserScript==
  2. // @name 保存fetch和xhr
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-04-08
  5. // @description 自动保存/备份 fetch/xhr/ajax请求响应的脚本,导出为json文件,可以用于多种用途,比如说自动保存浏览过的b站视频评论、收藏夹、知乎回答、知乎评论、知乎收藏、小红书评论、百度网盘文件列表等等。初始目的为弥补https://archive.org/无法保存动态加载的网页的问题。而且f12 network export har无法仅保存某些请求,且步骤比较繁琐,无法自动。
  6. // @author You
  7. // @match https://www.zhihu.com/collection/*
  8. // @match https://www.zhihu.com/people/*
  9. // @match https://*.bilibili.com/*
  10. // @match https://cloud.heytap.com/*
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=zhihu.com
  12. // @require https://update.gf.qytechs.cn/scripts/472943/1320613/Itsnotlupus%27%20MiddleMan.js
  13. // @run-at document-start
  14. // @grant GM_setValue
  15. // @grant GM_listValues
  16. // @grant GM_getValue
  17. // @grant GM_deleteValue
  18. // @grant GM_notification
  19. // @license LGPLv3
  20. // ==/UserScript==
  21.  
  22.  
  23.  
  24. newFunction("https://www.zhihu.com/api/v4/*");
  25. newFunction("https://api.bilibili.com/x/v3/*");
  26. newFunction("https://api.bilibili.com/x/space/wbi/arc/search*");
  27. newFunction("https://ck-rest-cn.heytap.com/albumpc/v3/albumDetail");
  28. newFunction("https://api.bilibili.com/x/v2/reply/wbi/*");
  29.  
  30.  
  31.  
  32. function newFunction(hookUrl) {
  33. middleMan.addHook(hookUrl, {
  34. async responseHandler(request, response, error) {
  35. // console.log("snooped on response:",request, response, error);
  36. // console.log(await response?.json())
  37. // let jsonString = JSON.stringify(await response?.json());
  38. let jsonString = await response.json(); // 可以是对象不转换为字符串
  39.  
  40.  
  41. // gpt,使用GM_setValue将response.url作为key把json存储起来
  42. if (jsonString && response.url) {
  43. GM_setValue(response.url, jsonString);
  44. }
  45. }
  46. });
  47. }
  48.  
  49. // gpt,使用GM_listValues和GM_getValue函数,获取所有保存的键值对为一个对象
  50. // 获取所有保存的键值对并组成一个对象
  51. function savedJson() {
  52. let savedData = {};
  53. let keys = GM_listValues();
  54. keys.forEach(key => {
  55. let value = GM_getValue(key);
  56. savedData[key] = value;
  57. });
  58. // 将savedData对象下载为一个json文件
  59. downloadFunc(savedData)
  60. }
  61.  
  62. function downloadFunc(params) {
  63. // 创建一个 JSON 字符串
  64. let jsonData = JSON.stringify(params);
  65. // 创建一个新的 Blob 对象
  66. let blob = new Blob([jsonData], {
  67. type: 'application/json'
  68. });
  69. // 创建一个临时 URL,用于下载 JSON 文件
  70. let url = URL.createObjectURL(blob);
  71. // 创建一个隐藏的链接
  72. let a = document.createElement('a');
  73. a.style.display = 'none';
  74. a.href = url;
  75.  
  76. let currentDate = new Date(+new Date() + 8 * 3600 * 1000).toISOString().slice(0, 19).replace(/[-T:/]/g, ''); // 获取当前日期和时间
  77. a.download = 'savedResponse_' + currentDate + '.json'; // 文件名包含当前日期和时间 // gpt,'savedResponse这里加上当前的日期和时间.json'
  78.  
  79.  
  80. document.body.appendChild(a);
  81.  
  82. a.onclick = (e) => {
  83. e.stopPropagation();
  84. }
  85. // 触发点击事件来下载文件
  86. a.click();
  87. // 释放 URL 对象
  88. URL.revokeObjectURL(url);
  89. }
  90.  
  91.  
  92. // clearSavedData 函数用于执行清空操作
  93. function clearSavedData() {
  94. // 获取所有保存的键值对
  95. let keys = GM_listValues();
  96.  
  97. // 遍历所有键并删除对应的值
  98. keys.forEach(key => {
  99. GM_deleteValue(key);
  100. });
  101.  
  102. // 提示用户清空完成(可选)
  103. // alert('保存的响应数据已清空');
  104. GM_notification({
  105. title: '数据已清空',
  106. text: '保存的数据已清空',
  107. timeout: 3000 // 通知显示时间,单位为毫秒
  108. });
  109. }
  110.  
  111. let top = 0
  112. // gpt,在页面右上方添加一个按钮,点击这个按钮,调用savedJson函数
  113. function addButton(textContent, listener) {
  114. // 创建一个按钮
  115. let button = document.createElement('button');
  116.  
  117. button.textContent = textContent;
  118. button.style.position = 'fixed';
  119. button.style.top = top + 50 + 'px';
  120. top += 50;
  121. button.style.right = '10px';
  122. button.style.zIndex = '9999';
  123.  
  124. // 添加点击事件
  125. button.addEventListener('click', listener);
  126.  
  127. // 添加鼠标悬停和离开事件处理
  128. button.addEventListener('mouseenter', function () {
  129. button.style.backgroundColor = '#ddd'; // 鼠标悬停时的颜色
  130. });
  131.  
  132. button.addEventListener('mouseleave', function () {
  133. button.style.backgroundColor = ''; // 鼠标离开时恢复原色
  134. });
  135.  
  136. // 将按钮添加到页面
  137. document.body.appendChild(button);
  138.  
  139. // gpt,给这个按钮添加鼠标悬停变色的效果
  140. }
  141.  
  142.  
  143. // setTimeout(addButton('下载保存的所有请求数据', savedJson), 3000);
  144. setTimeout(() => addButton('下载保存的所有请求数据', savedJson), 3000);
  145. setTimeout(() => addButton('清空', clearSavedData), 3000);
  146.  
  147. console.log(123);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址