What.CD : Mark The OP

Marks the thread's [OP]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @id          whatcd-mark-op
// @name        What.CD : Mark The OP
// @namespace   hateradio)))
// @description Marks the thread's [OP]
// @include     http*://*what.cd/forums.php*viewthread*
// @version     1.1.1
// @created     17 June 2012
// @grant       none
// ==/UserScript==

(function(){
	var mark = {
		id : 0,
		doc : document,
		findOP: function(){
			var a = document.querySelector('.breadcrumbs ~ .linkbox a:first-child'), xhr = new XMLHttpRequest();
			if(a && a.textContent === '<< First'){
				xhr.open('get', a.href, true);
				xhr.onload = this.xhr;
				xhr.send(null);
			}else{
				this.proc();
			}
		},
		xhr: function(){
			var d = document.implementation.createHTMLDocument('');
			d.documentElement.innerHTML = this.responseText;
			mark.doc = d;
			mark.proc();
		},
		proc : function(){
			this.grabID();
			if(this.id > 0){
				this.grabPosts();
				this.modPosts();
			}
		},
		grabID : function(){
			var u = this.doc.querySelector('table.forum_post a[href*="user.php"]');
			if(/(?:id=(\d+))/.test(u)){
				this.id = RegExp.lastParen;
			}
		},
		grabPosts : function(){
			this.posts = document.querySelectorAll('.forum_post a[href="user.php?id='+this.id+'"]');
		},
		modPosts : function(){
			var i = this.posts.length, a;
			while(i--){
				a = this.posts[i];
				a.parentNode.insertBefore(document.createTextNode('[OP] '), a);
			}
		}
	};
	mark.findOP();
}());