MediaWiki:Cycle11.js

From Infinite Nomic Wiki
Revision as of 19:30, 10 January 2022 by miraheze>CodeTriangle (this code should give players the percentage of total shares owned using a tooltip.)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// DOM objects
let st = document.querySelector("#sharetable tbody");

// Only do the rest if the sharetable exists
if (st) {
	// This block gets the total amount of shares for each rule
	var totals = [];
	// Loop over the number of columns in the first row (should never be greater
	// than number of rows in table)
	for (let i = 1; i < st.children[0].children.length; i++) {
		let total = 0;
		for (let row of st.children) {
			// Skip over anything that doesn't look like a number (i.e. headings)
			if (!isNaN(row.children[i].innerText)) {
			  total += parseInt(row.children[i].innerText);
			}
		}
		totals.push(total);
		
		// Code to generate a hover-over indication of percentages.
		for (let row of st.children) {
			if (!isNaN(row.children[i].innerText)) {
				row.children[i].setAttribute(
        			"title",
        			// hacky way to round to two decimal points
        			// because the right way is worse
        			((parseInt(row.children[i].innerText) / total * 10000) | 0) / 100 + "%"
    			);
			}
		}
	}
	
	// Construct a new row to contain totals for each rule.
	let newRow = document.createElement("tr");
	let totalLabel = document.createElement("th");
	totalLabel.innerText = "Total";
	newRow.appendChild(totalLabel);
	for (let total of totals) {
		let newEle = document.createElement("th");
		newEle.innerText = total;
		newEle.style = "text-align: left;";
		newRow.appendChild(newEle);
	}
	// Append this row to the share table.
	st.appendChild(newRow);
}