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

Content deleted Content added
provide two variations
m Remove legacy globals per phab:T72470 (via WP:JWB)
 
(19 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.
 
Other versions:
There are two variations, depending on whether the "Enhanced recent changes" option is selected in your user preferences.
* [[User:Ilmari_Karonen/unwatch.js]] (''legacy script'')
* [[User:Quarl/watchlist.js]]
* [[user:js/watchlist]]
* [[Bugzilla:424]] (if implemented)
 
*/
__TOC__
 
== No enhanced recent changes ==
 
<pre><nowiki>
// Unwatch link: No enhanced recent changes
// Add Unwatch to watch list
// from [[User:Matthewmayer/monobook.js]]
addOnloadHook(function () {
if (window.___location.href.indexOf("Special:Watchlist")!=-1) {
//correct page, so retrieve watchlist items
var items=document.getElementById('bodyContent').getElementsByTagName('li');
for (var i=0;i<items.length;i++) {
//what is the title of this page?
pagename=items[i].getElementsByTagName('a')[0].title;
//create 'unwatch' element
unwatchelt=document.createElement('a');
unwatchelt.setAttribute('href','/w/index.php?title='+pagename+'&action=unwatch');
unwatchelt.setAttribute('title','Unwatch '+pagename);
unwatchelt.appendChild(document.createTextNode('(unwatch)'));
//add the 'unwatch' element
items[i].appendChild(unwatchelt);
}
}
}
);
//</nowiki></pre>
 
// Check if we're on the watchlist
== Enhanced recent changes ==
if (!mw.config.get('wgCanonicalSpecialPageName') || mw.config.get('wgCanonicalSpecialPageName') != "Watchlist") return;
if (!document.forms[0] || !document.forms[0].namespace) return;
 
// Unwatch links go back to watchlist with "Removing requested items from watchlist..." message
<pre><nowiki>
var query_prefix = "title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=submit&remove=1&id[]=";
// Unwatch link: Enhanced recent changes
addOnloadHook(function () {
if (window.___location.href.indexOf("Special:Watchlist") == -1) return;
 
// ...or...
// this is all tag soup, we just have to wade through it :-(
// Unwatch links go to "Removed from watchlist" page
// look for h4 followed by div
//var query_prefix = "action=unwatch&title=";
var headings = document.getElementById('content').getElementsByTagName('h4');
 
// get list of all links in content:
for (var i = 0; i < headings.length; i++) {
var itemslinks = document.getElementById('bodyContentcontent').getElementsByTagName('lia');
var div = headings[i].nextSibling;
while (div && div.nodeType == 3) div = div.nextSibling; // skip text nodes
if (!div || div.tagName.toLowerCase() != 'div') continue;
 
// make a static copy of the nodelist and lose the original for speed
var pagename = "";
// while we're at it, prune the uninteresting links from the list
for (var node = div.firstChild; node; node = node.nextSibling) {
var linksCopy = new Array ();
for (var i = 0; i <items links.length; i++) {
if (/[?&]action=history([&#]|$)/.test(links[i].href)) linksCopy.push(links[i]);
}
links = linksCopy;
 
for (var i = 0; i < headingslinks.length; i++) {
// grab the title of the first link on each line
// create unwatch link and append it after history link
if (!pagename && node.tagName && node.tagName.toLowerCase() == 'a')
var pagenameunwatch = nodedocument.titlecreateElement('a');
unwatch.href = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?" + query_prefix + encodeURIComponent(links[i].title);
if (pagename && node.tagName && node.tagName.toLowerCase() == 'br')
pagenameunwatch.title = "Unwatch "+links[i].title;
unwatcheltunwatch.appendChild(document.createTextNode('("unwatch)'"));
divlinks[i].parentNode.insertBefore(unwatch, nodelinks[i].nextSibling);
 
// insert a delimiter between the two links
// add the (unwatch) link after diff and hist (look for distinctive text node)
var divdelim = headingslinks[i].nextSiblingpreviousSibling;
if (pagename && node.nodeType == 3 && node.nodeValue == ") . . ") {
delimText = (delim.nodeType == 3 ? delim.nodeValue : ""); // kluge to handle case where "diff" is unlinked
var unwatch = document.createElement('a');
delim = document.createTextNode(delimText.replace(/^.*diff/, ""));
unwatch.href = "/w/index.php?title=Special:Watchlist&action=submit&remove=1&id[]="+encodeURIComponent(pagename);
links[i].parentNode.insertBefore(delim, 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
}
});
 
// </nowiki></pre>