JS practice for S1

JS练习脚本

目前為 2018-01-23 提交的版本,檢視 最新版本

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