User:Gutza/quickUnwatch.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:Gutza/quickUnwatch. |
// 2008, [[:ro:Utilizator:Gutza]], GPL
quickUnwatch=new Object;
quickUnwatch.linkText="-w";
quickUnwatch.onLoad = function()
{
quickUnwatch.unwatching=false;
if (!quickUnwatch.validPage()) {
return false;
}
var links=document.getElementsByTagName('a');
var prevlink='n/a';
var linkdelta=2;
for(var i=0;i<links.length;i++) {
if (links[i].href.indexOf('action=history')==-1) {
if (links[i].href.indexOf('&diff=')!=-1) {
prevlink='diff';
} else {
prevlink='n/a';
}
continue;
}
if (prevlink=='diff') {
linkdelta=2;
} else {
linkdelta=1;
}
prevlink='hist';
var article_link=links[i-linkdelta];
var article_name=article_link.innerHTML;
var newnode=document.createElement("span");
newnode.innerHTML="(<a href='#' onClick='quickUnwatch.unwatch(\""+escape(article_name)+"\"); return false'>"+quickUnwatch.linkText+"</a>) ";
article_link.parentNode.insertBefore(newnode,article_link);
i++;
}
}
quickUnwatch.unwatch = function(article)
{
if (quickUnwatch.unwatching) {
alert("Already unwatching!");
return false;
}
quickUnwatch.unwatching=true;
if (wgCanonicalSpecialPageName != "Watchlist") {
return false;
}
article=unescape(article);
var atoms=quickUnwatch.getAtoms(article);
if (!atoms) {
return false;
}
atoms.unwatch.innerHTML="…";
sajax_do_call(
"wfAjaxWatch",
[atoms.article.innerHTML, "u"],
quickUnwatch.processResult
);
// if the request isn't done in 10 seconds, allow user to try again
wgAjaxWatch.timeoutID = window.setTimeout(
function() { quickUnwatch.unwatching = false; quickUnwatch.restoreLink(atoms.article.innerHTML); },
10000
);
}
quickUnwatch.processResult = function(request)
{
var response = request.responseText;
if( response.match(/^<u#>/) ) {
window.location="/w/index.php?title=Special:Watchlist&action=purge";
} else {
// Either we got a <err#> error code or it just plain broke.
alert("Unwatch failed, please try again!");
quickUnwatch.unwatching = false;
return;
}
if(quickUnwatch.timeoutID) {
window.clearTimeout(quickUnwatch.timeoutID);
}
}
quickUnwatch.restoreLink = function(article)
{
if (wgCanonicalSpecialPageName != "Watchlist") {
return false;
}
var atoms=quickUnwatch.getAtoms(article);
if (!atoms) {
return false;
}
atoms.unwatch.innerHTML=quickUnwatch.linkText;
}
quickUnwatch.getAtoms = function(article)
{
var links=document.getElementsByTagName('a');
var prevlink='n/a';
var linkdelta=2;
for(var i=0;i<links.length;i++) {
if (links[i].href.indexOf('action=history')==-1) {
if (links[i].href.indexOf('&diff=')!=-1) {
prevlink='diff';
} else {
prevlink='n/a';
}
continue;
}
if (prevlink=='diff') {
linkdelta=2;
} else {
linkdelta=1;
}
var article_link=links[i-linkdelta];
prevlink='hist';
var article_name=article_link.innerHTML;
if (article_name!=article) {
continue;
}
return {
article: links[i-linkdelta],
unwatch: links[i-linkdelta-1]
};
}
return false;
}
quickUnwatch.validPage = function()
{
if (wgCanonicalSpecialPageName != "Watchlist") {
return false;
}
var tmp=new String(window.location);
if (tmp.indexOf("Special:Watchlist/")!=-1) {
return false;
}
return true;
}
hookEvent("load", quickUnwatch.onLoad);