User:Qwerfjkl/scripts/journal.js

// Forked from Benutzer:Schnark/js/journal // Documentation at Benutzer:Schnark/js/journal

/*global mediaWiki*/ (function ($, mw) { "use strict"; var config = {	cookie: 'schnark-journal', //Name des Cookies/Storage-Key	count: 5, //Anzahl der Einträge	type: 'storage', //alternativ: cookie	style: 'position: fixed; bottom: 0.5em; left: 14.75em; font-size: 0.75em;' },

specialPages = { Allmessages: 'lang', Allpages: 'from', BlockList: 'wpTarget', Categories: 'from', Contributions: 'target', DeletedContributions: 'target', Emailuser: 'target', Export: 'pages', FileDuplicateSearch: 'filename', LinkSearch: 'target', Listfiles: 'user', Listusers: 'group', Log: 'type', MIMEsearch: 'mime', Movepage: 'target', PagesWithProp: 'propname', Prefixindex: 'prefix', RandomInCategory: 'category', Search: 'search', Undelete: 'target', Userrights: 'user', Whatlinkshere: 'target', Withoutinterwiki: 'prefix' };

function getCookie { return mw.loader.using('mediawiki.cookie').then(function {		var val = mw.cookie.get(config.cookie);		if (val) {			return val;		} else {			throw 'missing';		}	}); }

function setCookie (val) { return mw.loader.using('mediawiki.cookie').then(function {		mw.cookie.set(config.cookie, val);	}); }

function getStorage { return mw.loader.using('mediawiki.storage').then(function {		var val = mw.storage.get(config.cookie);		if (val) {			return val;		} else {			throw 'missing';		}	}); }

function setStorage (val) { return mw.loader.using('mediawiki.storage').then(function {		if (!mw.storage.set(config.cookie, val)) {			throw 'error';		}	}); }

function getJournal { var first = config.type === 'cookie' ? getCookie : getStorage, second = config.type === 'cookie' ? getStorage : getCookie; return first.then(null, function {		return second.then(null, function  { return 'Special:Search'; });	}); }

function setJournal (val) { var first = config.type === 'cookie' ? setCookie : setStorage, second = config.type === 'cookie' ? setStorage : setCookie; return first(val).then(null, function {		return second(val);	}); }

function pageName { var page = mw.config.get('wgPageName'), sub; if (mw.config.get('wgNamespaceNumber') === -1 && page.indexOf('/') === -1) { sub = specialPages[mw.config.get('wgCanonicalSpecialPageName')]; sub = sub && mw.util.getParamValue(sub); if (sub) { page += '/' + sub; }	}	return page; }

function update (titles) { var title = pageName, index = titles.indexOf(title), html; if (index > -1) { titles.splice(index, 1); }	html = titles.map(function (title) {		return mw.html.element('a', {href: mw.util.getUrl(title)}, title.replace(/_/g, ' '));	}).join(' · '); titles.push(title); while (titles.length > config.count) { titles.shift; }	setJournal(titles.join('|')); return html; }

function init { getJournal.then(function (titles) {		$('#p-personal').after(mw.html.element('div', {			id: 'schnark-journal',			style: config.style		}, new mw.html.Raw( update(titles.split('|')) )));	}); }

mw.hook('userjs.load-script.journal').fire(config);

$.when(mw.loader.using('mediawiki.util'), $.ready).then(init);

})(jQuery, mediaWiki); //

// Category:Wikipedia scripts