User:Erutuon/scripts/editableHeading.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

/*
	Makes page name in top header editable, and usable as a search box.
	This makes it easier to copy the page title, and allows you to easily
	search for a modification of the page title.
	Based on [[User:Dixtosa/editAndGo.js]], with modifications to hopefully
	allow it to be used when [[User:Erutuon/scripts/editTop.js]] is enabled.
	Buggy on certain pages.
*/

mw.loader.using("mediawiki.util", function () {

var pageName = mw.config.values.wgPageName;
/*	Take pagename, then
		replace underscores with spaces,
		escape periods and slashes,
		make sure pagename is not before &,
			which would indicate it is in a href attribute.		*/
if ( pageName && pageName.match("&") )
	pageName = "&";
else
	pageName = pageName.replace(/_/g, " ").replace(/([\.\/])/g, "\\$1");

// Avoid finding page name inside of HTML attributes by requiring it to be
// surrounded by ends of string or spacing characters.
var pageNameRegex = new RegExp("(\\s|^)(" + mw.util.escapeRegExp(pageName) + ")(\\s|$)");

var makeEditable = function(number, text) {
	return text.replace(
		pageNameRegex,
		'$1<span id="editable-title" contenteditable="true">$2</span>$3'
	);
};

$(function () {
	$("#firstHeading").html(makeEditable);
	$('#editable-title').keypress(function (event) {
		if(event.which === 13) { // enter key
			window.location = mw.util.getUrl("", {
				search: $('#editable-title').text(),
			});
			return false;
		}
	});
});

});