User:TeaLover1996/common.js: Difference between revisions

Content deleted Content added
TeaLover1996 (talk | contribs)
No edit summary
m Maintenance: Global identifier "$j" has been deprecated mw:ResourceLoader/Migration_guide_(users)#MediaWiki_1.17
 
(20 intermediate revisions by 2 users not shown)
Line 1:
importScript('User:Cameltrader/Advisor.js');
var npp_http;
// Allows the user to select a namespace to search in from the corner search bar
var npp_enabled;
var npp_num_pages;
var npp_refresh;
var npp_num_idle_req;
var npp_curr_idle_req;
 
var npp_str_no_ajax = "There seems to be a problem using the NewPagePatrol script. Your browser is not supported. God Bless!";
var npp_str_box_title = "New pages";
var npp_str_box_title_updating = "New pages (updating)";
var npp_str_box_title_failed = "New pages (update failed)";
var npp_str_enable = "Enable this box";
var npp_str_disable = "Disable this box";
 
$( npp_init );
/* initalise */
prefixString =
function npp_init() {
"<option >Article:</option>\n <option>Article talk:</option>\n <option>Wikipedia:</option>\n <option>Wikipedia talk:</option>\n <option>User:</option>\n <option>User talk:</option>\n<option>WikiProject:</option>\n <option>WikiProject talk:</option>\n<option>Category:</option>\n <option>Category talk:</option>\n<option>Template:</option>\n <option>Template talk:</option>\n <option>Book:</option>\n <option>Book talk:</option>\n<option>Help:</option>\n <option>Special:</option>";
// allow user settings through
$(document).ready(function()
if (npp_enabled == null) {
{
npp_enabled = false;
//retrieve existing elements that we're going to use
}
searchBar = document.getElementById("searchInput");
if (npp_num_pages == null) {
searchButton = document.getElementById("searchButton");
npp_num_pages = 10;
searchForm = document.getElementById("searchform");
}
searchDiv = document.getElementById("simpleSearch");
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") {
//create new elements that we're going to insert
npp_enabled = true;
prefixList = document.createElement("select");
} else {
hiddenInput = document.createElement("input");
npp_enabled = false;
}
}
}
// Either make a request or show nothing
//initialize new elements
npp_curr_idle_req = 0;
prefixList.id = "prefixList";
if (npp_enabled == true) {
prefixList.innerHTML = prefixString;
npp_ajax_request();
hiddenInput.id = "hiddenInput";
} else {
hiddenInput.name = "search";
npp_draw_disabled_box();
hiddenInput.type = "hidden";
}
}
/* init ajax */
//insert new elements
function npp_create_request() {
searchDiv.insertBefore(hiddenInput, searchBar);
try {
searchForm.insertBefore(prefixList, searchDiv);
npp_http = new XMLHttpRequest();
} catch (e) {
//modify old ones
try {
searchForm.onsubmit = createSearchTerm;
npp_http = new ActiveXObject("Msxml2.XMLHTTP");
searchForm.style.marginTop = "0px";
} catch (e) {
searchBar.removeAttribute("name"); //since we're no longer using the search bar to submit the search terms directly
try {
searchDiv.style.display = "inline-block";
npp_http = new ActiveXObject("Microsoft.XMLHTTP");
prefixList.style.marginTop = ".8em";
} catch (e) {
prefixList.tabIndex = searchBar.tabIndex;
return false;
});
}
}
}
npp_http.onreadystatechange = function() {
//onsubmit handler; concatenates prefixList and searchBar and inserts into hidden input prior to submission
if(npp_http.readyState == 4) npp_ajax_response();
function createSearchTerm(e)
}
{
//retrieve used elements
hiddenInput = document.getElementById("hiddenInput");
prefixList = document.getElementById("prefixList");
searchBar = document.getElementById("searchInput");
return true;
//do the work (handling special cases)
}
if(prefixList.value == "Article:")
{
/* make a request */
hiddenInput.value = "" + searchBar.value;
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) {
npp_disable_box();
}
 
// check we are enabled
if (npp_enabled == false) 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.firstChild.firstChild.data = npp_str_box_title_failed;
} else {
alert (npp_str_no_ajax);
}
}
else if(prefixList.value == "Article talk:")
{
// Get the current time
hiddenInput.value = "" + "Talk:" + searchBar.value;
var dateobj = new Date();
var now = Math.floor(dateobj.getTime() / 1000.0);
 
// Go back 5 mins in time
now -= (60 * 5);
 
// 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&rcstart=" + now + "&rclimit=" + npp_num_pages, true);
npp_http.send(null);
}
 
function npp_draw_disabled_box() {
if (mw.config.get('skin') == 'vector') {
npp_draw_disabled_box_vector();
} else {
npp_draw_disabled_box_monobook();
}
}
 
function npp_ajax_response() {
if (mw.config.get('skin') == 'vector') {
npp_ajax_response_vector();
} else {
npp_ajax_response_monobook();
}
 
// and do it again in 5 secs
setTimeout("npp_ajax_request()", npp_refresh * 1000);
}
 
function npp_disable_box() {
npp_enabled = false;
npp_draw_disabled_box();
document.cookie = "npp_show_box=no; path=/";
}
function npp_enable_box() {
npp_enabled = true;
npp_curr_idle_req = 0;
document.cookie = "npp_show_box=yes; path=/";
npp_ajax_request();
}
 
 
/* Draw disabled (monobook) */
function npp_draw_disabled_box_monobook() {
// Container div
var link_div = document.createElement('div');
link_div.className = 'pBody';
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portlet';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
// enable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
var a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_enable));
a.onclick = npp_enable_box;
p.appendChild(a);
link_div.appendChild(p);
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('column-one');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-search');
side_col.insertBefore(div, node);
}
}
 
/* Draw response (monobook) */
function npp_ajax_response_monobook() {
var items = npp_http.responseXML.getElementsByTagName('rc');
// create the div that holds all the newpage links
var link_div = document.createElement('div');
link_div.className = 'pBody';
var list = document.createElement('ul');
link_div.appendChild(list);
// 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 = mw.config.get('wgScript') + '?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);
else if(prefixList.value == "WikiProject:")
{}
hiddenInput.value = "" + "Wikipedia:WikiProject " + searchBar.value;
// Container }div
var div = document.createElement('div');
else if(prefixList.value == "WikiProject talk:")
div.setAttribute('id', 'p-newpages');
{
div.className = 'portlet';
hiddenInput.value = "" + "Wikipedia talk:WikiProject " + searchBar.value;
var heading = document.createElement('h3');
}
heading.appendChild(document.createTextNode(npp_str_box_title));
else
div.appendChild(heading);
{
div.appendChild(link_div);
hiddenInput.value = "" + prefixList.value + searchBar.value;
// disable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
p.style.textAlign = 'right';
a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_disable));
a.onclick = npp_disable_box;
p.appendChild(a);
link_div.appendChild(p);
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('column-one');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-search');
side_col.insertBefore(div, node);
}
}
 
 
/* Draw disabled box (vector skin) */
function npp_draw_disabled_box_vector() {
// Container div
var link_div = document.createElement('div');
link_div.className = 'body';
link_div.style.display = "block";
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portal collapsed';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
// enable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
var a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_enable));
a.onclick = npp_enable_box;
p.appendChild(a);
// add later
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('mw-panel');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-interaction');
side_col.insertBefore(div, node);
}
 
if( typeof $ != 'undefined' ) {
$('#p-newpages > h3').keydown( function( event ) {
if ( event.which == 13 /* Enter */ || event.which == 32 /* Space */ ) {
npp_toggle( $(this) );
}
} )
.mousedown( function() {
npp_toggle( $(this) );
$(this).blur();
return false;
} );
} else {
link_div.appendChild(p);
}
}
 
/* Draw response (vector skin) */
function npp_ajax_response_vector() {
var items = npp_http.responseXML.getElementsByTagName('rc');
// create the div that holds all the newpage links
var link_div = document.createElement('div');
link_div.className = 'body';
link_div.style.display = "block";
var list = document.createElement('ul');
link_div.appendChild(list);
// 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 = mw.config.get('wgScript') + '?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
a = document.createElement('a');
a.setAttribute('href', item_url);
//a.style.fontSize = 'x-small';
a.appendChild(document.createTextNode(item_name));
var li = document.createElement('li');
li.appendChild(a);
if (!patrolled) {
//li.setAttribute('class', 'not-patrolled');
li.setAttribute('style','background-color: #e7e7e7');
//li.style.color = '#555555';
 
}
list.appendChild(li);
}
// Container div
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portal expanded';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
 
// disable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
p.style.textAlign = 'left';
a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_disable));
a.onclick = npp_disable_box;
p.appendChild(a);
// add later
 
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('mw-panel');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-interaction');
side_col.insertBefore(div, node);
}
 
if( typeof $ != 'undefined' ) {
$('#p-newpages > h5').keydown( function( event ) {
if ( event.which == 13 /* Enter */ || event.which == 32 /* Space */ ) {
npp_toggle( $(this) );
}
} )
.mousedown( function() {
npp_toggle( $(this) );
$(this).blur();
return false;
} );
} else {
link_div.appendChild(p);
}
}
 
function npp_toggle( $element ) {
$.cookie( 'vector-nav-' + $element.parent().attr( 'id' ), $element.parent().is( '.collapsed' ) );
if( $element.parent().is('.collapsed') ) {
npp_enable_box();
} else {
npp_disable_box();
}
$element
.parent()
.toggleClass( 'expanded' )
.toggleClass( 'collapsed' )
.find( 'div.body' )
.slideToggle( 'fast' );
}