Jump to content

User:Awesome Aasim/rcwidget.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if (!rcwidget) {
	var rcwidget = {};
	rcwidget.onlyshowores = (typeof onlyshowores !== 'undefined') ? onlyshowores : false;
	rcwidget.orestolerance = (typeof orestolerance !== 'undefined') ? orestolerance : 0.70;
	rcwidget.fetchtime = (typeof rcfetchtime !== 'undefined') ? rcfetchtime : 0.5
	rcwidget.addToSidebar = function(text) {
		if (mw.config.get("skin") == "timeless") {
	        $('<div class="sidebar-chunk" id="rcwidget"><div class="sidebar-inner">' + text + '</div></div>').appendTo("#mw-related-navigation");
	        $("#catlinks-sidebar").appendTo("#mw-related-navigation");
	        $("#other-languages").appendTo("#mw-related-navigation");
		}
        $('<div class="portal" role="navigation" id="rcwidget" aria-labelledby="p-rcwidget-label">' + text + '</div>').prependTo("#mw-panel");
        $('<div role="navigation" class="portlet generated-sidebar" id="rcwidget" aria-labelledby="p-rcwidget-label">' + text + '</div>').prependTo("#sidebar");
        $('<div class="portlet" id="rcwidget">' + text + '</div>').prependTo("#mw_portlets");
        $("#p-navigation").prependTo("#mw-panel");
        $("#p-search").prependTo("#quickbar");
        $('#p-logo').prependTo("#mw-site-navigation");
        $('#p-logo').prependTo("#mw-panel");
        $('#p-logo').prependTo("#sidebar");
        $('#p-logo').prependTo("#mw_portlets");
        $('ul.hlist:first').appendTo('#mw-mf-page-left');
        if (mw.config.get("skin") == "vector" || mw.config.get("skin") == "vector-2022" ) {
        	$("#rcwidget").addClass("mw-portlet mw-portlet-navigation vector-menu vector-menu-portal");
        }
    };
    rcwidget.fetch = function() {
    	if (!document.hasFocus()) {
    		window.setTimeout(rcwidget.fetch, rcwidget.fetchtime * 1000);
    	} else {
	    	var returnHTML = "";
	    	returnHTML += "<ul>";
	    	$.get(mw.config.get("wgScriptPath") + "/api.php", {
				"action": "query",
				"format": "json",
				"list": "recentchanges",
				"rcnamespace": "0|3",
				"rcprop": "title|timestamp|flags|loginfo|oresscores|parsedcomment|user|ids|tags",
				"rcshow": "!bot" + (rcwidget.onlyshowores ? "|oresreview" : ""),
				"rctoponly": true,
				"rclimit": "50",
				"rctype": "edit|new"
			}).done(function(result) {
				if (result.error) {
					console.error(result.error.info);
		    		window.setTimeout(rcwidget.fetch, rcwidget.fetchtime * 1000);
				} else {
					var currDate = new Date();
					for (var change of result.query.recentchanges) {
						var changeDate = new Date(change.timestamp);
						var mildate = new Date(currDate - changeDate);
						var seconds = mildate.getUTCSeconds();
						var minutes = mildate.getUTCMinutes();
						var hours = mildate.getUTCHours();
						if (typeof change.oresscores != 'undefined') {
							if (typeof change.oresscores.damaging != 'undefined') {
								if (typeof change.oresscores.damaging.true != 'undefined') {
									if (rcwidget.onlyshowores && change.oresscores.damaging.true < rcwidget.orestolerance) {
										continue;
									} else {
										returnHTML += "<li>";
										if (change.type == "edit" && change.oresscores.damaging) {
											if (change.oresscores.damaging.true >= rcwidget.orestolerance) {
												returnHTML += "<b>";
											}
										}
										returnHTML += '<a href="/wiki/User_talk:' + change.user + '">' + change.user + '</a> ';
										switch (change.type) {
											case "edit": 
												if (change.tags.includes("mw-undo")) {
													returnHTML += "<a href=\"/wiki/Project:Undo\">undid</a> an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
												} else if (change.tags.includes("mw-rollback")) {
													returnHTML += "<a href=\"/wiki/Project:Rollback\">rolled back</a> edits to <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
												} else if (change.ns == 3) {
													returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
												} else {
													returnHTML += "made an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
												}
												break;
											case "new":
												if (change.ns == 3) {
													returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
												} else {
													returnHTML += "created the page <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
												}
												break;
										}
										if (change.type == "edit" && change.oresscores.damaging) {
											if (change.oresscores.damaging.true >= rcwidget.orestolerance) {
												returnHTML += "</b>";
											}
										}
										returnHTML += "<br><small class=\"rcwidget-date\" data-revtimestamp=\"" + change.timestamp + "\">";
										if (hours > 0) {
											returnHTML += " " + hours + ((hours == 1) ? " hour" : " hours") + " ago";
										} else if (minutes > 0) {
											returnHTML += " " + minutes + ((minutes == 1) ? " minute" : " minutes") + " ago";
										} else if (seconds > 0) {
											returnHTML += " " + seconds + ((seconds == 1) ? " second" : " seconds") + " ago";
										}
										returnHTML += "</small>";
										returnHTML += "</li>";
									}
								} else {
									returnHTML += "<li>";
									returnHTML += '<a href="/wiki/User_talk:' + change.user + '">' + change.user + '</a> ';
									switch (change.type) {
										case "edit": 
											if (change.tags.includes("mw-undo")) {
												returnHTML += "<a href=\"/wiki/Project:Undo\">undid</a> an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
											} else if (change.tags.includes("mw-rollback")) {
												returnHTML += "<a href=\"/wiki/Project:Rollback\">rolled back</a> edits to <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
											} else if (change.ns == 3) {
												returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
											} else {
												returnHTML += "made an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
											}
											break;
										case "new":
											if (change.ns == 3) {
												returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
											} else {
												returnHTML += "created the page <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
											}
											break;
									}
									returnHTML += "<br><small class=\"rcwidget-date\" data-revtimestamp=\"" + change.timestamp + "\">";
									if (hours > 0) {
										returnHTML += " " + hours + ((hours == 1) ? " hour" : " hours") + " ago";
									} else if (minutes > 0) {
										returnHTML += " " + minutes + ((minutes == 1) ? " minute" : " minutes") + " ago";
									} else if (seconds > 0) {
										returnHTML += " " + seconds + ((seconds == 1) ? " second" : " seconds") + " ago";
									}
									returnHTML += "</small>";
									returnHTML += "</li>";
								}
							} else {
								returnHTML += "<li>";
								returnHTML += '<a href="/wiki/User_talk:' + change.user + '">' + change.user + '</a> ';
								switch (change.type) {
									case "edit": 
										if (change.tags.includes("mw-undo")) {
											returnHTML += "<a href=\"/wiki/Project:Undo\">undid</a> an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
										} else if (change.tags.includes("mw-rollback")) {
											returnHTML += "<a href=\"/wiki/Project:Rollback\">rolled back</a> edits to <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
										} else if (change.ns == 3) {
											returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
										} else {
											returnHTML += "made an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
										}
										break;
									case "new":
										if (change.ns == 3) {
											returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
										} else {
											returnHTML += "created the page <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
										}
										break;
								}
								returnHTML += "<br><small class=\"rcwidget-date\" data-revtimestamp=\"" + change.timestamp + "\">";
								if (hours > 0) {
									returnHTML += " " + hours + ((hours == 1) ? " hour" : " hours") + " ago";
								} else if (minutes > 0) {
									returnHTML += " " + minutes + ((minutes == 1) ? " minute" : " minutes") + " ago";
								} else if (seconds > 0) {
									returnHTML += " " + seconds + ((seconds == 1) ? " second" : " seconds") + " ago";
								}
								returnHTML += "</small>";
								returnHTML += "</li>";
							}
						} else {
							returnHTML += "<li>";
							returnHTML += '<a href="/wiki/User_talk:' + change.user + '">' + change.user + '</a> ';
							switch (change.type) {
								case "edit": 
									if (change.tags.includes("mw-undo")) {
										returnHTML += "<a href=\"/wiki/Project:Undo\">undid</a> an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
									} else if (change.tags.includes("mw-rollback")) {
										returnHTML += "<a href=\"/wiki/Project:Rollback\">rolled back</a> edits to <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
									} else if (change.ns == 3) {
										returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
									} else {
										returnHTML += "made an <a href=\"/wiki/Special:Diff/" + change.revid + "\">edit</a> to <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
									}
									break;
								case "new":
									if (change.ns == 3) {
										returnHTML += 'left a message for <a href="/wiki/' + change.title + '">' + change.title.replace("User talk:", "") + "</a>";
									} else {
										returnHTML += "created the page <a href=\"/wiki/" + change.title + "\">" + change.title + '</a>';
									}
									break;
							}
							returnHTML += "<br><small class=\"rcwidget-date\" data-revtimestamp=\"" + change.timestamp + "\">";
							if (hours > 0) {
								returnHTML += " " + hours + ((hours == 1) ? " hour" : " hours") + " ago";
							} else if (minutes > 0) {
								returnHTML += " " + minutes + ((minutes == 1) ? " minute" : " minutes") + " ago";
							} else if (seconds > 0) {
								returnHTML += " " + seconds + ((minutes == 1) ? " second" : " seconds") + " ago";
							}
							returnHTML += "</small>";
							returnHTML += "</li>";
						}
					}
					returnHTML += "<li><a href=\"/wiki/Special:RecentChanges\">View all recent changes</a></li>"
		    		returnHTML += "</ul>";
			    	$("#rcwidget-content").html(returnHTML);
			    	if ($("#rcwidget-content").text().toLowerCase() == "View all recent changes".toLowerCase()) {
			    		$("#rcwidget-content").html("<ul><li>No recent changes found.</li></ul>");
			    	}
				}
		    	window.setTimeout(rcwidget.fetch, rcwidget.fetchtime * 1000);
			}).fail(function(result) {
		    	window.setTimeout(rcwidget.fetch, rcwidget.fetchtime * 1000);
			});
    	}
    };
    $("#rcwidget-content").css("height", "auto");
    $("#rcwidget-content").css("overflow", "auto");
	$(document).ready(function() {
        rcwidget.addToSidebar((mw.config.get('skin') == 'vector' || mw.config.get('skin') == 'vector-2022') ? '<div id="rcwidget-label" lang="en" dir="ltr"><span><a href="/wiki/Special:RecentChanges">Recent changes</a></span></div><div class="mw-portlet-body body pBody" id="rcwidget-content" style="height:250px;overflow:hidden;">Loading...</div>' : '<h3 id="rcwidget-label" lang="en" dir="ltr"><span><a href="/wiki/Special:RecentChanges">Recent changes</a></span></h3><div class="mw-portlet-body body pBody" id="rcwidget-content" style="height:250px;overflow:hidden;">Loading...</div>');
        rcwidget.fetch();
    	window.setInterval(function() {
			var currDate = new Date();
    		$('.rcwidget-date').each(function() {
    			var changeDate = new Date($(this).data('revtimestamp'));
				var mildate = new Date(currDate - changeDate);
				var seconds = mildate.getUTCSeconds();
				var minutes = mildate.getUTCMinutes();
				var hours = mildate.getUTCHours();
				if (hours > 0) {
					$(this).html(hours + ((hours == 1) ? " hour" : " hours") + " ago");
				} else if (minutes > 0) {
					$(this).html(minutes + ((minutes == 1) ? " minute" : " minutes") + " ago");
				} else if (seconds > 0) {
					$(this).html(seconds + ((seconds == 1) ? " second" : " seconds") + " ago");
				}
    		})
    	}, rcwidget.fetchtime);
	});
}