JS practice for S1

JS练习脚本

当前为 2018-01-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name JS practice for S1
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.142
  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. let pattern = new RegExp('<div id="flsrchdiv">[\\s\\S]+(?=<script type)');
  75. let xmldata = pattern.exec(data.response);
  76. let parser = new DOMParser();
  77. doc = parser.parseFromString(xmldata, "application/xml");
  78. let forumItems = doc.querySelectorAll('.jump_bdl li:nth-child(3) p a');
  79. /*删除原快捷入口*/
  80. while(linkList.firstChild) {
  81. linkList.removeChild(linkList.firstChild);
  82. }
  83. /*生成新快捷入口*/
  84. if(forumItems.length >= 1){
  85. let pattern = new RegExp('.*?(?=论坛|$)');
  86. for(let i = 0; i < forumItems.length; i++){
  87. let item = forumItems[i];
  88. let itemText = pattern.exec(item.textContent);
  89. let href = item.getAttribute('href');
  90. linkList.appendChild(createLink(itemText,href));
  91. }
  92. }
  93. /*在此处添加您的自定义快捷入口,格式为 linkList.appendChild(createLink("入口名称","主域名/2b/后面的地址")); ,参考下面提供的默认项*/
  94. linkList.appendChild(createLink("抹布","home.php?mod=space&do=friend&view=blacklist"));
  95. linkList.appendChild(createLink("客户端","thread-1486168-1-1.html"));
  96. linkList.appendChild(createLink("我的回复","forum.php?mod=guide&view=my&type=reply"));
  97. }
  98. });
  99.  
  100. //记录并显示上次阅读的界面
  101. let pattern5 = new RegExp('forum\\.php\\?mod=(forumdisplay|viewthread)|(forum|thread)(-[0-9]+)+\\.html');
  102. if(pattern5.test(document.URL)){
  103. if (!store.enabled) {
  104. alert('浏览器不支持本地缓存或开启了私隐模式,功能将失效。');
  105. }else{
  106. let lasttime = store.get('lasttime') ? store.get('lasttime') : {};
  107. let lastread = store.get('lastread') ? store.get('lastread') : {};
  108. let lastrc = store.get('lastrc') ? store.get('lastrc') : {};
  109. if(unsafeWindow.tid){
  110. let tiddate = new Date();
  111. let date = tiddate.getTime() / 3600000 + (tiddate.getTimezoneOffset() / 60);
  112. date = date ? date : 1;
  113. lasttime[unsafeWindow.tid] = date;
  114. store.set('lasttime', lasttime);
  115. GM_xmlhttpRequest({
  116. method: "GET",
  117. url: "https://" + location.hostname + "/2b/api/mobile/index.php?module=viewthread&tid=" + unsafeWindow.tid,
  118. onload: function(data){
  119. let json = JSON.parse(data.responseText);
  120. var replies = json.Variables.thread.replies;
  121. replies = replies ? replies : 1;
  122. lastrc[unsafeWindow.tid] = replies;
  123. store.set('lastrc', lastrc);
  124. }
  125. });
  126. let page = document.querySelector('#pgt > div > div > strong');
  127. page = page ? page.textContent : 1;
  128. lastread[unsafeWindow.tid] = page;
  129. store.set('lastread', lastread);
  130. }else{
  131. let table = document.getElementsByName('moderate')[0].children[2];
  132. if(table) {
  133. let tbodys = table.getElementsByTagName('tbody');
  134. for(i = 0;i < tbodys.length;i++) {
  135. let tbody = tbodys[i];
  136. let [ordertype, tid] = tbody.id.split('_');
  137. if(tid){
  138. let page = lastread[tid];
  139. if(page){
  140. let currentdate = new Date();
  141. let cdate = currentdate.getTime() / 3600000 + (currentdate.getTimezoneOffset() / 60);
  142. let hours = cdate - lasttime[tid];
  143. if(hours <= 1) {
  144. var fcolor = 'rgb(192,51,34)';
  145. }else if(hours > 1 && hours <= 24){
  146. var fcolor = 'rgb(' + Math.floor(192-hours) + ',' + Math.floor(51+(hours/4)) + ',' + Math.floor(34+(hours/2)) + ')';
  147. }else if(hours > 24 && hours <= 168){
  148. var fcolor = 'rgb(' + Math.floor(168-((hours-24)*5/9)) + ',' + Math.floor(57+((hours-24)/6)) + ',' + Math.floor(46+((hours-24)/4)) + ')';
  149. }else{
  150. var fcolor = 'rgb(85,83,83)';
  151. }
  152. let oldrc = lastrc[tid];
  153. let ele = document.createElement('a');
  154. ele.style.color = fcolor;
  155. ele.style.fontWeight = 'bold';
  156. ele.style.padding = '1px 3px';
  157. ele.style.border = '1px solid ' + fcolor;
  158. ele.text = '回第' + page + '页';
  159. let prevpage = document.querySelector('#pgt > div > strong');
  160. prevpage = prevpage ? prevpage.textContent : 1;
  161. if(document.querySelector('#' + tbody.id + ' a').href.indexOf("forum.php")!=-1){
  162. ele.href = 'forum.php?mod=viewthread&tid=' + tid + '&extra=page%3D' + prevpage + '&page=' + page;
  163. }else{
  164. ele.href = 'thread-' + tid + '-' + page + '-' + prevpage + '.html';
  165. }
  166. let currentrc = parseInt(document.querySelector('#' + tbody.id + ' > tr > .num > .xi2').innerHTML);
  167. if(currentrc > oldrc) {
  168. let rc = document.createElement('div');
  169. let node=document.createTextNode('+' + String(currentrc - oldrc));
  170. rc.appendChild(node);
  171. rc.style.display = 'inline';
  172. rc.style.color = '#F6F7EB';
  173. rc.style.backgroundColor = fcolor;
  174. rc.style.fontWeight = 'bold';
  175. rc.style.padding = '1px 3px 1px 1px';
  176. rc.style.border = '1px solid ' + fcolor;
  177. rc.style.borderRadius = '0 4px 4px 0';
  178. ele.style.borderRadius = '4px 0 0 4px';
  179. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  180. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(rc);
  181. }else{
  182. ele.style.borderRadius = '4px';
  183. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }