User:Henrik/js/dyk-notifier.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Henrik/js/dyk-notifier. |
// <nowiki>
// <pre>
/*
To install, add
// [[User:Henrik/dyk-notifier]]
importScript('User:Henrik/js/dyk-notifier.js');
to your monobook.js (located at User:YOURUSERNAME/monobook.js)
*/
function mylog(text) {
var p2 = document.createElement("p");
p2.appendChild(
document.createTextNode(text)
);
p2.setAttribute("class", "labrat");
var bottle = document.getElementById("log");
bottle.appendChild(p2);
}
function mydate()
{
var months = ["January","February","March","April",
"May","June","July","August",
"September","October","November","December"];
var currentTime = new Date();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
return ""+day+" "+months[month]+"|"+year;
}
function clear(){
var parent = document.getElementById("log");
while ( parent.hasChildNodes() ) { parent.removeChild(parent.firstChild); }
}
function makeXhr() {
xhr = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
xhr = new XMLHttpRequest();
if (xhr.overrideMimeType) {
// set type accordingly to anticipated content type
//xhr.overrideMimeType('text/xml');
xhr.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xhr) {
alert('Cannot create XMLHTTP instance');
return false;
}
return xhr;
}
function makePOSTRequest(url, parameters) {
//xhr.onreadystatechange = alertContents;
var xhr = makeXhr();
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", parameters.length);
xhr.setRequestHeader("Connection", "close");
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200) {
mylog("successful!");
}
}
}
xhr.open('POST', url, false);
xhr.send(parameters);
}
function postTalk(user,msg,summary)
{
var xhr = makeXhr();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4)
{
if(xhr.status == 200) {
var doc = xhr.responseXML;
var form = xhr.responseXML.getElementById( 'editform' );
var pstr = 'wpEditToken='+encodeURIComponent(form.wpEditToken.value) + "&"+
'wpAutoSummary='+encodeURIComponent(form.wpAutoSummary.value) + "&"+
'wpStarttime='+encodeURIComponent(form.wpStarttime.value) + "&"+
'wpEdittime='+encodeURIComponent( form.wpEdittime.value) + "&"+
//'wpSave='+encodeURIComponent("Save page") + "&" +
'wpSection=new&'+
'wpSummary='+encodeURIComponent("foo") + "&" +
'wpTextbox1='+encodeURIComponent("foo");
alert(form.action); alert(pstr);
makePOSTRequest(form.action,pstr);
}
}
};
xhr.overrideMimeType('text/xml');
xhr.open('GET', "http://en.wikipedia.org/w/index.php?title="+user+"&action=edit", false);
xhr.send(null);
}
function xpathfind(findPattern) {
return document.evaluate( findPattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
}
function dykNotifyAll(autosave) {
allLinks = xpathfind("//div[@id=\"credits\"]/ul/li");
for(i=0;i<allLinks.snapshotLength;i++) {
var idx = i + 1;
articleLink = xpathfind("//div[@id=\"credits\"]/ul/li["+idx+"]//a[1]");
var title = articleLink.snapshotItem(0).textContent;
mylog(title);
var atalkurl = articleLink.snapshotItem(0).href.replace("/wiki/","/wiki/Talk:")+"?action=edit&xamfind="+escape("({{.*}})?(.*\n=.*=)?")+"&amreplace="+escape("$1\n{{dyktalk|"+mydate()+"}}\n$2")+"&amsummary=Dyktalk"+autosave;
postTalk("User_talk:Henrik/sandbox/test1","hello ~~"+"~~","hi");
//mylog(" -- talk page: " + atalkurl);
resultLinks = xpathfind("//div[@id=\"credits\"]/ul/li["+idx+"]//a[starts-with(@title,'User talk:')]");
if ( (res = resultLinks.snapshotItem(0) ) != null ){
authormsg = escape("{"+"{subst:UpdatedDYK|"+mydate()+"|"+title+"}} --~~"+"~~");
authorurl = res.href+"?action=edit§ion=new&amsummary="+escape(title)+"&amaddbefore="+authormsg+autosave;
//window.open(authorurl,"authortalk"+i);
mylog(" -- author: " + res.textContent);
}
if ( (res = resultLinks.snapshotItem(1) ) != null ){
authormsg = escape("{"+"{subst:UpdatedDYKNom|"+mydate()+"|"+title+"}} --~~"+"~~");
authorurl = res.href+"?action=edit§ion=new&amsummary="+escape(title)+"&amaddbefore="+authormsg+autosave;
//window.open(authorurl,"notifiertalk"+i);
mylog(" -- nominator: " + res.textContent);
}
}
}
function insertDykButtons() {
if (mw.config.get('wgPageName') != "User:Henrik/sandbox" &&
mw.config.get('wgPageName') != "Template:Did_you_know/Next_update" &&
mw.config.get('wgPageName') != "Template:Did_you_know/Next_update/Clear")
return;
d=document.createElement('div')
var b=document.createElement('input');
b.type='button';
b.value='Notify contributors';
b.onclick=function(){
dykNotifyAll("");
}
d.appendChild(b);
var b=document.createElement('input');
b.type='button';
b.value='Notify contributors (autosave)';
b.onclick=function(){
dykNotifyAll("&amautosave=1");
}
d.appendChild(b);
document.getElementById("credits").appendChild(d);
}
$(insertDykButtons);
// </nowiki>