JS practice for S1

JS练习脚本

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

  1. // ==UserScript==
  2. // @name JS practice for S1
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.121
  5. // @description JS练习脚本
  6. // @author Lery
  7. // @include /^([^\.]+\.)?(saraba1st|stage1(st)?)\.(com|cc)/2b/.*
  8. // @grant GM_xmlhttpRequest
  9. // @require https://cdn.bootcss.com/store.js/1.3.20/store+json2.min.js
  10. // ==/UserScript==
  11.  
  12. //自动对旧链接进行修改跳转
  13. var pattern0 = new RegExp('^([^.]+\\.)?(saraba1st|stage1(st)?)\\.(com|cc)/2b/read');
  14. if(pattern0.test(document.URL)){
  15. location.href="https://" + location.hostname + "/2b/thread-" + location.href.split("tid-")[1].split(".html")[0] + "-1-1.html"
  16. }
  17.  
  18. //修改网页标题后缀
  19. var pattern1 = new RegExp('^(?:论坛|.+?)(?=(?:论坛)?(?: - Stage1st)? - stage1\\/s1 游戏动漫论坛$)');
  20. if(pattern1.test(document.title)){
  21. document.title = pattern1.exec(document.title) + " - STAGE1";
  22. }
  23.  
  24. //自动签到
  25. if(document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]')){
  26. ajaxget(document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]').href);
  27. document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"] + .pipe').remove();
  28. document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]').remove();
  29. }
  30.  
  31. //点击更换漫区随机图
  32. if(document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]')){
  33. var changepic = document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]');
  34. changepic.onclick = function(){
  35. document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]').src="http://ac.stage3rd.com/S1_ACG_randpic.asp"+"?t=" + Math.random();
  36. };
  37. }
  38.  
  39. //自定义快捷入口
  40. function createLink(name, addr) {
  41. var node = document.createElement("li");
  42. if(window.location.pathname == "/2b/" + addr)
  43. node.className = "a";
  44. var link = document.createElement("a");
  45. link.setAttribute("href",addr);
  46. link.setAttribute("hidefocus","true");
  47. link.appendChild(document.createTextNode(name));
  48. node.appendChild(link);
  49. return node;
  50. }
  51.  
  52. /*找到导航栏位置*/
  53. document.getElementsByTagName("h2")[0].childNodes[0].setAttribute("href", "./forum.php");
  54. var linkList = document.getElementById("nv").getElementsByTagName("ul")[0];
  55.  
  56. /*增加快捷入口*/
  57. GM_xmlhttpRequest({
  58. method: "GET",
  59. url: "https://" + location.hostname + "/2b/forum.php?mod=ajax&action=forumjump",
  60. onload: function(data){
  61. pattern3 = new RegExp('<div id="flsrchdiv">[\\s\\S]+(?=<script type)');
  62. var xmldata = pattern3.exec(data.response);
  63. var parser = new DOMParser();
  64. doc = parser.parseFromString(xmldata, "application/xml");
  65. var forumItems = doc.querySelectorAll('.jump_bdl li:nth-child(3) p a');
  66. /*删除原快捷入口*/
  67. while(linkList.firstChild) {
  68. linkList.removeChild(linkList.firstChild);
  69. }
  70. /*生成新快捷入口*/
  71. if(forumItems.length >= 1){
  72. var pattern4 = new RegExp('.*?(?=论坛|$)');
  73. for(var i = 0; i < forumItems.length; i++){
  74. var item = forumItems[i];
  75. var itemText = pattern4.exec(item.textContent);
  76. var href = item.getAttribute('href');
  77. linkList.appendChild(createLink(itemText,href));
  78. }
  79. }
  80. /*在此处添加您的自定义快捷入口,格式为 linkList.appendChild(createLink("入口名称","主域名/2b/后面的地址")); ,参考下面提供的默认项*/
  81. linkList.appendChild(createLink("抹布","home.php?mod=space&do=friend&view=blacklist"));
  82. linkList.appendChild(createLink("客户端","thread-1486168-1-1.html"));
  83. linkList.appendChild(createLink("我的回复","forum.php?mod=guide&view=my&type=reply"));
  84. }
  85. });
  86.  
  87. //记录并显示上次阅读的界面
  88. var pattern5 = new RegExp('forum\\.php\\?mod=(forumdisplay|viewthread)|(forum|thread)(-[0-9]+)+\\.html');
  89. if(pattern5.test(document.URL)){
  90. if (!store.enabled) {
  91. alert('浏览器不支持本地缓存或开启了私隐模式,功能将失效。');
  92. }else{
  93. var lastread = store.get('lastread') ? store.get('lastread') : {};
  94. if(unsafeWindow.tid){
  95. var page = document.querySelector('#pgt > div > div > strong');
  96. page = page ? page.textContent : 1;
  97. lastread[unsafeWindow.tid] = page;
  98. store.set('lastread', lastread);
  99. }else{
  100. var table = document.getElementsByName('moderate')[0].children[2];
  101. if(table) {
  102. var tbodys = table.getElementsByTagName('tbody');
  103. for(i = 0;i < tbodys.length;i++) {
  104. var tbody = tbodys[i];
  105. var [ordertype, tid] = tbody.id.split('_');
  106. if(tid){
  107. var page = lastread[tid];
  108. if(page){
  109. var ele = document.createElement('a');
  110. ele.style.color = '#C03322';
  111. ele.style.fontWeight = 'bold';
  112. ele.style.padding = '1px 3px';
  113. ele.style.border = '1px solid #C03322';
  114. ele.style.borderRadius = '4px';
  115. ele.text = '回第' + page + '页';
  116. var prevpage = document.querySelector('#pgt > div > strong');
  117. prevpage = prevpage ? prevpage.textContent : 1;
  118. if(document.querySelector('#' + tbody.id + ' a').href.indexOf("forum.php")!=-1){
  119. ele.href = 'forum.php?mod=viewthread&tid=' + tid + '&extra=page%3D' + prevpage + '&page=' + page;
  120. }else{
  121. ele.href = 'thread-' + tid + '-' + page + '-' + prevpage + '.html';
  122. }
  123. document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }