User:Cheng chai fung/common/New page patroller.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//========================================================================
//=                    created by en:User:TheJosh                        =
//=        取自User:Liangent/Scripts/NewPagePatroller.js再經修改         =
//=                   本工具对不具有巡查权限的用户无效!                  =
//========================================================================

var npp_http;
var npp_enabled = true;
var npp_num_pages = 5;
var npp_refresh;
var npp_num_idle_req;
var npp_curr_idle_req;

var npp_str_no_ajax = wgULS("浏览器不支持本NewPagePatrol脚本。","瀏覽器不支援本NewPagePatrol腳本");
var npp_str_box_title = wgULS("新条目","新條目");
var npp_str_box_title_updating = wgULS("新条目(更新中)","新條目(更新中)");
var npp_str_box_title_failed = wgULS("新条目(更新失败)","新條目(更新失敗)");

$(npp_init);

/* initalise */
function npp_init() 
{
	
	// allow user settings through
	if(npp_enabled == null)
	{
		npp_enabled = false;
	}
	if(npp_num_pages == null)
	{
		npp_num_pages = 10;
	}
	if(npp_refresh == null)
	{
		npp_refresh = 5;
	}
	if(npp_num_idle_req == null)
	{
		npp_num_idle_req = 60;
	}
	
	// A few limits to be nice to the servers
	if(npp_num_pages > 50)
	{
		npp_num_pages = 50;
	}
	if(npp_num_pages < 1)
	{
		npp_num_pages = 1;
	}
	if(npp_refresh < 2)
	{
		npp_refresh = 2;
	}
	if(npp_num_idle_req > 1000)
	{
		npp_num_idle_req = 1000;
	}
	if(npp_num_idle_req < 5)
	{
		npp_num_idle_req = 5;
	}
	
	// get our cookie
	if(document.cookie.length > 0)
	{
		var c_start = document.cookie.indexOf("npp_show_box=");
		if(c_start != -1)
		{ 
			c_start = c_start + 13; 
			var c_end = document.cookie.indexOf(";", c_start);
			if(c_end == -1)
			{
				c_end = document.cookie.length;
			}
			
			if(document.cookie.substring(c_start, c_end) == "yes")
			{
				npp_enabled = true;
 			} else
			{
				npp_enabled = false;
 			}
		} 
	}
	
	// Container div
	var div = document.createElement('div');
	div.setAttribute('id', 'p-newpages');
	var heading = document.createElement('h5');
	var link_div = document.createElement('div');
	link_div.className = 'body';
	if(npp_enabled == true)
	{
		div.className = 'portal expanded';
		link_div.style.display = 'block';
	} else
	{
		div.className = 'portal collapsed';
		link_div.style.display = 'none';
	}
	heading.appendChild(document.createTextNode(npp_str_box_title));
	div.appendChild(heading);
	div.appendChild(link_div);
	var side_col = document.getElementById('mw-panel');
	var node = document.getElementById('p-navigation');
	side_col.insertBefore(div, node.nextSibling);
	
	// Either make a request or show nothing
	npp_curr_idle_req = 0;
	npp_ajax_request();
}

/* init ajax */
function npp_create_request()
{
	try
	{
		npp_http = new XMLHttpRequest();
	} catch(e)
	{
		try
		{
			npp_http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e)
		{
			try
			{
				npp_http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e)
			{
				return false;
			}
		}
	}
	
	npp_http.onreadystatechange = function()
	{
		if(npp_http.readyState == 4)
		{
			npp_ajax_response();
		}
	}
	
	return true;
}

/* make a request */
function npp_ajax_request() {
	// if we have done too many requests, disable the box
	npp_curr_idle_req++;
	if(npp_curr_idle_req > npp_num_idle_req)
	{
		return;
	}

	// firstly, inform the user
	var cur_box = document.getElementById('p-newpages');
	if(cur_box != null)
	{
		cur_box.firstChild.firstChild.data = npp_str_box_title_updating;
	}

	if(npp_create_request() == false)
	{
		if(cur_box != null && cur_box.childNodes.length != 0)
		{
			cur_box.firstChild.firstChild.data = npp_str_box_title_failed;
		} else
		{
			alert(npp_str_no_ajax);
		}
	}
	
	// Then make the request
	npp_http.open("GET", "/w/api.php?action=query&format=xml&list=recentchanges&rcshow=!bot|!redirect&rctype=new&rcnamespace=0&rcprop=title|timestamp|ids|patrolled&rclimit=" + npp_num_pages, true);
	npp_http.send(null);
}

/* we have received a response */
function npp_ajax_response()
{
	var items = npp_http.responseXML.getElementsByTagName('rc');
	
	var list = document.createElement('ul');
	
	// populate the list with 10 links.
	for(var i = 0; i < items.length; i++)
	{
		var item_name = items[i].getAttribute('title');
		var rcid = items[i].getAttribute('rcid');
		var patrolled = items[i].getAttribute('patrolled') != null;
	
		item_name = item_name.replace(/&/, "%26");
		var item_url = 'http://zh.wikipedia.org/w/index.php?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
	
		a = document.createElement('a');
		a.setAttribute('href', item_url);
		a.appendChild(document.createTextNode(item_name));
	
		var li = document.createElement('li');
		li.appendChild(a);
		if(!patrolled)
		{
			li.setAttribute('class', 'not-patrolled');
		}
		list.appendChild(li);
	}
	
	// now replace the div
	var cur_box = document.getElementById('p-newpages');
	if(cur_box != null && cur_box.childNodes.length != 0)
	{
		cur_box.firstChild.firstChild.data = npp_str_box_title;
		var link_div = cur_box.childNodes[1];
		if(link_div.childNodes.length != 0)
		{
			link_div.replaceChild(list, link_div.firstChild);
		} else
		{
			link_div.appendChild(list);
		}
	}
	
	// and do it again in 5 secs
	setTimeout("npp_ajax_request()", npp_refresh * 1000);
}