Wikipedia:WikiProject User scripts/Scripts/Unwatch: Difference between revisions

Content deleted Content added
doesn't work :-( — replace with version that does
m Remove legacy globals per phab:T72470 (via WP:JWB)
 
(20 intermediate revisions by 6 users not shown)
Line 1:
/* This script adds an "unwatch" link to each entry in your watchlist. This version works regardless of whether you have the "Enhanced recent changes" option selected in your [[Special:Preferences|user preferences]]. By default the links take you back to [[Special:Watchlist|your watchlist]]. If you'd prefer them to take you to a "Removed from watchlist" page like the unwatch links at the top of the watched pages, uncomment the second line in the function.
// This script adds an "unwatch" link to each entry in your watchlist. <pre><nowiki>
 
Other versions:
* [[User:Ilmari_Karonen/unwatch.js]] (''legacy script'')
* [[User:Quarl/watchlist.js]]
* [[user:js/watchlist]]
* [[Bugzilla:424]] (if implemented)
 
*/
 
addOnloadHook(function () {
if (window.___location.href.indexOf("Special:Watchlist") == -1) return;
 
// Check if we're on the watchlist
// this is all tag soup, we just have to wade through it :-(
if (!mw.config.get('wgCanonicalSpecialPageName') || mw.config.get('wgCanonicalSpecialPageName') != "Watchlist") return;
// look for h4 followed by div
if (!document.forms[0] || !document.forms[0].namespace) return;
var headings = document.getElementById('content').getElementsByTagName('h4');
 
// Unwatch links go back to watchlist with "Removing requested items from watchlist..." message
for (var i = 0; i < headings.length; i++) {
var query_prefix = "title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=submit&remove=1&id[]=";
var div = headings[i].nextSibling;
while (div && div.nodeType == 3) div = div.nextSibling; // skip text nodes
if (!div || div.tagName.toLowerCase() != 'div') continue;
 
// ...or...
var pagename = "";
// Unwatch links go to "Removed from watchlist" page
for (var node = div.firstChild; node; node = node.nextSibling) {
//var query_prefix = "action=unwatch&title=";
 
// grabget the titlelist of the first linkall onlinks eachin linecontent:
var headingslinks = document.getElementById('content').getElementsByTagName('h4a');
if (!pagename && node.tagName && node.tagName.toLowerCase() == 'a')
pagename = node.title;
if (pagename && node.tagName && node.tagName.toLowerCase() == 'br')
pagename = "";
 
// make a static copy of the nodelist and lose the original for speed
// add the (unwatch) link after diff and hist (look for distinctive text node)
// while we're at it, prune the uninteresting links from the list
if (pagename && node.nodeType == 3 && node.nodeValue == ") . . ") {
var linksCopy = new Array ();
var unwatch = document.createElement('a');
for (var i = 0; i < headingslinks.length; i++) {
unwatch.href = "/w/index.php?title=Special:Watchlist&action=submit&remove=1&id[]="+encodeURIComponent(pagename);
if (/[?&]action=history([&#]|$)/.test(links[i].href)) linksCopy.push(links[i]);
unwatch.title = "Unwatch "+pagename;
unwatch.appendChild(document.createTextNode("unwatch"));
div.insertBefore(document.createTextNode("; "), node);
div.insertBefore(unwatch, node);
}
}
// XXX: might want to double check that pagename is null here, but I'll ignore it for now
}
links = linksCopy;
});
 
for (var i = 0; i < links.length; i++) {
// </nowiki></pre>
// create unwatch link and append it after history link
var unwatch = document.createElement('a');
unwatch.href = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?" + query_prefix + encodeURIComponent(links[i].title);
unwatch.title = "Unwatch "+pagenamelinks[i].title;
unwatch.appendChild(document.createTextNode("unwatch"));
divlinks[i].parentNode.insertBefore(unwatch, nodelinks[i].nextSibling);
 
// insert a delimiter between the two links
var divdelim = headingslinks[i].nextSiblingpreviousSibling;
delimText = (delim.nodeType == 3 ? delim.nodeValue : ""); // kluge to handle case where "diff" is unlinked
delim = document.createTextNode(delimText.replace(/^.*diff/, ""));
links[i].parentNode.insertBefore(delim, unwatch);
}
});