JS practice for S1

JS练习脚本

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         JS practice for S1
// @namespace    http://tampermonkey.net/
// @version      0.12
// @description  JS练习脚本
// @author       Lery
// @include      /^([^\.]+\.)?(saraba1st|stage1(st)?)\.(com|cc)/2b/.*/
// @grant        GM_xmlhttpRequest
// @require      https://cdn.bootcss.com/store.js/1.3.20/store+json2.min.js
// ==/UserScript==

//自动对旧链接进行修改跳转
var pattern0 = new RegExp('^([^\.]+\.)?(saraba1st|stage1(st)?)\.(com|cc)/2b/read');
if(pattern0.test(document.URL)){
	location.href="https://" + location.hostname + "/2b/thread-" + location.href.split("tid-")[1].split(".html")[0] + "-1-1.html"
}

//修改网页标题后缀
var pattern1 = new RegExp('^(?:论坛|.+?)(?=(?:论坛)?(?: - Stage1st)? - stage1\/s1 游戏动漫论坛$)');
if(pattern1.test(document.title)){
   document.title = pattern1.exec(document.title) + " - STAGE1";
}

//自动签到
if(document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]')){
    ajaxget(document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]').href);
	document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"] + .pipe').remove();
    document.querySelector('a[href*="study_daily_attendance-daily_attendance.html"]').remove();
}

//点击更换漫区随机图
if(document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]')){
    var changepic = document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]');
    changepic.onclick = function(){
        document.querySelector('img[src^="http://ac.stage3rd.com/S1_ACG_randpic.asp"]').src="http://ac.stage3rd.com/S1_ACG_randpic.asp"+"?t=" + Math.random();
    };
}

//自定义快捷入口
function createLink(name, addr) {
    var node = document.createElement("li");
    if(window.location.pathname == "/2b/" + addr)
        node.className = "a";
    var link = document.createElement("a");
    link.setAttribute("href",addr);
    link.setAttribute("hidefocus","true");
    link.appendChild(document.createTextNode(name));
    node.appendChild(link);
    return node;
}

/*找到导航栏位置*/
document.getElementsByTagName("h2")[0].childNodes[0].setAttribute("href", "./forum.php");
var linkList = document.getElementById("nv").getElementsByTagName("ul")[0];

/*增加快捷入口*/
GM_xmlhttpRequest({
  method: "GET",
  url: "https://" + location.hostname + "/2b/forum.php?mod=ajax&action=forumjump",
  onload: function(data){
      pattern3 = new RegExp('<div id="flsrchdiv">[\\s\\S]+(?=<script type)');
      var xmldata = pattern3.exec(data.response);
      var parser = new DOMParser();
      doc = parser.parseFromString(xmldata, "application/xml");
      var forumItems = doc.querySelectorAll('.jump_bdl li:nth-child(3) p a');
      /*删除原快捷入口*/
      while(linkList.firstChild) {
          linkList.removeChild(linkList.firstChild);
      }
      /*生成新快捷入口*/
      if(forumItems.length >= 1){
          var pattern4 = new RegExp('.*?(?=论坛|$)');
          for(var i = 0; i < forumItems.length; i++){
              var item = forumItems[i];
              var itemText = pattern4.exec(item.textContent);
              var href = item.getAttribute('href');
              linkList.appendChild(createLink(itemText,href));
          }
      }
	  /*在此处添加您的自定义快捷入口,格式为 linkList.appendChild(createLink("入口名称","主域名/2b/后面的地址")); ,参考下面提供的默认项*/
      linkList.appendChild(createLink("抹布","home.php?mod=space&do=friend&view=blacklist"));
      linkList.appendChild(createLink("客户端","thread-1486168-1-1.html"));
	  linkList.appendChild(createLink("我的回复","forum.php?mod=guide&view=my&type=reply"));
  }
});

//记录并显示上次阅读的界面
var pattern5 = new RegExp('forum\.php\?mod=(forumdisplay|viewthread)|(forum|thread)(-[0-9]+)+\.html');
if(pattern5.test(document.URL)){
	if (!store.enabled) {
		alert('浏览器不支持本地缓存或开启了私隐模式,功能将失效。');
	}else{
		var lastread = store.get('lastread') ? store.get('lastread') : {};
		if(unsafeWindow.tid){
			var page = document.querySelector('#pgt > div > div > strong');
			page = page ? page.textContent : 1;
			lastread[unsafeWindow.tid] = page;
			store.set('lastread', lastread);
		}else{
			var table = document.getElementsByName('moderate')[0].children[2];
			if(table) {
				var tbodys = table.getElementsByTagName('tbody');
				for(i = 0;i < tbodys.length;i++) {
					var tbody = tbodys[i];
					var [ordertype, tid] = tbody.id.split('_');
					if(tid){
						var page = lastread[tid];
						if(page){
							var ele = document.createElement('a');
							ele.style.color = '#C03322';
							ele.style.fontWeight = 'bold';
							ele.style.padding = '1px 3px';
							ele.style.border = '1px solid #C03322';
							ele.style.borderRadius = '4px';
							ele.text = '回第' + page + '页';
							var prevpage = document.querySelector('#pgt > div > strong');
							prevpage = prevpage ? prevpage.textContent : 1;
							if(document.querySelector('#' + tbody.id + ' a').href.indexOf("forum.php")!=-1){
								ele.href = 'forum.php?mod=viewthread&tid=' + tid + '&extra=page%3D' + prevpage + '&page=' + page;
							}else{
								ele.href = 'thread-' + tid + '-' + page + '-' + prevpage + '.html';
							}
							document.querySelector('#' + tbody.id + ' > tr > th').appendChild(ele);
						}
					}
				}
			}
		}
	}
}