MediaWiki:Gadget-edit-first-section.js

Z Wikibooks, biblioteki wolnych podręczników.

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
/*
==== Dodanie linka [edytuj] dla sekcji nagłówkowej ====
; Pomysł: [[:en:User:Pile0nades]]
; Wykonanie: Maciej Jaros [[:pl:User:Nux]]
; Licencja: [http://opensource.org/licenses/gpl-license.php GNU General Public License v2]
*/
// Liczba nagłówków drugiego, trzeciego i czwartego stopnia
// jakie muszą się pojawić w artykule, żeby pojawił się link
window.addEditTopLinkNumHeaders = 2; // dla 2 => dla dwóch i więcej się pojawi

function addEditTopLink() {
	//
	// somehow it gets run twice on some pages - stop that
	if (window.addEditTopLinkDone) {
		return;
	}
	window.addEditTopLinkDone = true;

	//
	// if this is preview page or there is no edit tab, stop
	if (!mw.config.get('wgIsArticle')) {
		return;
	}

	var bodyContent = document.getElementById("bodyContent");
	//
	// if there are no edit-section links then stop
	var spans = bodyContent.getElementsByTagName("span");
	var i;
	for (i = 0; i < spans.length; i++) {
		if (spans[i].className == 'editsection') {
			break;
		}
	}
	if (i>=spans.length) {
		return;
	}

	//
	// additional checkup to stop
	var test = bodyContent.getElementsByTagName("h2").length +
		bodyContent.getElementsByTagName("h3").length +
		bodyContent.getElementsByTagName("h4").length;

	// note that there is always siteSub (h3)
	if (test<=addEditTopLinkNumHeaders) {
		return;
	}


	var caEdit = document.getElementById("ca-edit");
	if (!caEdit) {
		caEdit = document.getElementById("ca-viewsource");
	}
	if (!caEdit) {
		return;
	}
	//
	// get first header element
	var fst_h1 = document.getElementById("content").getElementsByTagName("h1")[0];

	//
	// Creating elements
	//
	// create div
	var div = document.createElement("DIV");
	div.className = 'editsection';

	// create link
	var link = document.createElement("A");
	link.href = caEdit.getElementsByTagName("a")[0].href + '&section=0';
	link.setAttribute('title', 'edytuj sekcję nagłówkową artykułu');
	link.appendChild(document.createTextNode('edytuj'));
	// append link and stuff to div
	div.appendChild(document.createTextNode('['));
	div.appendChild(link);
	div.appendChild(document.createTextNode(']'));

	//
	// Styling
	//
	div.style.cssText = 'padding:.7em 0 0 1.0em; float:right; font-size:x-small;';

	//
	// Insert edit div into h1 and content of h1 to div (it has to be like that so that FF doesn't select the edit link on double click)
	//
	var div_h1 = document.createElement("div");
	// move children
	while(fst_h1.childNodes.length)
	{
		div_h1.appendChild(fst_h1.firstChild)
	}
	fst_h1.appendChild(div);	// edit link
	fst_h1.appendChild(div_h1);	// previous h1 content
}
if ( mw.config.get( 'skin' ) == 'vector' || mw.config.get( 'skin' ) == 'monobook' ) {
	jQuery(document).ready(addEditTopLink);
}