מדיה ויקי:Common.js/externalsmaker.js: הבדלים בין גרסאות בדף

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית
קפיצה לניווט קפיצה לחיפוש
(+ הגאולה)
אין תקציר עריכה
שורה 89: שורה 89:
},
},
{
{
hostname: 'www.hageula.com',
hostname: 'hageula.com',
params: [
params: [
{ str: 'קישור הגאולה' },
{ str: 'קישור הגאולה' },

גרסה מ־14:47, 29 ביוני 2014

/*Author:[[user:שמוליק]] with a lot of help from [[user:קיפודנחש]] */
function wikiit() {

	function dateFormat(dateArr)
	{
		var m = ["ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר"];
		if (dateArr instanceof Array)
		{
			if (dateArr.length != 3)
				return '';

			dateArr[1] = m[Number(dateArr[1])-1];
			if (Number(dateArr[2])<=50)
				dateArr[2]=20+dateArr[2];
			else if (Number(dateArr[2])>=50&&Number(dateArr[2])<100)
				dateArr[2]=19+dateArr[2];

			if (dateArr[0].charAt(0) == '0')
				dateArr[0] = dateArr[0].substring(1, dateArr[0].length);

			dateArr = dateArr[0]+" ב"+dateArr[1]+" "+dateArr[2];
		}
		return jQuery.trim(dateArr);
	}

 function dateFormatShortHeb(dateArr)
	{
		var m = ["ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר"];
		if (dateArr instanceof Array)
		{
			if (dateArr.length != 3)
				return '';

      if (dateArr[1][dateArr[1].length-1] == '\'')
      {
        for (i in m)
          if (m[i].indexOf(dateArr[1].substr(0,dateArr[1].length-1)) == 0)
          {
            dateArr[1] = m[i];
            break;
          }
      }

			if (Number(dateArr[2])<=50)
				dateArr[2]=20+dateArr[2];
			else if (Number(dateArr[2])>=50&&Number(dateArr[2])<100)
				dateArr[2]=19+dateArr[2];

			if (dateArr[0].charAt(0) == '0')
				dateArr[0] = dateArr[0].substring(1, dateArr[0].length);

			dateArr = dateArr[0]+" ב"+dateArr[1]+" "+dateArr[2];
		}
		return jQuery.trim(dateArr);
	}

	function engDateParse(engDate)
	{
		var m = ["January" , "February" , "March" , "April", "May", "June", "July", "August", "September", "October","November", "December"];
		engDate = engDate.match(/(\w+) (\d+), (\d+)/);
		return [engDate[2], (m.indexOf(engDate[1]) + 1) , engDate[3] ];
	}

	var ATags = [/<a .*?>/gi, /<\/a>/gi];

	function match (str, expr){str = str?str.match(expr):str; return (str&&str.length>1)?str[1]:'';}

  function getParameterByName(name)
  {
    /* http://james.padolsey.com/javascript/bujs-1-getparameterbyname/ */
    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/gi, ' '));
  }

	var data = [
		{
			hostname: 'www.chabad.info',
			params: [
				{ str: 'קישור חבד אינפו' },
				{
					str:  location.href,
					match: /www.chabad.info\/#!g=1&url=article&id=(\d+)/
				},
				//[
					{ telem: 'div.art_title' }
				//]
			]
		},
		{
			hostname: 'hageula.com',
			params: [
				{ str: 'קישור הגאולה' },
				{
					str:  location.href,
					match: /hageula.com\/news\/([^/])+\/d+.html/
				},
				{
					str:  location.href,
					match: /hageula.com\/news\/[^/]+\/(d+).html/
				},
				//[
					{ telem: 'h1:first' }
				//]
			]
		},
		{
			hostname: 'www.chabad.co.il',
			params: [
				{ str: 'קישור חבד בישראל' },
				{
					str:  location.href,
					match: /www.chabad.co.il\/\?template=article&topic=(\d+)&article=\d+/
				},
				{
					str:  location.href,
					match: /www.chabad.co.il\/\?template=article&topic=\d+&article=(\d+)/
				},
				//[
					{ telem: 'div.art_title' }
				//]
			]
		}
	];

  var isFound = false;
  for (var i = 0; i < data.length; i++)
  {
    if (location.hostname.match(data[i].hostname)
    && (!data[i].hrefmatch || location.href.match(data[i].hrefmatch))
    && (!data[i].condition || data[i].condition())
    )
    {
      var params = [];
      var k = 0;
      for (var j = 0; j < data[i].params.length; j++)
		try {
			var curParam = (data[i].params[j] instanceof Array)?(data[i].params[j][k]):(data[i].params[j]);
			params[j] = '';
			if (typeof curParam.str != "undefined")
			  params[j] = curParam.str;
			else if (typeof curParam.urlparam!= "undefined")
                          params[j] = getParameterByName(curParam.urlparam)||"";
			else if (typeof curParam.elem != "undefined")
			{
			  if (curParam.elem instanceof Array)
			  {
				params[j] = $(curParam.elem[0]);
				for(var elemIdx = 1; elemIdx < curParam.elem.length; elemIdx++)
				{
				  var func = curParam.elem[elemIdx].split(',');
				  params[j] = params[j][func[0]](func.length>0?func[1]:null);
				}
				if (typeof(params[j]) != "string")
          params[j] = params[j].html();
			  }
			  else
				params[j] = $(curParam.elem).html();
			}
			else if (typeof curParam.elements != "undefined")
			{
			  params[j] = $(curParam.elements[0]).map(function(el){return $(this).html();}).toArray().join(curParam.elements[1]);
			}
			else if (typeof curParam.telements != "undefined")
			{
			  params[j] = $(curParam.telements[0]).map(function(el){return $(this).text();}).toArray().join(curParam.telements[1]);
			}
			else if (typeof curParam.telem != "undefined")
			{
				params[j] = $(curParam.telem).first().text();
			}

			if (typeof curParam.match != "undefined")
			{
			  if (!(curParam.match instanceof Array))
				params[j] = match(params[j], curParam.match);
			  else for (patIdx = 0; patIdx < curParam.match.length; patIdx++)
			  {
				temp = match(params[j], curParam.match[patIdx]);
				if (temp != '')
				{
				  params[j] = temp;
				  break;
				}
			  }
			}


			if (typeof curParam.remove != "undefined")
			  for (var removeIdx = 0; removeIdx < curParam.remove.length; removeIdx++)
				params[j] =  params[j].replace(curParam.remove[removeIdx],"");

			if (typeof curParam.split != "undefined")
			  params[j] =  params[j].split(curParam.split);


			if (typeof curParam.func != "undefined")
			{
			  if (!(curParam.func instanceof Array))
				curParam.func = [curParam.func];
			  for (var funcIdx = 0; funcIdx < curParam.func.length; funcIdx++)
			  {
				params[j] = curParam.func[funcIdx](params[j]);
			  }
			}

			if (typeof params[j] == "string")
			{
            params[j] = jQuery.trim(unescape(params[j].replace(/&nbsp;|\u202B|\u202C/gm," ").replace(/\s+/gm, ' '))
                                                            .replace("=","{{=}}")
                                                            .replace(/(\|\|?)/gm,function(x){return "{{" + x.replace(/\|/g,"!") + "}}";}));
      }
      else if (jQuery.isArray(params[j]))
      {
        params[j] = jQuery.trim(params[j].join("|"));
      }

			if (typeof curParam.prefix != "undefined")
				params[j] = curParam.prefix + params[j];

 			if (typeof curParam.defvalue != "undefined" && params[j] == curParam.defvalue)
				params[j] = '';

			if (typeof curParam.shouldnt != "undefined" && typeof params[j] == "string" && params[j].match(curParam.shouldnt))
				params[j] = '';


			if ((params[j] == '') && (data[i].params[j] instanceof Array) && (k < data[i].params[j].length - 1))
			{
				k++;
				j--;
				continue;
			}

			k = 0;

		}
			catch(e) {}


		var minimum = (typeof data[i].minimum != "undefined") ? (data[i].minimum) : (0);

		while (params[params.length-1]=="" && params.length > minimum) //remove all last empty params
			params.pop();

		prompt("Your template:", '{{' + params.join('|') + '}}');
		isFound = true;
		break;
	}
  }

  if (!isFound)  alert("This site\\page isn't supported by the bookmarklet.");
}
(function ()
{
	if (typeof(jQuery)!="undefined")
		window.$ = jQuery;
	var s=document.createElement('script');
	s.setAttribute('src',"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
	s.onload=wikiit;
	document.getElementsByTagName('body')[0].appendChild(s);
})();