Jump to content

User:JJPMaster/Scripts/AfC time logger.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by JJPMaster (talk | contribs) at 03:40, 24 May 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
(() => {
	let ns = mw.config.get("wgCanonicalNamespace");
	let name = mw.config.get("wgPageName");
	var startTime = new Date().getTime();
	var spent = 0;
	var wasAccepted = false;
	
	function getTime() {
		var time = new Date().getTime();
		spent = (time - startTime) / 1000;
		return spent;
	}
	
	function postToLog(title, time, action) {
		var params = {
			action: "query",
			titles: "User:" + mw.config.get("wgUserName") + "/AfC time log",
			format: "json"
		},
		api = new mw.Api();
		api.get(params).done((data) => {
			if(typeof data.query.pages["-1"].missing !== undefined) {
				params = {
					action: "edit",
					title: "User:" + mw.config.get("wgUserName") + "/AfC time log",
					text: "This page measures the amount of time it takes for me to review articles at AfC. This information is obtained using a [[User:JJPMaster/afc.js|user script]].\r\n* [[" + title + "]] - " + action + " after " + time + " seconds",
					summary: "Added " + title + " to AfC time log using [[User:JJPMaster/afc.js]]",
					format: "json"
				},
				api.postWithToken("csrf", params).done((data) => {
					console.log(data);
				});
			}
			else {
				params = {
					action: "edit",
					title: "User:" + mw.config.get("wgUserName") + "/AfC time log",
					appendtext: "\r\n* [[" + title + "]] - " + action + " after " + time + " seconds",
					summary: "Added " + title + " to AfC time log using [[User:JJPMaster/afc.js]]",
					format: "json"
				},
				api.postWithToken("csrf", params).done((data) => {
					console.log(data);
				});
			}
		});
	}
	
	$(document).ready(() => {
		if(ns == "Draft") {
			console.log("Draft detected, start time is " + startTime);
			$("#bodyContent").on("click", "#afchSubmitForm", () => {
				console.log("Spent " + getTime() + " before reviewing " + name); 
				if($("#afchSubmitForm").hasClass("accept")) {
					postToLog(name, getTime(), "Accepted");
				}
				else postToLog(name, getTime(), "Declined");
			});
		}
		else console.log("Not a draft");
	});
})();