משתמש:מיכאל.צבאן/common.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
/**
 * Easily decode links (%D7%97%D7%95%D7%A7 -> חוק)
 */
document.querySelector("#p-lang").insertAdjacentHTML("afterend",
'<nav id="decodeSection" class="vector-menu-portal portal" role="navigation">' +
	'<h3 class="portal">קידוד קישורים</h3>' +
	'<input id="decodeBox">' +
	'<button id="decodeButton" type="button">קדד</button>' +
	'<p id="decodeResult"></p>' +
'</nav>');

function decode() {
	var encodedText = document.querySelector("#decodeBox").value;
	try {
		document.querySelector("#decodeResult").innerText = decodeURIComponent(encodedText);
	}
	catch (error) {
		document.querySelector("#decodeResult").innerText = "Error: " + error.message;
		console.log("Error with string \""+encodedText+"\":");
		console.error(error);
	}
}

document.querySelector("#decodeButton").onclick = decode;

/**
 * Easily see where cites apper in article body, even if the same cite is referenced more than once
 */
document.querySelector("#decodeSection").insertAdjacentHTML("afterend",
'<nav id="colorCitesSection" class="vector-menu-portal portal" role="navigation">' +
	'<h3 class="portal">צבע הערות שוליים</h3>' +
	'<button id="highlightCites" type="button">' +
	'צבע את 34 הערות השוליים הראשונות' +
	'</button>' +
'</nav>');

var colors = ["red", "yellow", "lightgreen", "lightblue", "fuchsia", "orange", "wheat", "#e6194B", "#3cb44b", "#ffe119", "#f58231", "#911eb4", "#bfef45", "#fabed4", "#469990", "#dcbeff", "#9A6324", "#fffac8", "#aaffc3", "#808000", "#ffd8b1", "#a9a9a9", "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a", "#ffff99", "#b15928"];

function color() {
	var cites = document.querySelectorAll('[id*="cite_note"]'); //get all cite notes
	cites = Array.prototype.slice.call(cites); //convert cites to array

	for (var i = 0; i < cites.length; i++) {
		cites[i] = /\d+/.exec(cites[i].id)[0]; //get cite's indexes
	}

	if (cites.length > colors.length) { //limit cites maximum length to the length of colors; weird things will happen otherwise
		cites = cites.slice(0, colors.length);
	}
	for (i = 0; i < cites.length; i++) { //color
		$('[href*="#cite_note"][href*='+cites[i]+']').css("background-color", colors[i]);
		$('[id*="cite_note"][id*='+cites[i]+']').css("background-color", colors[i]);
	}
}

document.querySelector("#highlightCites").onclick = color;

/**
 * Custom styles
 */
function addCSS(s) {
	document.head.appendChild(document.createElement("style")).innerHTML = s;
}
var style =
'#decodeBox {' +
	'width:80%;' +
	'margin: 0.5em;' +
'}' +
'#decodeButton {' +
	'width: 50%;' +
	'margin-right: 0.5em;' +
'}' +
'#decodeResult {' +
	'word-wrap: break-word;' +
'}' +
'#highlightCites {' +
	'margin: 0.5em;' +
	'font-size: 0.75em;' +
	'padding-top: 0.25em;' +
	'padding-bottom: 0.25em;' +
'}';
addCSS(style);

/**
 * Script imports
 */
mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Jon_Harald_Søby/diffedit.js&action=raw&ctype=text/javascript');
importUserScript(76);