Greasy Fork 还支持 简体中文。

JS practice for S1

JS练习脚本

目前為 2018-09-16 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name JS practice for S1
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.146
  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://cdn.bootcss.com/store.js/1.3.20/store.min.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. if(window.location.pathname == "/2b/" + addr)
  57. node.className = "a";
  58. let link = document.createElement("a");
  59. link.setAttribute("href",addr);
  60. link.setAttribute("hidefocus","true");
  61. link.appendChild(document.createTextNode(name));
  62. node.appendChild(link);
  63. return node;
  64. }
  65.  
  66. //找到导航栏位置
  67. document.getElementsByTagName("h2")[0].childNodes[0].setAttribute("href", "./forum.php");
  68. var linkList = document.getElementById("nv").getElementsByTagName("ul")[0];
  69.  
  70. //增加快捷入口
  71. GM_xmlhttpRequest({
  72. method: "GET",
  73. url: "https://" + location.hostname + "/2b/forum.php?mod=ajax&action=forumjump",//获取快捷导航栏板块页面
  74. onload: function(data){
  75. //识别获取收藏板块列表
  76. let pattern = new RegExp('<div id="flsrchdiv">[\\s\\S]+(?=<script type)');
  77. let xmldata = pattern.exec(data.response);
  78. let parser = new DOMParser();
  79. let doc = parser.parseFromString(xmldata, "application/xml");
  80. let forumItems = doc.querySelectorAll('.jump_bdl li:nth-child(3) p a');
  81. //删除原快捷入口
  82. while(linkList.firstChild) {
  83. linkList.removeChild(linkList.firstChild);
  84. }
  85. //生成新快捷入口
  86. if(forumItems.length >= 1){
  87. let pattern = new RegExp('.*?(?=论坛|$)');
  88. for(let i = 0; i < forumItems.length; i++){
  89. let item = forumItems[i];
  90. let itemText = pattern.exec(item.textContent);
  91. let href = item.getAttribute('href');
  92. linkList.appendChild(createLink(itemText,href));
  93. }
  94. }
  95. /*在此处添加您的自定义快捷入口,格式为 linkList.appendChild(createLink("入口名称","主域名/2b/后面的地址")); ,参考下面提供的默认项*/
  96. linkList.appendChild(createLink("抹布","home.php?mod=space&do=friend&view=blacklist"));
  97. linkList.appendChild(createLink("客户端","thread-1486168-1-1.html"));
  98. linkList.appendChild(createLink("我的回复","forum.php?mod=guide&view=my&type=reply"));
  99. }
  100. });
  101.  
  102. //记录并显示上次阅读的界面
  103. let pattern5 = new RegExp('forum\\.php\\?mod=(forumdisplay|viewthread)|(forum|thread)(-[0-9]+)+\\.html');
  104. if(pattern5.test(document.URL)){
  105. if (!store.enabled) {
  106. alert('浏览器不支持本地缓存或开启了私隐模式,功能将失效。');
  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(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. if(hours <= 1) {
  152. var fcolor = 'rgb(192,51,34)';
  153. }else if(hours > 1 && hours <= 24){
  154. var fcolor = 'rgb(' + Math.floor(192-hours) + ',' + Math.floor(51+(hours/4)) + ',' + Math.floor(34+(hours/2)) + ')';
  155. }else if(hours > 24 && hours <= 168){
  156. var fcolor = 'rgb(' + Math.floor(168-((hours-24)*5/9)) + ',' + Math.floor(57+((hours-24)/6)) + ',' + Math.floor(46+((hours-24)/4)) + ')';
  157. }else{
  158. var fcolor = 'rgb(85,83,83)';
  159. }
  160. //设定返回最后阅读页标记
  161. let oldrc = lastrc[tid];
  162. let ele = document.createElement('a');
  163. ele.style.color = fcolor;
  164. ele.style.fontWeight = 'bold';
  165. ele.style.padding = '1px 3px';
  166. ele.style.border = '1px solid ' + fcolor;
  167. ele.text = '回第' + page + '页';
  168. let prevpage = document.querySelector('#pgt > div > strong');
  169. prevpage = prevpage ? prevpage.textContent : 1;
  170. if(document.querySelector('#' + tbody.id + ' a').href.indexOf("forum.php")!=-1){
  171. ele.href = 'forum.php?mod=viewthread&tid=' + tid + '&extra=page%3D' + prevpage + '&page=' + page;
  172. }else{
  173. ele.href = 'thread-' + tid + '-' + page + '-' + prevpage + '.html';
  174. }
  175. let currentrc = parseInt(document.querySelector('#' + tbody.id + ' > tr > .num > .xi2').innerHTML);//读取当前回复数
  176. //根据回复数差决定是否显示新增值
  177. if(currentrc > oldrc) {
  178. let rc = document.createElement('div');
  179. let node=document.createTextNode('+' + String(currentrc - oldrc));
  180. rc.appendChild(node);
  181. rc.style.display = 'inline';
  182. rc.style.color = '#F6F7EB';
  183. rc.style.backgroundColor = fcolor;
  184. rc.style.fontWeight = 'bold';
  185. rc.style.padding = '1px 3px 1px 1px';
  186. rc.style.border = '1px solid ' + fcolor;
  187. rc.style.borderRadius = '0 4px 4px 0';
  188. ele.style.borderRadius = '4px 0 0 4px';
  189. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  190. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(rc);
  191. }else{
  192. ele.style.borderRadius = '4px';
  193. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }