JS practice for S1

JS练习脚本

当前为 2024-11-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name JS practice for S1
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.152
  5. // @description JS练习脚本
  6. // @author Lery
  7. // @include *://*.saraba1st.com/2b/*
  8. // @include *://*.stage1st.com/2b/*
  9. // @include *://*.stage1.cc/2b/*
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_addStyle
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/store.js/2.0.12/store.legacy.js
  13. // ==/UserScript==
  14.  
  15. //自动对旧链接进行修改跳转
  16. let pattern0 = new RegExp('^([^.]+\\.)?(saraba1st|stage1(st)?)\\.(com|cc)/2b/read');
  17. if(pattern0.test(document.URL)){
  18. location.href="https://" + location.hostname + "/2b/thread-" + location.href.split("tid-")[1].split(".html")[0] + "-1-1.html";
  19. }
  20.  
  21. //修改网页标题后缀
  22. let pattern1 = new RegExp('^(?:论坛|.+?)(?=(?:论坛)?(?: - Stage1st)? - stage1\\/s1 游戏动漫论坛$)');
  23. if(pattern1.test(document.title)){
  24. document.title = pattern1.exec(document.title) + " - STAGE1";
  25. }
  26.  
  27. //自动签到
  28. if(document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]')){//检测到签名链接
  29. ajaxget(document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]').href);//后台获取链接→进行签名
  30. document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"] + .pipe').remove();//删除链接旁的分隔线
  31. document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]').remove();//删除链接
  32. }
  33.  
  34. //点击更换漫区随机图
  35. if(document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]')){
  36. let changepic = document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]');
  37. changepic.onclick = function(){
  38. document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]').src= "http://ac.stage3rd.com/S1_ACG_randpic.asp?t=" + Math.random();
  39. };
  40. }
  41.  
  42. //记录位置
  43. var timer = null;
  44. window.addEventListener('scroll', function () {
  45. if (typeof timer === 'number') {
  46. clearTimeout(timer);
  47. }
  48. timer = setTimeout(function () {
  49. //添加onscroll事件处理
  50. }, 500);
  51. }, false);
  52.  
  53. //自定义快捷入口
  54. function createLink(name, addr) {
  55. let node = document.createElement("li");
  56. let link = document.createElement("a");
  57. if(window.location.pathname + window.location.search == "/2b/" + addr){
  58. node.className = "a";
  59. }
  60. link.setAttribute("href",addr);
  61. link.setAttribute("hidefocus","true");
  62. link.appendChild(document.createTextNode(name));
  63. node.appendChild(link);
  64. return node;
  65. }
  66.  
  67. //找到导航栏位置
  68. document.getElementsByTagName("h2")[0].childNodes[0].setAttribute("href", "./forum.php");
  69. var linkList = document.getElementById("nv").getElementsByTagName("ul")[0];
  70.  
  71. //增加快捷入口
  72. GM_xmlhttpRequest({
  73. method: "GET",
  74. url: "https://" + location.hostname + "/2b/forum.php?mod=ajax&action=forumjump",//获取快捷导航栏板块页面
  75. onload: function(data){
  76. //识别获取收藏板块列表
  77. let pattern = new RegExp('<div id="flsrchdiv">[\\s\\S]+(?=<script type)');
  78. let xmldata = pattern.exec(data.response);
  79. let parser = new DOMParser();
  80. let doc = parser.parseFromString(xmldata, "application/xml");
  81. let forumItems = doc.querySelectorAll('.jump_bdl li:nth-child(3) p a');
  82. //删除原快捷入口
  83. while(linkList.firstChild) {
  84. linkList.removeChild(linkList.firstChild);
  85. }
  86. //生成新快捷入口
  87. if(forumItems.length >= 1){
  88. let pattern = new RegExp('.*?(?=论坛|$)');
  89. for(let i = 0; i < forumItems.length; i++){
  90. let item = forumItems[i];
  91. let itemText = pattern.exec(item.textContent);
  92. let href = item.getAttribute('href');
  93. linkList.appendChild(createLink(itemText,href));
  94. }
  95. }
  96. /*在此处添加您的自定义快捷入口,格式为 linkList.appendChild(createLink("入口名称","主域名/2b/后面的地址")); ,参考下面提供的默认项*/
  97. linkList.appendChild(createLink("抹布","home.php?mod=space&do=friend&view=blacklist"));
  98. linkList.appendChild(createLink("客户端","thread-1486168-1-1.html"));
  99. linkList.appendChild(createLink("我的回复","home.php?mod=space&do=thread&view=me&type=reply"));
  100. }
  101. });
  102.  
  103. //记录并显示上次阅读的界面
  104. let pattern5 = new RegExp('forum\\.php\\?mod=(forumdisplay|viewthread)|(forum|thread)(-[0-9]+)+\\.html');
  105. if(pattern5.test(document.URL)){
  106. if (!store.enabled) {
  107. }else{
  108. let lasttime = store.get('lasttime') ? store.get('lasttime') : {};
  109. let lastread = store.get('lastread') ? store.get('lastread') : {};
  110. let lastrc = store.get('lastrc') ? store.get('lastrc') : {};
  111. //若检测为帖内
  112. if(unsafeWindow.tid){
  113. //记录储存浏览日期(获取毫秒数后转化为小时数,加上时区差值)
  114. let tiddate = new Date();
  115. let date = tiddate.getTime() / 3600000 + (tiddate.getTimezoneOffset() / 60);
  116. date = date ? date : 1;
  117. lasttime[unsafeWindow.tid] = date;
  118. store.set('lasttime', lasttime);
  119. //记录储存回复数(获取api数据后json格式化并提取回复数)
  120. GM_xmlhttpRequest({
  121. method: "GET",
  122. url: "https://" + location.hostname + "/2b/api/mobile/index.php?module=viewthread&tid=" + unsafeWindow.tid,
  123. onload: function(data){
  124. let json = JSON.parse(data.responseText);
  125. var replies = json.Variables.thread.replies;
  126. replies = replies ? replies : 1;
  127. lastrc[unsafeWindow.tid] = replies;
  128. store.set('lastrc', lastrc);
  129. }
  130. });
  131. //记录储存页数(独处高亮页码按钮)
  132. let page = document.querySelector('#pgt > div > div > strong');
  133. page = page ? page.textContent : 1;
  134. lastread[unsafeWindow.tid] = page;
  135. store.set('lastread', lastread);
  136. }else{
  137. let table = document.getElementsByName('moderate')[0].children[2];
  138. //若检测为板块主题列表
  139. if(table) {
  140. let tbodys = table.getElementsByTagName('tbody');
  141. for(let i = 0;i < tbodys.length;i++) {
  142. let tbody = tbodys[i];
  143. let [ordertype, tid] = tbody.id.split('_');
  144. if(tid){
  145. let page = lastread[tid];
  146. if(page){
  147. //根据记录时间和当前时间差值生成对应RGB色值
  148. let currentdate = new Date();
  149. let cdate = currentdate.getTime() / 3600000 + (currentdate.getTimezoneOffset() / 60);
  150. let hours = cdate - lasttime[tid];
  151. var fcolor = 'rgb(192,51,34)';
  152. if(hours <= 1) {
  153. fcolor = 'rgb(192,51,34)';
  154. }else if(hours > 1 && hours <= 24){
  155. fcolor = 'rgb(' + Math.floor(192-hours) + ',' + Math.floor(51+(hours/4)) + ',' + Math.floor(34+(hours/2)) + ')';
  156. }else if(hours > 24 && hours <= 168){
  157. fcolor = 'rgb(' + Math.floor(168-((hours-24)*5/9)) + ',' + Math.floor(57+((hours-24)/6)) + ',' + Math.floor(46+((hours-24)/4)) + ')';
  158. }else{
  159. fcolor = 'rgb(85,83,83)';
  160. }
  161. //设定返回最后阅读页标记
  162. let oldrc = lastrc[tid];
  163. let ele = document.createElement('a');
  164. ele.style.color = fcolor;
  165. ele.style.fontWeight = 'bold';
  166. ele.style.padding = '1px 3px';
  167. ele.style.border = '1px solid ' + fcolor;
  168. ele.text = '回第' + page + '页';
  169. let prevpage = document.querySelector('#pgt > div > strong');
  170. prevpage = prevpage ? prevpage.textContent : 1;
  171. if(document.querySelector('#' + tbody.id + ' a').href.indexOf("forum.php")!=-1){
  172. ele.href = 'forum.php?mod=viewthread&tid=' + tid + '&extra=page%3D' + prevpage + '&page=' + page;
  173. }else{
  174. ele.href = 'thread-' + tid + '-' + page + '-' + prevpage + '.html';
  175. }
  176. let currentrc = parseInt(document.querySelector('#' + tbody.id + ' > tr > .num > .xi2').innerHTML);//读取当前回复数
  177. //根据回复数差决定是否显示新增值
  178. if(currentrc > oldrc) {
  179. let rc = document.createElement('div');
  180. let node=document.createTextNode('+' + String(currentrc - oldrc));
  181. rc.appendChild(node);
  182. rc.style.display = 'inline';
  183. rc.style.color = '#F6F7EB';
  184. rc.style.backgroundColor = fcolor;
  185. rc.style.fontWeight = 'bold';
  186. rc.style.padding = '1px 3px 1px 1px';
  187. rc.style.border = '1px solid ' + fcolor;
  188. rc.style.borderRadius = '0 4px 4px 0';
  189. ele.style.borderRadius = '4px 0 0 4px';
  190. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  191. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(rc);
  192. }else{
  193. ele.style.borderRadius = '4px';
  194. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }