Greasy Fork is available in English.

NGA优化摸鱼体验插件-信息加强

修复属地显示,成分查询(仅1页)

  1. // ==UserScript==
  2. // @name NGA优化摸鱼体验插件-信息加强
  3. // @namespace https://github.com/DelCrona/NGA_checkInfo
  4. // @version 1.1.1
  5. // @author DelCrona
  6. // @description 修复属地显示,成分查询(仅1页)
  7. // @license MIT
  8. // @require https://cdn.staticfile.net/jquery/3.4.0/jquery.min.js
  9. // @require https://cdn.staticfile.net/spectrum/1.8.0/spectrum.js
  10. // @require https://cdn.staticfile.net/localforage/1.10.0/localforage.min.js
  11. // @require https://cdn.staticfile.net/echarts/5.4.2/echarts.min.js
  12. // @match *://bbs.nga.cn/*
  13. // @match *://ngabbs.com/*
  14. // @match *://nga.178.com/*
  15. // @match *://g.nga.cn/*
  16. // @grant GM_registerMenuCommand
  17. // @grant GM_setValue
  18. // @grant GM_getValue
  19. // @grant GM_deleteValue
  20. // @grant GM_listValues
  21. // @grant unsafeWindow
  22. // @run-at document-start
  23. // @inject-into content
  24. // ==/UserScript==
  25.  
  26. (function (registerPlugin) {
  27. 'use strict';
  28. registerPlugin({
  29. name: 'checkInfo', // 插件唯一KEY
  30. title: '信息增强', // 插件名称
  31. desc: '修复查询端口被封的问题', // 插件说明
  32. settings: [{
  33. key: 'textInput',
  34. title: '占位符',
  35. desc: '描述信息\n描述信息',
  36. }],
  37. requestTasks: [],
  38. currentUserInfo: {},
  39. queryTimer: null,
  40. $el: null,
  41. pageInfo: {},
  42. beforeSaveSettingFunc(setting) {
  43. //console.log(setting)
  44. // return 值则不会保存,并抛出错误
  45. //return '拦截'
  46. },
  47. preProcFunc() {
  48. console.log('已运行: preProcFunc()')
  49. },
  50. initFunc() {
  51. console.log('已运行: initFunc()')
  52. console.log('插件ID: ', this.pluginID)
  53. console.log('插件配置: ', this.pluginSettings)
  54. console.log('主脚本: ', this.mainScript)
  55. console.log('主脚本引用库: ', this.mainScript.libs)
  56. },
  57. postProcFunc() {
  58.  
  59. },
  60. renderThreadsFunc($el) {
  61.  
  62. },
  63. renderFormsFunc($el) {
  64. var _this = this;
  65. const uid = parseInt($el.find('[name="uid"]').text());
  66. const userInfo = unsafeWindow.commonui.userInfo.users[uid];
  67. //console.log(userInfo);
  68. $el.find('.hld__qbc').append('<button id=repair>1页主题回复</button>');
  69. //点击按钮执行
  70. $el.find('.hld__qbc > #repair').click(() => {
  71. if($el.find('.postcontent > .checkInfo_record').length) return;
  72. //给全局变量赋值,方便调用
  73. this.$el = $el;
  74. this.currentUserInfo = userInfo;
  75. this.pageInfo = {
  76. post: {
  77. label: '发布主题',
  78. pages: 0,
  79. status: '',
  80. earliestPostdate: new Date().getTime() / 1000
  81. },
  82. reply: {
  83. label: '回复主题',
  84. pages: 0,
  85. status: '',
  86. earliestPostdate: new Date().getTime() / 1000
  87. }};
  88. // 一页主题+一页回复
  89. _this.checkRecord(uid, 'post', 1);
  90. _this.queryRecord('on');
  91. });
  92. //调用获取信息的函数,异步使用
  93. _this.getUserInfo(uid)
  94. .then(userInfo =>{
  95. this.$el = $el;
  96. //覆盖本体属地信息
  97. _this.displayLoc(userInfo);
  98. })
  99. },
  100. checkRecord(uid, type, page){
  101. return new Promise((resolve, reject) => {
  102. var url = `https://${window.location.host}/thread.php?authorid=${uid}&page=${page}`
  103. if (type == 'reply') url += '&searchpost=1';
  104. $.ajax({url})
  105. .then(postInfo =>{
  106. var _this = this;
  107. var parser = new DOMParser();
  108. var htmlDoc = parser.parseFromString(postInfo, "text/html");
  109. var scriptTags = htmlDoc.querySelectorAll('.titleadd2')
  110. var contentCount = {};
  111. //全局变量翻页
  112. if (page > this.pageInfo[type].pages) {
  113. this.pageInfo[type].pages = page;
  114. }
  115. //获取成分
  116. if (scriptTags.length) {
  117. scriptTags.forEach(element =>{
  118. //console.log(element.textContent);
  119. var content = element.textContent.trim();
  120. if (contentCount[content]) {
  121. contentCount[content]++;
  122. } else {
  123. contentCount[content] = 1;
  124. }
  125. })
  126. }
  127. //打印+合并
  128. _this.displayRecord(uid, contentCount);
  129. resolve(contentCount);
  130. })
  131. })
  132. .catch(error => {
  133. // 请求失败,将错误传递给reject
  134. reject(error);
  135. console.log(error);
  136. });
  137. },
  138. async queryRecord(status){
  139. if(status != 'end'){
  140. var _this = this;
  141. //定时任务,启动!
  142. this.queryTimer = setInterval(async() => {
  143. try {
  144. /*
  145. if(this.pageInfo.post.pages < 3){
  146. console.log(this.pageInfo.post.pages + 1);
  147. await _this.checkRecord(this.currentUserInfo.uid, 'post', this.pageInfo.post.pages + 1)
  148. }
  149. else*/
  150. if(this.pageInfo.reply.pages < 1){
  151. //console.log(this.pageInfo.reply.pages + 1);
  152. await _this.checkRecord(this.currentUserInfo.uid, 'reply', this.pageInfo.reply.pages + 1);
  153. this.queryRecord('end');
  154. }
  155. if(this.pageInfo.post.pages >= 3 && this.pageInfo.reply.pages >= 3) this.queryRecord('end');
  156. } catch(err) {
  157. console.log(err)
  158. this.queryRecord('end');
  159. } finally {
  160.  
  161. }
  162. }, 3000);
  163. } else {
  164. if (this.queryTimer) {
  165. clearInterval(this.queryTimer)
  166. this.queryTimer = null
  167. }
  168. }
  169. },
  170. displayLoc(userInfo){
  171. var _this = this;
  172. //调用本体函数显示属地
  173. var flag = _this.mainScript.getModule('UserEnhance').getCountryFlag(userInfo.ipLoc);
  174. this.$el.find('.hld__user-location').attr('title', `IP属地: ${userInfo.ipLoc}`)
  175. this.$el.find('.hld__user-location > span').replaceWith(flag);
  176. },
  177. displayRecord(uid, contentCount){
  178. //查找是否生成过
  179. var insert = this.$el.find('.postcontent > .checkInfo_record').length;
  180. if(!insert){
  181. //生成第一页
  182. this.$el.find('.postcontent').prepend(`<div class = checkInfo_record><span>${JSON.stringify(contentCount,null,2)}</span></div>`);
  183. }else{
  184. //原值和新值合并
  185. var reply = this.$el.find('.postcontent > .checkInfo_record').text();
  186. var replyJson = JSON.parse(reply);
  187. //轮询每个键,相同则+1,不同则新增
  188. Object.keys(contentCount).forEach(key =>{
  189. replyJson[key] = (replyJson[key] || 0) + contentCount[key];
  190. })
  191. this.$el.find('.postcontent > .checkInfo_record').text(`${JSON.stringify(replyJson,null,2)}`)
  192. //console.log(replyJson);
  193. }
  194. },
  195. //访问个人页获取uid和信息字符串
  196. getUserInfo(uid){
  197. return new Promise((resolve, reject) => {
  198. $.ajax(`https://${window.location.host}/nuke.php?func=ucp&uid=${uid}`)
  199. .then(html => {
  200. var parser = new DOMParser();
  201. var htmlDoc = parser.parseFromString(html, "text/html");
  202. // 查找包含__UCPUSER项的<script>标签
  203. var scriptTags = htmlDoc.querySelectorAll("script");
  204. scriptTags.forEach(scr =>{
  205. //获取标签里的内容
  206. var scrText = scr.textContent;
  207. if (scrText.includes("__UCPUSER")){
  208. //获取用户信息的json文件并以正则提取
  209. var match = scrText.match(/var __UCPUSER =(\{.*\});/);
  210. var userInfo = JSON.parse(match[1]);
  211. // 解析成功,将用户信息传递给resolve
  212. resolve(userInfo);
  213. }
  214. })
  215. })
  216. .catch(error => {
  217. // 请求失败,将错误传递给reject
  218. reject(error);
  219. });
  220. });
  221. },
  222. asyncStyle() {
  223. return `#ngascript_plugin_${this.pluginID} {color: red}`
  224. },
  225. style: `
  226. #ngascript_plugin_test {color: red}
  227. `
  228. })
  229.  
  230. })(function(plugin) {
  231. plugin.meta = GM_info.script
  232. unsafeWindow.ngaScriptPlugins = unsafeWindow.ngaScriptPlugins || []
  233. unsafeWindow.ngaScriptPlugins.push(plugin)
  234. });