User:Where next Columbus?/WatchlistWatcher.js

/* Based on User:TheJosh's http://en.wikipedia.org/w/index.php?title=User:TheJosh/Scripts/NewPagePatrol.js&oldid=189427828 */ var wlw_http; var wlw_enabled; var wlw_num_pages; var wlw_refresh; addOnloadHook( wlw_init ); /* initalise */ function wlw_init { // allow user settings through if(wlw_enabled == null) { wlw_enabled = false; } if(wlw_num_pages == null) { wlw_num_pages = 10; } if(wlw_refresh == null) { wlw_refresh = 60; } // A few limits to be nice to the servers if (wlw_num_pages > 50) { wlw_num_pages = 50; } if (wlw_num_pages < 1) { wlw_num_pages = 1; } if (wlw_refresh < 2) { wlw_refresh = 2; } // get our cookie if (document.cookie.length > 0) { var c_start = document.cookie.indexOf("wlw_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") { wlw_enabled = true; } else { wlw_enabled = false; }   }   }  // Either make a request or show nothing if (wlw_enabled == true) { wlw_ajax_request; } else { wlw_draw_disabled_box; } } /* init ajax */ function wlw_create_request { try { wlw_http = new XMLHttpRequest; } catch (e) { try { wlw_http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { wlw_http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; }   }  }  wlw_http.onreadystatechange = function { if(wlw_http.readyState == 4) wlw_ajax_response; } return true; } /* make a request */ function wlw_ajax_request { // check we are enabled if (wlw_enabled == false) return; // firstly, inform the user var cur_box = document.getElementById('p-newpages'); if (cur_box != null) { cur_box.firstChild.firstChild.data = 'Watchlist (updating)'; } if (wlw_create_request  == false) { if (cur_box != null) { cur_box.firstChild.firstChild.data = 'Watchlist (update failed)'; } else { alert ("There seems to be a problem using the WatchlistWatcher script. I can't make AJAX objects, so I'm just going to complain. God Bless!"); } }  // Then make the request wlw_http.open("GET", "/w/api.php?action=query&format=xml&list=watchlist&wlnamespace=0&wllimit=" + wlw_num_pages, true); wlw_http.send(null); } /* we have received a response */ function wlw_ajax_response { var items = wlw_http.responseXML.getElementsByTagName('query').firstChild; //var items = prefilter.getNamedItem('watchlist'); // 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('revid'); item_name = item_name.replace(/&/, "%26"); var item_url = 'http://en.wikipedia.org/w/index.php?title=' + item_name + '&oldid=' + 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); list.appendChild(li); } // Container div var div = document.createElement('div'); div.setAttribute('id', 'p-watchlist'); div.className = 'portlet'; var heading = document.createElement('h5'); heading.appendChild(document.createTextNode('Watchlist')); 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'; a = document.createElement('a'); a.appendChild(document.createTextNode('disable this box')); a.onclick = wlw_disable_box; p.appendChild(a); link_div.appendChild(p); // now replace the div var old_div = document.getElementById('p-watchlist'); 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("wlw_ajax_request", wlw_refresh * 1000); } function wlw_disable_box { wlw_enabled = false; wlw_draw_disabled_box; document.cookie = "wlw_show_box=no; path=/"; } function wlw_enable_box { wlw_enabled = true; document.cookie = "wlw_show_box=yes; path=/"; wlw_ajax_request; } function wlw_draw_disabled_box { // Container div var link_div = document.createElement('div'); link_div.className = 'pBody'; var div = document.createElement('div'); div.setAttribute('id', 'p-watchlist'); div.className = 'portlet'; var heading = document.createElement('h5'); heading.appendChild(document.createTextNode('Watchlist')); 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('enable this box')); a.onclick = wlw_enable_box; p.appendChild(a); link_div.appendChild(p); // now replace the div var old_div = document.getElementById('p-watchlist'); 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); } }