User:Garzfoth/Scripts/WatchlistUpdateNotice.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:Garzfoth/Scripts/WatchlistUpdateNotice. |
// Migrated from https://en.wikipedia.org/wiki/User:Garzfoth/common.js on 2021-05-05
// Notes:
// • For changelogs prior to migration, see https://en.wikipedia.org/enwiki/w/index.php?title=User:Garzfoth/common.js&action=history
// • The way this uses mw.loader to create functions as vars (anonymous functions or such, I forget the term + function declarations within blocks), and especially the way it's initialized, is a bit odd and deserves further investigation.
// • I also need to make sure this isn't interfering with e.g. the watchlist update hooks.
// • TODO: Review, improvements, diff, cleanup, etc.
// Custom watchlist update notice in top toolbar
// Originally from: [[User:APerson/watchlist-notice.js]]
// Note: if it acts odd, mark all pages as visited. in the future, look at showing # of unread articles,
// or checking only recent ones (not worth the time right now)
// (THIS WAS PATCHED FOR THE UI CHANGE ISSUE)
mw.loader.using('mediawiki.util', function () {
var showNotice = function () {
// Stick a "Watchlist update!" notice after the notification count
$("#pt-userpage").before(
$("<li>").append(
$("<a>")
.text( "Watchlist update!" )
.css({
"background-color": "green",
"color": "white",
"border-radius": "2px",
"padding": "0.25em 0.45em 0.2em",
"cursor": "pointer",
"font-weight": "bold",
"transition": "background-color 0.5s"
})
.attr("href", "/wiki/Special:Watchlist")
.mouseover(updateNotice)
)
.attr("id", "watchlist-update-notice")
);
};
var updateNotice = function () {
// Lighten the background color so the user knows we're updating
$("#watchlist-update-notice a").css("background-color", "#7bff7b");
// Update the notice
$.getJSON(
mw.util.wikiScript('api'),
{
format: "json",
action: "query",
list: "watchlist",
wlshow: "unread",
wllimit: 1 // Because we're checking if there are *any* entries
} ).done(function (data) {
if (!data.query) return;
if (data.query.watchlist.length) {
// There are new watchlist diffs to read, so show notice
if(!$("#watchlist-update-notice").length) {
// That is, if it isn't shown already
showNotice();
} else {
// There's already a notice, so change background
$("#watchlist-update-notice a").css("background-color", "green");
}
} else {
// No new watchlist diffs to read, so hide notice
$("#watchlist-update-notice").remove();
}
}
);
};
$(document).ready(function () {
updateNotice();
});
});