Created
March 4, 2014 03:57
-
-
Save seaside98/9340023 to your computer and use it in GitHub Desktop.
For use on Wikia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<syntaxhighlight lang="javascript"> | |
/* User Talk Notifications | |
* Code by Seaside98 */ | |
//Variables | |
var talkWikis = window.talkWikis || []; | |
var pageName = window.talkName || 'Talk'; | |
var talkNames = []; | |
var enUserName = encodeURIComponent(wgUserName); | |
//Add WikiaNotification element if not present | |
if (!$('#WikiaNotifications').length) { | |
$('#WikiaBarWrapper').before('<ul id="WikiaNotifications" class="WikiaNotifications"></ul>'); | |
} | |
//Add option in edit menu | |
if (wgPageName == 'User:'+wgUserName+'/'+pageName) { | |
$('#WikiaPageHeader .WikiaMenuElement').append('<li class="userTalkNotify"><a href="http://community.wikia.com/wiki/User:'+enUserName+'/global.js" onclick="utnModify(\'New\')">Notify</a></li>'); | |
} | |
//Uppercase the first letter of each wiki's name | |
for (var a in talkWikis) { | |
talkNames[a] = talkWikis[a].replace(/\b./g, function(m){ return m.toUpperCase(); }); | |
utnCheckWiki(a); | |
} | |
//Check for new messages | |
function utnCheckWiki(z) { | |
//If we are on the talkpage or the cookie isn't set, then update the timestamp | |
if (wgServer+wgPageName == 'http://'+talkWikis[z]+'.wikia.comUser:'+wgUserName+'/'+pageName) { | |
$('.userTalkNotify').replaceWith('<li><a href="http://community.wikia.com/wiki/User:'+enUserName+'/global.js" onclick="utnModify(\'Old\')">Don\'t Notify</a></li>'); | |
utnSetStamp(z); | |
} else if (!$.cookie('talk'+talkWikis[z])){ | |
utnSetStamp(z); | |
} else { | |
//Load the edits since the last timestamp, organize the data, and then display it to the user | |
$.ajax({ | |
url: "http://"+talkWikis[z]+".wikia.com/api.php?action=query&prop=revisions&titles=User:"+enUserName+"/"+pageName+"&rvprop=user&rvend="+$.cookie('talk'+talkWikis[z])+"&format=json", | |
dataType: "jsonp", | |
cache: false | |
}).done(function(data) { | |
var userAry = []; | |
var userList; | |
var plural; | |
var p; for (p in data.query.pages) { break; } | |
if ("revisions" in data.query.pages[p]) { | |
for (var i=0; i<data.query.pages[p].revisions.length; i++) { | |
userAry[i]=data.query.pages[p].revisions[i].user; | |
} | |
$.unique(userAry); | |
if (userAry.length == 1) { | |
userList = userAry[0]; | |
plural = 'a new message'; | |
} else { | |
var userFirst = userAry.pop(); | |
userList = userAry.join(", ")+' and '+userFirst; | |
plural = 'new messages'; | |
} | |
if (wgServer == 'http://'+talkWikis[z]+'.wikia.com') { | |
$('#WikiaNotifications').append('<li><div data-type="1"><a class="sprite close-notification talkClose" onclick="utnSetStamp('+z+')" target="_blank"></a>You have <a href="http://'+talkWikis[z]+'.wikia.com/wiki/User:'+enUserName+'/'+pageName+'">'+plural+'</a> from '+userList+'.</div></li>'); | |
} else { | |
$('#WikiaNotifications').append('<li><div data-type="1"><a class="sprite close-notification talkClose" onclick="utnSetStamp('+z+')" target="_blank"></a>You have '+plural+' on the <a href="http://'+talkWikis[z]+'.wikia.com/wiki/User:'+enUserName+'/'+pageName+'">'+talkNames[z]+' Wiki</a> from '+userList+'.</div></li>'); | |
} | |
$('.talkClose').click(function() { | |
$(this).parent().remove(); | |
}); | |
} | |
}); | |
} | |
} | |
//Update the timestamp cookie | |
function utnSetStamp(z) { | |
//Load the last revision to get the timestamp instead of relying on the user's computer for the correct time | |
$.ajax({ | |
url: "http://"+talkWikis[z]+".wikia.com/api.php?action=query&prop=revisions&titles=User:"+enUserName+"/"+pageName+"&rvprop=timestamp&rvlimit=1&format=json", | |
dataType: "jsonp", | |
cache: false | |
}).done(function(data) { | |
var p; for (p in data.query.pages) { break; } | |
//Add 1 second to the timestamp of the last edit and store it as a cookie | |
if ("revisions" in data.query.pages[p]) { | |
var curTime = (parseInt(data.query.pages[p].revisions[0].timestamp.replace(/[^0-9]/g, ""))+1).toString() | |
$.cookie('talk'+talkWikis[z], curTime, {expires: 5, path: '/', domain: '.wikia.com'}); | |
console.log('Stamp: '+curTime); | |
//If the page doesn't exist, prompt the user to create it | |
} else if (wgServer+wgPageName != 'http://'+talkWikis[z]+'.wikia.comUser:'+wgUserName+'/'+pageName) { | |
$('#WikiaNotifications').append('<li><div data-type="1">Please create the page <a href="http://'+talkWikis[z]+'.wikia.com/wiki/User:'+enUserName+'/'+pageName+'" target="_blank">User:'+wgUserName+'/'+pageName+'</a> on the '+talkNames[z]+' Wiki.</div></li>'); | |
} | |
}); | |
} | |
//Set cookie on click | |
function utnModify(z) { | |
$.cookie('talkPage'+z, wgServer.substr(7,wgServer.length-17), {path: '/', domain: '.wikia.com'}); | |
} | |
//Start process of editing global.js (Using this instead of cookies for cross browser/device compatibility.) | |
if (wgServer+wgPageName == 'http://community.wikia.comUser:'+wgUserName+'/global.js' && ($.cookie('talkPageNew') || $.cookie('talkPageOld'))) { | |
var text = $('.javascript').text(); | |
utnAddNewWiki($.cookie('talkPageNew'), $.cookie('talkPageOld')); | |
//Edit the text | |
function utnAddNewWiki(tNew, tOld) { | |
var wName; | |
text = text.replace(/\u00A0/g," "); //Annoying space character | |
if (tNew) { | |
$.cookie('talkPageNew', null, {path: '/', domain: '.wikia.com'}); | |
if (text.match(/window\.talkWikis.*?=.*?\[.*?\]/)) { | |
text = text.replace(/window\.talkWikis.*?=.*?\[([^\]]*)/,"window.talkWikis = ['"+tNew+"',$1").replace(/window\.talkWikis.*?\[(.*?),(?=\])/,"window.talkWikis = [$1"); //Add new and check commas | |
} else { | |
text = "window.talkWikis = ['"+tNew+"'];\n"+text; | |
} | |
wName = tNew; | |
} else { | |
$.cookie('talkPageOld', null, {path: '/', domain: '.wikia.com'}); | |
var re = new RegExp("window\\.talkWikis[\\s\\S]*?\\[(.*?)(('"+tOld+"',)|(,'"+tOld+"')|('"+tOld+"'))"); | |
text = text.replace(re,"window.talkWikis = [$1"); | |
wName = tOld; | |
} | |
$('body').prepend('<div style="width:100%;height:100%;background:white;z-index:100000000;position:absolute;opacity:.8;"><div class="utnOver" style="position:fixed;top:50%;left:50%;font-size:50px;font-weight:bold;margin-top:-40px;margin-left:-190px;color:white;background:black;border-radius:30px;padding:20px;width:360px;height:60px;text-align:center;">Editing Array...</div></div>'); | |
utnEditPage(wName); | |
} | |
//Edit the actual page | |
function utnEditPage(z) { | |
$.ajax({ | |
url: "http://community.wikia.com/api.php?action=query&prop=info&intoken=edit&titles=User:"+enUserName+"/global.js&format=json", | |
dataType: "json" | |
}).done(function(data) { | |
var p; for (p in data.query.pages) { break; } | |
$.ajax({ | |
url: "http://community.wikia.com/api.php?action=edit&minor=true&title=User:"+enUserName+"/global.js&token="+encodeURIComponent(data.query.pages[p].edittoken), | |
data: {'text': text}, | |
dataType: "json", | |
type: 'POST' | |
}).fail(function() { //It works. Even though the computer doesn't know it. | |
$('.utnOver').text('Redirecting...'); | |
window.open("http://"+z+".wikia.com/wiki/User:"+enUserName+"/"+pageName,"_self"); | |
}); | |
}); | |
} | |
} | |
//</syntaxhighlight> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment