NGA优化摸鱼体验插件-标记备份

适用范围:不想使用webdav且标记数量巨大

目前为 2024-04-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name NGA优化摸鱼体验插件-标记备份
  3. // @namespace https://github.com/DelCrona/Mark_LocalBackup
  4. // @version 1.0.2
  5. // @author DelCrona
  6. // @description 适用范围:不想使用webdav且标记数量巨大
  7. // @license MIT
  8. // @match *://bbs.nga.cn/*
  9. // @match *://ngabbs.com/*
  10. // @match *://nga.178.com/*
  11. // @match *://g.nga.cn/*
  12. // @require https://cdn.staticfile.org/blueimp-md5/2.19.0/js/md5.min.js
  13. // @grant unsafeWindow
  14. // @grant GM_xmlhttpRequest
  15. // @run-at document-start
  16. // @inject-into content
  17. // ==/UserScript==
  18.  
  19. (function (registerPlugin) {
  20. 'use strict';
  21. registerPlugin({
  22. name: 'LocalBackup', // 插件唯一KEY
  23. title: '本地备份', // 插件名称
  24. desc: '将标记列表备份到本地/上传', // 插件说明
  25. settings: [{
  26. key: 'textInput',
  27. title: '重要提示:使用前请先用本体功能备份标记',
  28. desc: ''
  29. }, {
  30. key: 'numberInput',
  31. title: '上传功能上传非标记文件会导致标记直接损坏!',
  32. desc: ''
  33. }, {
  34. key: 'checkBox',
  35. title: '请务必确认文件是否为标记文件.json!',
  36. desc: ''
  37. }, {
  38. key: 'checkBox',
  39. title: '覆盖上传同理,使用前优先备份!',
  40. desc: ''
  41. },{
  42. key: 'desc',
  43. title: '备份文件:自动下载标记文件',
  44. desc: ''
  45. }, {
  46. key: 'desc2',
  47. title: '覆盖上传标记:上传后直接覆盖原标记',
  48. desc: ''
  49. }, {
  50. key: 'desc3',
  51. title: '合并上传标记:将现存标记和上传标记合并',
  52. desc: ''
  53. }],
  54. buttons: [ {
  55. title: '备份标记',
  56. action: 'backup'
  57. },{
  58. title: '覆盖上传标记',
  59. action: 'loadFile'
  60. },{
  61. title: '合并上传标记',
  62. action: 'mergeFile'
  63. }],
  64. beforeSaveSettingFunc(setting) {
  65. // console.log(setting)
  66. // return 值则不会保存,并抛出错误
  67. // return '拦截'
  68. },
  69. preProcFunc() {
  70.  
  71. },
  72. initFunc() {
  73. /*
  74. console.log('已运行: initFunc()')
  75. console.log('插件ID: ', this.pluginID)
  76. console.log('插件配置: ', this.pluginSettings)
  77. console.log('主脚本: ', this.mainScript)
  78. console.log('主脚本引用库: ', this.mainScript.libs)
  79. */
  80. },
  81. postProcFunc() {
  82. },
  83. renderThreadsFunc($el) {
  84.  
  85. },
  86. renderFormsFunc($el) {
  87. },
  88. renderAlwaysFunc() {
  89. // console.log('循环运行: renderAlwaysFunc()')
  90. },
  91. async backup(){
  92. const markList = this.mainScript.getModule('MarkAndBan').markList;
  93. const markListStr = JSON.stringify(markList);
  94. const filename = "NGA_marklist";
  95. const mimeType = "application/json";
  96. // 模拟下载功能导出列表
  97. const blob = new Blob([markListStr], {type: mimeType});
  98. const a = document.createElement('a');
  99. a.href = URL.createObjectURL(blob);
  100. a.download = filename;
  101. document.body.appendChild(a);
  102. a.click();
  103. document.body.removeChild(a);
  104. },
  105. async loadFile(){
  106. // 创建一个隐藏的上传按钮
  107. const _this = this;
  108. var uploadButton = document.createElement('input');
  109. uploadButton.type = 'file';
  110. uploadButton.style.display = 'none';
  111. uploadButton.id = 'hiddenFileInput'; // 为上传按钮设置一个ID,方便后续操作
  112. // 将隐藏的上传按钮添加到DOM中
  113. document.body.appendChild(uploadButton);
  114. // 为上传按钮添加change事件监听器
  115. uploadButton.addEventListener('change', function() {
  116. // 获取用户选择的文件
  117. var file = this.files[0];
  118. if (file) {
  119. // 读取文件内容
  120. const markList = _this.mainScript.getModule('MarkAndBan').markList;
  121. const markListStr = JSON.stringify(markList);
  122. var reader = new FileReader();
  123. reader.onload = function(event) {
  124. // 文件读取完成后,输出文件内容
  125. // console.log('文件内容:', event.target.result);
  126. const markUpload = event.target.result;
  127. // 在这里可以对文件内容进行进一步处理,例如显示在网页上或发送到服务器
  128. if (md5(markUpload) !== md5(markListStr)){
  129. const markListStr = markUpload;
  130. const markList = JSON.parse(markListStr);
  131. _this.mainScript.getModule('MarkAndBan').markList = markList;
  132. _this.mainScript.setValue("hld__NGA_mark_list", markListStr);
  133. _this.mainScript.popNotification('标记名单列表已还原');
  134. }
  135. // 清理资源
  136. reader = null;
  137. };
  138. // 以文本格式读取文件内容
  139. reader.readAsText(file);
  140. }
  141. // 移除上传按钮
  142. document.body.removeChild(uploadButton);
  143. });
  144. // 模拟点击上传按钮
  145. uploadButton.click();
  146. },
  147. async mergeFile(){
  148. const _this = this;
  149. var uploadButton = document.createElement('input');
  150. uploadButton.type = "file";
  151. uploadButton.style.display = "none";
  152. uploadButton.id = 'hiddenFileInput'; // 为上传按钮设置一个ID,方便后续操作
  153. document.body.appendChild(uploadButton);
  154. uploadButton.addEventListener('change', function() {
  155. // 获取用户选择的文件
  156. var file = this.files[0];
  157. if (file) {
  158. // 读取文件内容
  159. const fileA = _this.mainScript.getModule('MarkAndBan').markList;
  160. const markListStr = JSON.stringify(fileA);
  161. var reader = new FileReader();
  162. reader.onload = function(event) {
  163. // 文件读取完成后,输出文件内容
  164. // console.log('文件内容:', event.target.result);
  165. const markUpload = event.target.result;
  166. // 在这里可以对文件内容进行进一步处理,例如显示在网页上或发送到服务器
  167. if (md5(markUpload) !== md5(markListStr)){
  168. const fileB = JSON.parse(markUpload);
  169. var mergedJSON = fileA;
  170. fileB.forEach(function(objB){
  171. var matchObj = mergedJSON.findIndex(function(objA){
  172. return objA.uid === objB.uid;
  173. });
  174. if (matchObj !== -1){
  175. objB.marks.forEach(function(markB){
  176. var existMark = mergedJSON[matchObj].marks.find(function(markA){
  177. return (markA.mark === markB.mark && markA.bg_color === markB.bg_color);
  178. });
  179. if (!existMark){
  180. mergedJSON[matchObj].marks.push(markB);
  181. }
  182. })
  183. } else {
  184. mergedJSON.push(objB);
  185. }
  186. })
  187. const mergedJSONStr = JSON.stringify(mergedJSON);
  188. _this.mainScript.getModule('MarkAndBan').markList = mergedJSON;
  189. _this.mainScript.setValue("hld__NGA_mark_list", mergedJSONStr);
  190. _this.mainScript.popNotification('标记名单列表已合并');
  191. }
  192. // 清理资源
  193. reader = null;
  194. };
  195. // 以文本格式读取文件内容
  196. reader.readAsText(file);
  197. }
  198. // 移除上传按钮
  199. document.body.removeChild(uploadButton);
  200. });
  201.  
  202. uploadButton.click();
  203. }
  204. })
  205.  
  206. })(function(plugin) {
  207. plugin.meta = GM_info.script
  208. unsafeWindow.ngaScriptPlugins = unsafeWindow.ngaScriptPlugins || []
  209. unsafeWindow.ngaScriptPlugins.push(plugin)
  210. });

QingJ © 2025

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