JS practice for S1

JS练习脚本

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