NGA Agent

非常时期的非常手段

  1. // ==UserScript==
  2. // @name NGA Agent
  3. // @namespace https://greasyfork.org/users/263018
  4. // @version 1.5.3
  5. // @author snyssss
  6. // @description 非常时期的非常手段
  7. // @license MIT
  8.  
  9. // @match *://bbs.nga.cn/*
  10. // @match *://ngabbs.com/*
  11. // @match *://nga.178.com/*
  12.  
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant unsafeWindow
  17. // @noframes
  18. // ==/UserScript==
  19.  
  20. (({ commonui: ui, __CURRENT_UID: uid, _LOADERREAD: loader }) => {
  21. !function(t,r){for(var n in r)t[n]=r[n]}(window,function(t){function r(e){if(n[e])return n[e].exports;var o=n[e]={i:e,l:!1,exports:{}};return t[e].call(o.exports,o,o.exports,r),o.l=!0,o.exports}var n={};return r.m=t,r.c=n,r.i=function(t){return t},r.d=function(t,n,e){r.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:e})},r.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(n,"a",n),n},r.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},r.p="",r(r.s=2)}([function(t,r,n){"use strict";function e(t,r){var n={};for(var e in t)n[e]=t[e];return n.target=n.currentTarget=r,n}function o(t,r){function n(r){return function(){var n=this.hasOwnProperty(r+"_")?this[r+"_"]:this.xhr[r],e=(t[r]||{}).getter;return e&&e(n,this)||n}}function o(r){return function(n){var o=this.xhr,i=this,u=t[r];if("on"===r.substring(0,2))i[r+"_"]=n,o[r]=function(u){u=e(u,i),t[r]&&t[r].call(i,o,u)||n.call(i,u)};else{var c=(u||{}).setter;n=c&&c(n,i)||n,this[r+"_"]=n;try{o[r]=n}catch(t){}}}}function i(r){return function(){var n=[].slice.call(arguments);if(t[r]){var e=t[r].call(this,n,this.xhr);if(e)return e}return this.xhr[r].apply(this.xhr,n)}}return r=r||window,r[c]=r[c]||r.XMLHttpRequest,r.XMLHttpRequest=function(){for(var t=new r[c],e=0;e<a.length;++e){var f="on"+a[e];void 0===t[f]&&(t[f]=null)}for(var s in t){var l="";try{l=u(t[s])}catch(t){}"function"===l?this[s]=i(s):Object.defineProperty(this,s,{get:n(s),set:o(s),enumerable:!0})}var h=this;t.getProxy=function(){return h},this.xhr=t},Object.assign(r.XMLHttpRequest,{UNSENT:0,OPENED:1,HEADERS_RECEIVED:2,LOADING:3,DONE:4}),r[c]}function i(t){t=t||window,t[c]&&(t.XMLHttpRequest=t[c]),t[c]=void 0}Object.defineProperty(r,"__esModule",{value:!0});var u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};r.configEvent=e,r.hook=o,r.unHook=i;var c="__xhr",a=r.events=["load","loadend","timeout","error","readystatechange","abort"]},,function(t,r,n){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.ah=void 0;var e=n(0);r.ah={hook:e.hook,unHook:e.unHook}}]));
  22. //# sourceMappingURL=ajaxhook.core.min.js.map
  23.  
  24. // KEY
  25. const USER_AGENT_KEY = "USER_AGENT_KEY";
  26. const AUTO_CHECK_IN_ENABLE_KEY = "AUTO_CHECK_IN_ENABLE_KEY";
  27. const AUTO_CHECK_IN_LAST_TIME_KEY = "AUTO_CHECK_IN_LAST_TIME_KEY";
  28.  
  29. // User Agent
  30. const USER_AGENT = (() => {
  31. const data = GM_getValue(USER_AGENT_KEY) || "Nga_Official";
  32.  
  33. GM_registerMenuCommand(`修改UA${data}`, () => {
  34. const value = prompt("修改UA", data);
  35.  
  36. if (value) {
  37. GM_setValue(USER_AGENT_KEY, value);
  38.  
  39. location.reload();
  40. }
  41. });
  42.  
  43. return data;
  44. })();
  45.  
  46. // RedirectUri
  47. const redirectUri = (() => {
  48. const params = new URLSearchParams(location.search);
  49.  
  50. return params.get("redirectUri");
  51. })();
  52.  
  53. // Skip
  54. if (!document.title || location.href.indexOf('/misc/') > 0 || location.search.match(/(lib=login)|(favor=)/)) {
  55. return;
  56. }
  57.  
  58. // Hook
  59. const hooked = (() => {
  60. const isSameOrigin = (url) => {
  61. return url.indexOf("/") === 0 || url.indexOf(location.host) >= 0;
  62. }
  63.  
  64. if (ui && ui.hooked) {
  65. return true;
  66. }
  67.  
  68. if (location.hash.match(/^#pid(\d+)Anchor$/)) {
  69. const pid = parseInt(location.hash.match(/^#pid(\d+)Anchor$/)[1], 10);
  70.  
  71. if (ui &&
  72. ui.postArg &&
  73. Object.values(ui.postArg.data).findIndex(item => item.pid === pid) < 0) {
  74. return false;
  75. }
  76. }
  77.  
  78. if (ui && loader) {
  79. ah.hook(
  80. {
  81. open: (args, xhr) => {
  82. xhr._url = args[1];
  83.  
  84. return false;
  85. },
  86. send: (_, xhr) => {
  87. const url = xhr._url || "";
  88.  
  89. if (isSameOrigin(url)) {
  90. xhr.setRequestHeader("X-User-Agent", USER_AGENT);
  91.        xhr.overrideMimeType("text/plain; charset=gb2312");
  92. }
  93.  
  94. return false;
  95. },
  96. },
  97. ui._w
  98. );
  99.  
  100. const f = __NUKE.doRequest;
  101.  
  102. __NUKE.doRequest = (args) => {
  103. const u = args.u.u || args.u;
  104. const a = args.u.a || {};
  105.  
  106. if (isSameOrigin(u)) {
  107. return f({
  108. ...args,
  109. xr: 1,
  110. u: {
  111. u,
  112. a: {
  113. ...a,
  114. __output: 1,
  115. __inchst: 'UTF8'
  116. },
  117. },
  118. });
  119. }
  120.  
  121. return f(args);
  122. };
  123.  
  124.  
  125. ui.hooked = true;
  126.  
  127. return true;
  128. }
  129.  
  130. return false;
  131. })();
  132.  
  133. // Redirect
  134. if (!hooked && redirectUri === null) {
  135. location.href = `/?redirectUri=${encodeURIComponent(location.href)}`;
  136. return;
  137. }
  138.  
  139. // Clear body event
  140. (() => {
  141. const temp = document.createElement("DIV");
  142.  
  143. temp.append(...document.body.childNodes);
  144.  
  145. document.body.outerHTML = document.body.outerHTML;
  146. document.body.innerHTML = "";
  147. document.body.append(...temp.childNodes);
  148. })();
  149.  
  150. // Reload lib
  151. (() => {
  152. const { topicArg, postArg } = ui;
  153.  
  154. __SCRIPTS.syncLoad("forum", "loaderRead", () => {
  155. loader = unsafeWindow._LOADERREAD;
  156.  
  157. if (loader) {
  158. loader.init();
  159.  
  160. // Reload page
  161. if (redirectUri) {
  162. loader.go(33, {
  163. url: encodeURI(decodeURIComponent(redirectUri)),
  164. });
  165. } else {
  166. (() => {
  167. if (topicArg && topicArg.data.find(item => item[14] > 0)) {
  168. return;
  169. }
  170.  
  171. if (postArg && Object.values(postArg.data).length > 1) {
  172. return;
  173. }
  174.  
  175. loader.go(5, {
  176. url: location.href,
  177. });
  178. })();
  179. }
  180.  
  181. ui.topicArg = topicArg;
  182. }
  183. });
  184. })();
  185.  
  186. // Auto check in
  187. (() => {
  188. const autoCheckInEnable = GM_getValue(AUTO_CHECK_IN_ENABLE_KEY) || false;
  189. const autoCheckInLastTime = GM_getValue(AUTO_CHECK_IN_LAST_TIME_KEY) || 0;
  190.  
  191. if (autoCheckInEnable) {
  192. GM_registerMenuCommand("自动签到:启用", () => {
  193. GM_setValue(AUTO_CHECK_IN_ENABLE_KEY, false);
  194. GM_setValue(AUTO_CHECK_IN_LAST_TIME_KEY, 0);
  195. location.reload();
  196. });
  197. } else {
  198. GM_registerMenuCommand("自动签到:禁用", () => {
  199. GM_setValue(AUTO_CHECK_IN_ENABLE_KEY, true);
  200. location.reload();
  201. });
  202. }
  203.  
  204. if (autoCheckInEnable && uid) {
  205. const today = new Date();
  206.  
  207. const lastTime = new Date(autoCheckInLastTime);
  208.  
  209. const isToday =
  210. lastTime.getDate() === today.getDate() &&
  211. lastTime.getMonth() === today.getMonth() &&
  212. lastTime.getFullYear() === today.getFullYear();
  213.  
  214. if (isToday === false) {
  215. fetch(`/nuke.php?__lib=check_in&__act=check_in&lite=js`, {
  216. method: "POST",
  217. headers: {
  218. "X-User-Agent": USER_AGENT,
  219. },
  220. })
  221. .then((res) => res.blob())
  222. .then((blob) => {
  223. const reader = new FileReader();
  224.  
  225. reader.onload = () => {
  226. const text = reader.result;
  227. const result = JSON.parse(
  228. text.replace("window.script_muti_get_var_store=", "")
  229. );
  230.  
  231. const { data, error } = result;
  232.  
  233. if (data || error) {
  234. alert((data || error)[0]);
  235. }
  236.  
  237. GM_setValue(AUTO_CHECK_IN_LAST_TIME_KEY, today.getTime());
  238. };
  239.  
  240. reader.readAsText(blob, "GBK");
  241. });
  242. }
  243. }
  244. })();
  245. })(unsafeWindow);