User:Oshwah/AbuseFilterPatrol.js

var afp_http; var afp_enabled; var afp_num_pages; var afp_refresh;

/* initalise */ function afp_init {

// allow user settings through if(afp_enabled === undefined) { afp_enabled = false; } if(afp_num_pages === undefined) { afp_num_pages = 10; } if(afp_refresh === undefined) { afp_refresh = 5; }

// A few limits to be nice to the servers if (afp_num_pages > 50) { afp_num_pages = 50; } if (afp_num_pages < 1) { afp_num_pages = 1; } if (afp_refresh < 2) { afp_refresh = 2; }

// get our cookie if (document.cookie.length > 0) { var c_start = document.cookie.indexOf("afp_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") { afp_enabled = true; } else { afp_enabled = false; }   }  }

// Either make a request or show nothing if (afp_enabled === true) { afp_ajax_request; } else { afp_draw_disabled_box; } }

/* init ajax */ function afp_create_request { try { afp_http = new XMLHttpRequest;

} catch (e) { try { afp_http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { afp_http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; }   }  }

afp_http.onreadystatechange = function { if(afp_http.readyState == 4) afp_ajax_response; };

return true; }

/* make a request */ function afp_ajax_request { // check we are enabled if (afp_enabled === false) return;

// firstly, inform the user var afp_cur_box = document.getElementById('p-abusefilterpatrolscript'); if (afp_cur_box !== null) { afp_cur_box.firstChild.firstChild.data = 'Abuse logs (...)'; }

if (afp_create_request === false) { if (afp_cur_box !== null) { afp_cur_box.firstChild.firstChild.data = 'Abuse logs (Update FAILED!)'; } else { alert ("Error with AJAX object creation."); } }

// Then make the request afp_http.open("GET", "/w/api.php?action=query&format=xml&list=abuselog&aflfilter=51|53&afllimit=" + afp_num_pages, true); afp_http.send(null); }

/* we have received a response */ function afp_ajax_response {

var afp_items = afp_http.responseXML.getElementsByTagName('item');

// create the div that holds all the newpage links var afp_link_div = document.createElement('div'); afp_link_div.className = 'pBody'; var afp_list = document.createElement('ul'); afp_link_div.appendChild(afp_list);

// populate the list with 10 links. for (var i = 0; i < afp_items.length; i++) { var afp_ip_pattern = /^((\d{1,3}\.){3}\d{1,3}|([\da-f]{0,4}:){1,7}[\da-f]{0,4})(\/\d{1,3})?$/i; var afp_item_name = afp_items[i].getAttribute('user'); var enc_afp_item_name = afp_items[i].getAttribute('user'); if (!afp_ip_pattern.test(afp_item_name)) { enc_afp_item_name = encodeURIComponent(afp_item_name); }   var afp_item_url = 'https://en.wikipedia.org/wiki/Special:Contributions/' + enc_afp_item_name; a = document.createElement('a'); a.style.fontSize = 'small'; a.setAttribute('href', afp_item_url); a.appendChild(document.createTextNode(afp_item_name));

var li = document.createElement('li'); li.appendChild(a); afp_list.appendChild(li); }

// Container div var afp_div = document.createElement('div'); afp_div.setAttribute('id', 'p-abusefilterpatrolscript'); afp_div.className = 'portal'; var afp_heading = document.createElement('h6'); afp_heading.appendChild(document.createTextNode('Abuse logs')); afp_div.appendChild(afp_heading); afp_div.appendChild(afp_link_div); window.console.log("[ABUSE-FILTER-PATROL]: Object created!");

// 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('disable this box')); a.onclick = afp_disable_box; p.appendChild(a); afp_link_div.appendChild(p);

// now replace the div var afp_old_div = document.getElementById('p-abusefilterpatrolscript'); var afp_side_col = document.getElementById('mw-panel'); if (afp_old_div !== null) { afp_side_col.replaceChild(afp_div, afp_old_div); window.console.log("[ABUSE-FILTER-PATROL]: Object updated!"); } else { //var afp_node = document.getElementById('p-newuserpatrolscript'); //INSERTION OF ELEMENT AFTER p-newuserpatrolscript var afp_node = document.getElementById('p-tb'); afp_side_col.insertBefore(afp_div, afp_node.nextSibling); //Effectively, "insert after" window.console.log("[ABUSE-FILTER-PATROL]: Object inserted for the first time!"); }

// and do it again in 5 secs setTimeout(afp_ajax_request, afp_refresh * 1000); }

function afp_disable_box { afp_enabled = false; afp_draw_disabled_box; document.cookie = "afp_show_box=no; path=/"; }

function afp_enable_box { afp_enabled = true; document.cookie = "afp_show_box=yes; path=/"; afp_ajax_request; }

function afp_draw_disabled_box { // Container div var afp_link_div = document.createElement('div'); afp_link_div.className = 'pBody'; var afp_div = document.createElement('div'); afp_div.setAttribute('id', 'p-abusefilterpatrolscript'); afp_div.className = 'portal'; var afp_heading = document.createElement('h6'); afp_heading.appendChild(document.createTextNode('Abuse logs (Disabled)')); afp_div.appendChild(afp_heading); afp_div.appendChild(afp_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('enable this box')); a.onclick = afp_enable_box; p.appendChild(a); afp_link_div.appendChild(p);

// now replace the div var afp_old_div = document.getElementById('p-abusefilterpatrolscript'); var afp_side_col = document.getElementById('mw-panel'); if (afp_old_div !== null) { afp_side_col.replaceChild(afp_div, afp_old_div); } else { //var afp_node = document.getElementById('p-newuserpatrolscript'); //INSERTION OF ELEMENT AFTER p-newuserpatrolscript var afp_node = document.getElementById('p-tb'); afp_side_col.insertBefore(afp_div, afp_node.nextSibling); //Effectively, "insert after" } }

addOnloadHook( afp_init );