מדיה ויקי:סקריפטים/66.js

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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
// script for script developers: allows you to easilly set and clear personal script to be included
// on startup.

(function() {
	var lsKey = 'personalScript';
	
	function getTop() {return ((lsKey in localStorage) && localStorage[lsKey].split('\n')[0]) || ''}
	
	function allScripts() {
		return (localStorage[lsKey] || '').split('\n');
	}
	
	function setScript(script) {
		var ls = localStorage[lsKey];
		if (lsKey in localStorage)
		{
			var 
				lsa = allScripts(),
				ind = $.inArray(script, lsa);
			if (ind < 0)
				ind = 10;
			if (! lsa[0])
				lsa.shift(); // if first is empty, remove it.
			lsa.splice(ind, 1); // remove current if exists, or last if length >= 10
			lsa.unshift(script);
			localStorage[lsKey] = lsa.join('\n');
		} else
			localStorage[lsKey] = script;
		loadScript(script);
	}
	
	function loadScript(s) {
		if (!s)
			return;
		try {
			var isCss = /\.css$/i.test(s);
			if (/\/\//.test(s)) 
				mw.loader.load(s, isCss ? 'text/css' : 'text/javascript');
			else 
				return isCss ? importStylesheet(s) : importScript(s);
		} catch(e) {}
	}

	function mySetSpecialScript() {
		mw.loader.using('jquery.ui', function() {
			var 
				inputBox = $('<input>').val(getTop()).css({width: '22em', direction: 'ltr'}),
				selector = $('<select>')
					.css({direction: 'ltr'})
					.change(function() {inputBox.val(this.value)});
			$.each(allScripts(), function(ind, item) {
				selector.append($('<option>', {value: item, text: item.substr(0, 60) + (item.length > 60 ? '...':'')}));
			});
			$('<div>')
				.css({direction: 'ltr'})
				.dialog(
					{title: 'סקריפט אישי',
					modal: true,
					position: [60, 60],
					width: 'auto',
					minWidth: '36em',
					buttons: [
						{text: 'אישור', click: function() {
							setScript(inputBox.val());
							$(this).dialog('close');
						}},
						{text: 'ביטול', click: function() {
							setScript('');
							$(this).dialog('close');
						}}]
				})
				.append(inputBox)
				.append($('<p>'))
				.append(selector);
				$('.ui-dialog-buttonpane button').css({float: 'right'});
		});
	}

	loadScript(getTop());
	var portletLink = mw.util.addPortletLink('p-cactions', '#noAnchor', 'סקריפט אישי');
	if ( portletLink ) {
		portletLink.onclick = mySetSpecialScript;
	}
})();