User:Laaknor/RecentChangesPatrol.js

/* Original script http://en.wikipedia.org/wiki/User:TheJosh/Scripts/RecentChangesPatrol.js */ /* Adapted and rewritten to nowiki by User:Laaknor http://no.wikipedia.org/wiki/Bruker:Laaknor/Scripts/RecentChangesPatrol.js */ /* Moved back to enwiki 6th August 2008, Laaknor */ var rcp_http; var rcp_enabled; var rcp_num_pages; var rcp_refresh; var rcp_order; var rcp_max; var rcp_count; function rcp_get_cookie ( cookie_name ) { var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(|$)' ); if ( results ) return ( unescape ( results[2] ) ); else return null; } $( rcp_init ); /* initalise */ function rcp_init { // allow user settings through if(rcp_enabled == null) { rcp_enabled = false; } if(rcp_num_pages == null) { rcp_num_pages = 10; } if(rcp_refresh == null) { rcp_refresh = 10; } // if(rcp_order == null) { //   rcp_order = 'newer'; // }  if(rcp_max == null) { rcp_max = 36; } if(rcp_count == null) { rcp_count = 0; } // A few limits to be nice to the servers if (rcp_num_pages > 50) { rcp_num_pages = 50; } if (rcp_num_pages < 1) { rcp_num_pages = 1; } if (rcp_refresh < 2) { rcp_refresh = 2; } // get our cookie if (document.cookie.length > 0) { /*   var c_start = document.cookie.indexOf("rcp_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") { rcp_enabled = true; } else { rcp_enabled = false; } // end if c_end } // end if (c_start != -1) */   var c_show = rcp_get_cookie("rcp_show_box"); if(c_show == "yes") { rcp_enabled = true; } else { rcp_enabled = false; }   var c_order = rcp_get_cookie("rcp_order"); if(c_order == "newer") { rcp_order = "newer"; } else { rcp_order = "older"; } } // end if (document.cookie.length > 0) // Either make a request or show nothing if (rcp_enabled == true) { rcp_ajax_request; } else { rcp_draw_disabled_box; } } /* init ajax */ function rcp_create_request { try { rcp_http = new XMLHttpRequest; } catch (e) { try { rcp_http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { rcp_http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; }   }  }  rcp_http.onreadystatechange = function { if(rcp_http.readyState == 4) rcp_ajax_response; } return true; } /* make a request */ function rcp_ajax_request { // check we are enabled if (rcp_max < rcp_count++) return; if (rcp_enabled == false) return; // firstly, inform the user var cur_box = document.getElementById('p-recentchanges'); if (cur_box != null) { cur_box.firstChild.firstChild.data = 'Unpatrolled (updating)'; } if (rcp_create_request  == false) { if (cur_box != null) { cur_box.firstChild.firstChild.data = 'Unpatrolled (update feiled)'; } else { alert ("There seems to be a problem using the RecentChangePatrol script. I can't make AJAX objects, so I'm just going to complain. God Bless!"); } }  // Then make the request rcp_http.open("GET", "http://en.wikipedia.org/w/api.php?action=query&list=recentchanges&format=xml&rcshow=!patrolled&rctype=new&rcprop=patrolled|title|timestamp|ids&rcdir=" + rcp_order + "&rclimit=" + rcp_num_pages, true); rcp_http.send(null); } /* we have received a response */ function rcp_ajax_response { var items = rcp_http.responseXML.getElementsByTagName('rc'); /* Check if no response. Could mean no right */ if(items.length == 0) { rcp_disable_box; } // create the div that holds all the recentchanges 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 item_patrolled = items[i].getAttribute('patrolled'); var item_type = items[i].getAttribute('type'); if (item_type == "new") { var item_rcid = items[i].getAttribute('rcid'); var item_url = 'http://en.wikipedia.org/w/index.php?title=' + escape(item_name) + '&rcid=' + escape(item_rcid); } else { var item_oldrev = items[i].getAttribute('old_revid'); var item_url = 'http://en.wikipedia.org/w/index.php?diff=next&oldid=' + escape(item_oldrev) + '&title=' + escape(item_name); }   a = document.createElement('a'); a.setAttribute('href', item_url); a.appendChild(document.createTextNode(item_name)); //   b = document.CreateElement('b'); //   if(item_patrolled == NULL) { //       b.appendChild(document.createTextNode('patruljer')); //   }     if(items[i].getAttribute('patrolled') != '') { var li = document.createElement('li'); li.appendChild(a); //     li.appendChild(b); list.appendChild(li); } }  // Container div var div = document.createElement('div'); div.setAttribute('id', 'p-recentchanges'); div.className = 'portlet'; var heading = document.createElement('h5'); heading.appendChild(document.createTextNode('Unpatrolled')); 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 = 'right'; if(rcp_order == "newer") { aorder = document.createElement('a'); aorder.appendChild(document.createTextNode('Newest first')); aorder.onclick = order_older_box; } else { aorder = document.createElement('a'); aorder.appendChild(document.createTextNode('Oldest first')); aorder.onclick = order_newer_box; } a = document.createElement('a'); a.appendChild(document.createTextNode('Hide')); a.onclick = rcp_disable_box; var pmidle = document.createTextNode(' - '); p.appendChild(aorder); p.appendChild(pmidle); p.appendChild(a); link_div.appendChild(p); // now replace the div var old_div = document.getElementById('p-recentchanges'); 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); } // and do it again in 5 secs setTimeout("rcp_ajax_request", rcp_refresh * 1000); } function rcp_disable_box { rcp_enabled = false; rcp_draw_disabled_box; document.cookie = "rcp_show_box=no; path=/"; } function rcp_enable_box { rcp_enabled = true; document.cookie = "rcp_show_box=yes; path=/"; rcp_ajax_request; } function order_newer_box { rcp_order = "newer"; document.cookie = "rcp_order=newer"; rcp_ajax_request; } function order_older_box { rcp_order = "older"; document.cookie = "rcp_order=older"; rcp_ajax_request; } function rcp_draw_disabled_box { // Container div var link_div = document.createElement('div'); link_div.className = 'pBody'; var div = document.createElement('div'); div.setAttribute('id', 'p-recentchanges'); div.className = 'portlet'; var heading = document.createElement('h5'); heading.appendChild(document.createTextNode('Unpatrolled')); 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('Show this box')); a.onclick = rcp_enable_box; p.appendChild(a); link_div.appendChild(p); // now replace the div var old_div = document.getElementById('p-recentchanges'); 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); } }