MediaWiki:Common.js — различия между версиями

Материал из roboforum.ru Wiki
Перейти к: навигация, поиск
(Новая: // <source lang="javascript"> /* Execute function on page load ********************************************* * * Description: Wrapper around addOnloadHook() for backwards compatib...)
 
Строка 1: Строка 1:
 
// <source lang="javascript">
 
// <source lang="javascript">
/*  
+
 
Execute function on page load *********************************************
+
/* Execute function on page load *********************************************
  *
+
*
  *  Description: Wrapper around addOnloadHook() for backwards compatibility.
+
*  Description: Wrapper around addOnloadHook() for backwards compatibility.
  *              Will be removed in the near future.
+
*              Will be removed in the near future.
  */
+
*/
 +
 
 
/* Чтоб была возможность изменить поведение кнопки "+" (добавление новой темы) для страниц обсуждения */  
 
/* Чтоб была возможность изменить поведение кнопки "+" (добавление новой темы) для страниц обсуждения */  
 
addOnloadHook(function(){
 
addOnloadHook(function(){
Строка 29: Строка 30:
 
                             + encodeURIComponent(page.replace(' ','_'))
 
                             + encodeURIComponent(page.replace(' ','_'))
 
                             + '&action=raw&ctype=text/javascript&dontcountme=s';
 
                             + '&action=raw&ctype=text/javascript&dontcountme=s';
    if (lang) url = 'http://' + lang + '.wikipedia.org' + url;
 
 
     var s = document.createElement('script');
 
     var s = document.createElement('script');
 
     s.src = url;
 
     s.src = url;
Строка 543: Строка 543:
 
  }
 
  }
 
}
 
}
+
 
 
if (wgCanonicalNamespace == 'Special' && wgCanonicalSpecialPageName == 'Upload')
 
if (wgCanonicalNamespace == 'Special' && wgCanonicalSpecialPageName == 'Upload')
 
  addOnloadHook(uploadPage);
 
  addOnloadHook(uploadPage);
 +
 +
// add resmark
 +
importScript('MediaWiki:Resmark.js');
 +
 
// </source>
 
// </source>

Версия 21:59, 2 октября 2007

// <source lang="javascript">

/* Execute function on page load *********************************************
 *
 *  Description: Wrapper around addOnloadHook() for backwards compatibility.
 *               Will be removed in the near future.
 */

/* Чтоб была возможность изменить поведение кнопки "+" (добавление новой темы) для страниц обсуждения */ 
addOnloadHook(function(){
  var plus = document.getElementById('ca-addsection');
  if (!plus) return;
  var custom = document.getElementById('add-custom-section');
  if (!custom) return;
  plus.firstChild.setAttribute('href', custom.getElementsByTagName('A')[0].href);
})
/**********************************************************************************/
 function addLoadEvent( f ) { addOnloadHook( f ); }

 /** Import module *************************************************************
  *
  *  Предложен [[Участник:Alex_Smotrov]], на основе аналогичной английской функции
  *
  *  Description: Includes a raw wiki page as javascript or CSS, 
  *               used for including user made modules.
  */
 
 function importScript(page, lang) {
     var url = wgScriptPath + '/index.php?title='
                            + encodeURIComponent(page.replace(' ','_'))
                            + '&action=raw&ctype=text/javascript&dontcountme=s';
     var s = document.createElement('script');
     s.src = url;
     s.type='text/javascript';
     document.getElementsByTagName('head')[0].appendChild(s);
 }

 /** Internet Explorer bug fix **************************************************
  */
 
 if (window.showModalDialog && document.compatMode && document.compatMode == "CSS1Compat")
 {
   var oldWidth;
   var docEl = document.documentElement;
 
   function fixIEScroll()
   {
     if (!oldWidth || docEl.clientWidth > oldWidth)
       doFixIEScroll();
     else
       setTimeout(doFixIEScroll, 1);
   
     oldWidth = docEl.clientWidth;
   }
 
   function doFixIEScroll() {
     docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : "";
   }
 
   document.attachEvent("onreadystatechange", fixIEScroll);
   attachEvent("onresize", fixIEScroll);
 }

 /** Tooltips and access keys ***************************************************
  */
 ta = new Object();
 ta["t-print"] = new Array("","Версия без кнопок, пригодная для распечатки");
 ta["t-permalink"]           = new Array("","Ссылка на текущую версию этой статьи");

 /** Interwiki links to featured articles ***************************************
  *
  *  Описание: Ставит звёздочки для избранных иноязычных статей, а заодно и даёт картинку для статуса ICQ
  */
 
 function LinkFA() 
 {
   // iterate over all span-elements
   for(var i=0; a = document.getElementsByTagName("span")[i]; i++) {
      // if found a ICQ span
      if(a.className == "ICQ") {
            a.style.padding = "0 0 0 20px";
            a.style.backgroundImage = "url('http://status.icq.com/online.gif?icq="+a.id+"&img=5')";
            a.style.backgroundRepeat = "no-repeat";
      }
      // if found a FA span
      if(a.className == "FA") {
         // iterate over all li-elements
         for(var j=0; b = document.getElementsByTagName("li")[j]; j++) {
            // if found a FA link
            if(b.className == "interwiki-" + a.id) {
                b.className += " FA"
                b.title = "Эта статья является избранной в другом языковом разделе.";               
            }
         }
      }
   }
 }
 
 addOnloadHook( LinkFA );

/* меняет текст вкладки «статья» на «заглавная», когда надо */

 addOnloadHook(function(){
  if(wgArticleId==4401||wgArticleId==23){var nstabmain=document.getElementById('ca-nstab-main').firstChild;if(nstabmain.textContent){nstabmain.textContent='заглавная'}else{nstabmain.innerText='заглавная'};}
 });

 /* hasClass from en-wiki */

 var hasClass = (function () {
     var reCache = {};
     return function (element, className) {
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
     };
 })();

 /** Collapsible tables *********************************************************
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *               [[Wikipedia:NavFrame]].
  *  Maintainers: [[User:R. Koot]]
  */
 
 var autoCollapse = 2;
 var collapseCaption = "скрыть";
 var expandCaption = "показать";
 
 function collapseTable( tableIndex )
 {
     var Button = document.getElementById( "collapseButton" + tableIndex );
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
     if ( !Table || !Button ) {
         return false;
     }
 
     var Rows = Table.getElementsByTagName( "tr" ); 
 
     if ( Button.firstChild.data == collapseCaption ) {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = "none";
         }
         Button.firstChild.data = expandCaption;
     } else {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = Rows[0].style.display;
         }
         Button.firstChild.data = collapseCaption;
     }
 }
 
 function createCollapseButtons()
 {
     var tableIndex = 0;
     var NavigationBoxes = new Object();
     var Tables = document.getElementsByTagName( "table" );
 
     for ( var i = 0; i < Tables.length; i++ ) {
         if ( hasClass( Tables[i], "collapsible" ) ) {
             NavigationBoxes[ tableIndex ] = Tables[i];
             Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
             var Button     = document.createElement( "span" );
             var ButtonLink = document.createElement( "a" );
             var ButtonText = document.createTextNode( collapseCaption );
 
             Button.style.styleFloat = "right";
             Button.style.cssFloat = "right";
             Button.style.fontWeight = "normal";
             Button.style.textAlign = "right";
             Button.style.width = "6em";
 
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
             ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
             ButtonLink.appendChild( ButtonText );
 
             Button.appendChild( document.createTextNode( "[" ) );
             Button.appendChild( ButtonLink );
             Button.appendChild( document.createTextNode( "]" ) );
 
             var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
             /* only add button and increment count if there is a header row to work with */
             if (Header) {
                 Header.insertBefore( Button, Header.childNodes[0] );
                 tableIndex++;
             }
         }
     }
 
     for ( var i = 0;  i < tableIndex; i++ ) {
         if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
             collapseTable( i );
         }
     }
 }
 
 addOnloadHook( createCollapseButtons );

 /** Dynamic Navigation Bars (experimental) *************************************
  *
  *  Description: See [[Wikipedia:NavFrame]].
  *  Maintainers: UNMAINTAINED
  */
 
  // set up the words in your language
  var NavigationBarHide = '[' + collapseCaption + ']';
  var NavigationBarShow = '[' + expandCaption + ']';
  
  // set up max count of Navigation Bars on page,
  // if there are more, all will be hidden
  // NavigationBarShowDefault = 0; // all bars will be hidden
  // NavigationBarShowDefault = 1; // on pages with more than 1 bar all bars will be hidden
  var NavigationBarShowDefault = autoCollapse;
  
  
  // shows and hides content and picture (if available) of navigation bars
  // Parameters:
  //     indexNavigationBar: the index of navigation bar to be toggled
  function toggleNavigationBar(indexNavigationBar)
  {
     var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
     var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
  
     if (!NavFrame || !NavToggle) {
         return false;
     }
  
     // if shown now
     if (NavToggle.firstChild.data == NavigationBarHide) {
         for (
                 var NavChild = NavFrame.firstChild;
                 NavChild != null;
                 NavChild = NavChild.nextSibling
             ) {
             if ( hasClass( NavChild, 'NavPic' ) ) {
                 NavChild.style.display = 'none';
             }
             if ( hasClass( NavChild, 'NavContent') ) {
                 NavChild.style.display = 'none';
             }
         }
     NavToggle.firstChild.data = NavigationBarShow;
  
     // if hidden now
     } else if (NavToggle.firstChild.data == NavigationBarShow) {
         for (
                 var NavChild = NavFrame.firstChild;
                 NavChild != null;
                 NavChild = NavChild.nextSibling
             ) {
             if (hasClass(NavChild, 'NavPic')) {
                 NavChild.style.display = 'block';
             }
             if (hasClass(NavChild, 'NavContent')) {
                 NavChild.style.display = 'block';
             }
         }
     NavToggle.firstChild.data = NavigationBarHide;
     }
  }
  
  // adds show/hide-button to navigation bars
  function createNavigationBarToggleButton()
  {
     var indexNavigationBar = 0;
     // iterate over all < div >-elements 
     var divs = document.getElementsByTagName("div");
     for(
             var i=0; 
             NavFrame = divs[i]; 
             i++
         ) {
         // if found a navigation bar
         if (hasClass(NavFrame, "NavFrame")) {
  
             indexNavigationBar++;
             var NavToggle = document.createElement("a");
             NavToggle.className = 'NavToggle';
             NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
             NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
             
             var NavToggleText = document.createTextNode(NavigationBarHide);
             NavToggle.appendChild(NavToggleText);
             // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
             for(
               var j=0; 
               j < NavFrame.childNodes.length; 
               j++
             ) {
               if (hasClass(NavFrame.childNodes[j], "NavHead")) {
                 NavFrame.childNodes[j].appendChild(NavToggle);
               }
             }
             NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
         }
     }
     // if more Navigation Bars found than Default: hide all
     if (NavigationBarShowDefault < indexNavigationBar) {
         for(
                 var i=1; 
                 i<=indexNavigationBar; 
                 i++
         ) {
             toggleNavigationBar(i);
         }
     }
   
  }
  
  addOnloadHook( createNavigationBarToggleButton );

/** Ed's counter ***********************************************************	 
*	 
*/	 
		 
var disable_counter = 1;	 
function count_me()	 
{	 
if(disable_counter != 1 && window.location.href.match("^http://ru.wikipedia.org/wiki/") && !window.location.href.substring(7).match(":"))	 
	 
{	 
var rnd = Math.random() > 0.1 ? 1 : 0;	 
if(rnd == 0 || (document.referrer && !document.referrer.match("(wikipedia.org|wiktionary.org|wikiquote.org|wikisource.org|wikimedia.org|yandex.ru|google.ru|google.com)")))	 
{	 
var img = new Image();	 
img.src = "http://tools.wikimedia.de:22902/" + (rnd == 0 ? "1" : "0") + document.referrer;	 
}	 
}	 
}	 
count_me();

 /** Делаем ссылку "править" для нулевой секции *****************************************<nowiki>
 */
 var disable_zero_section = 0;
 function edit_zero_section()
 {
   var  body = document.getElementById ('bodyContent');
 
   if (disable_zero_section != 1 && body.innerHTML.match ('class="editsection"')) {
     var  title = document.title.substr (0, document.title.lastIndexOf (' — '));
 
     if (typeof encodeURIComponent == 'function')
       title = encodeURIComponent (title);
 
     body.innerHTML = '<div class="editsection" id="ca-edit-0">[<a href="http://ru.wikipedia.org/w/index.php?title=' + title + '&amp;action=edit&amp;section=0">править</a>]</div>' + body.innerHTML;
   }
 }
 
 addOnloadHook(edit_zero_section);

 /*</nowiki>*/

 /** Расширенный поиск *****************************************<nowiki>
 ** Автор: ru:User:Не А
 */
 function SpecialSearchEnhanced() 
 {
    var mainNode = document.getElementsByTagName("form");
    if (!mainNode) return;
    
    var searchValue = document.forms[0].search.value
	var safeSearchValue = searchValue.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
    var firstForm = mainNode[0];

    var node = document.createElement('div');
    
    var googleSearch  = '<form action="http://www.google.com/custom" method="get" name="google" target="_blank" id="google">';
    googleSearch += '<input type="hidden" name="hl" value="ru" />';
    googleSearch += '<input type="hidden" name="domains" value="ru.wikipedia.org" />';
    googleSearch += '<input type="hidden" name="q" maxlength="2048" value="' + safeSearchValue + '" />'
    googleSearch += '<input type="hidden" name="sitesearch" value="ru.wikipedia.org" />'
    googleSearch += '<input type="button" value="Google по Википедии" onclick="document.google.q.value = document.forms[0].search.value; this.form.submit();" style="width: 12em;" />'
    googleSearch += '</form>'
    
    var yandexSearch  = '<form action="http://www.yandex.ru/yandsearch" method="get" name="yandex" target="_blank" id="yandex">';
    yandexSearch += '<input type="hidden" name="text" maxlength="300" value="' + safeSearchValue + '" />';
    yandexSearch += '<input type="hidden" name="site" value="ru.wikipedia.org" />';
    yandexSearch += '<input type="hidden" name="ras" value="1" />'
    yandexSearch += '<input type="hidden" name="site_manually"  value="true" />'
    yandexSearch += '<input type="hidden" name="server_name" value="Википедия" />'
    yandexSearch += '<input type="button" value="Яндекс по Википедии"  onclick="document.yandex.text.value = document.forms[0].search.value; this.form.submit();" style="width: 12em;" />'
    yandexSearch += '</form>'

    node.innerHTML = node.innerHTML + '<table style="margin-left: 75%;  padding-left:4px;"><tr><td>' + yandexSearch + '</td></tr><tr><td>' + googleSearch + '</td></tr></table>';
        
    firstForm.parentNode.insertBefore(node, firstForm.nextSibling);
 }

 if (wgPageName == "Служебная:Search") { addOnloadHook(SpecialSearchEnhanced); }

 /*</nowiki>*/

 /** "Technical restrictions" title fix *****************************************
  */
 
 // For pages that have something like Template:Lowercase, replace the title, but only if it is cut-and-pasteable as a valid wikilink.
 //	(for instance [[iPod]]'s title is updated.  <nowiki>But [[C#]] is not an equivalent wikilink, so [[C Sharp]] doesn't have its main title changed)</nowiki>
 //
 // The function looks for a banner like this: <nowiki>
 // <div id="RealTitleBanner">    <!-- div that gets hidden -->
 //   <span id="RealTitle">title</span>
 // </div>
 // </nowiki>An element with id=DisableRealTitle disables the function.
 
 // similar to innerHTML, but only returns the text portions of the insides, excludes HTML
 function pickUpText(aParentElement) {
  var str = "";
 
  function pickUpTextInternal(aElement) {
    var child = aElement.firstChild;
    while (child) {
      if (child.nodeType == 1)          // ELEMENT_NODE 
        pickUpTextInternal(child);
      else if (child.nodeType == 3)     // TEXT_NODE
        str += child.nodeValue;
 
      child = child.nextSibling;
    }
  }
 
  pickUpTextInternal(aParentElement);
 
  return str;
 }
 
 var disableRealTitle = 0;		// users can disable this by making this true from their monobook.js
 addOnloadHook(function() {
 	try {
 		var realTitleBanner = document.getElementById("RealTitleBanner");
 		if (realTitleBanner && !document.getElementById("DisableRealTitle") && !disableRealTitle) {
 			var realTitle = document.getElementById("RealTitle");
 			if (realTitle) {
 				var realTitleHTML = realTitle.innerHTML;
 				realTitleText = pickUpText(realTitle);
 
 				var isPasteable = 0;
 				//var containsHTML = /</.test(realTitleHTML);	// contains ANY HTML
 				var containsTooMuchHTML = /</.test( realTitleHTML.replace(/<\/?(sub|sup|small|big)>/gi, "") ); // contains HTML that will be ignored when cut-n-pasted as a wikilink
 				// calculate whether the title is pasteable
 				var verifyTitle = realTitleText.replace(/^ +/, "");		// trim left spaces
 				verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length);	// uppercase first character
 
 				// if the namespace prefix is there, remove it on our verification copy.  If it isn't there, add it to the original realValue copy.
 				if (wgNamespaceNumber != 0) {
 					if (wgCanonicalNamespace == verifyTitle.substr(0, wgCanonicalNamespace.length).replace(/ /g, "_") && verifyTitle.charAt(wgCanonicalNamespace.length) == ":") {
 						verifyTitle = verifyTitle.substr(wgCanonicalNamespace.length + 1);
 					} else {
 						realTitleText = wgCanonicalNamespace.replace(/_/g, " ") + ":" + realTitleText;
 						realTitleHTML = wgCanonicalNamespace.replace(/_/g, " ") + ":" + realTitleHTML;
 					}
 				}
 
 				// verify whether wgTitle matches
 				verifyTitle = verifyTitle.replace(/^ +/, "").replace(/ +$/, "");		// trim left and right spaces
 				verifyTitle = verifyTitle.replace(/_/g, " ");		// underscores to spaces
 				verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length);	// uppercase first character
 				isPasteable = (verifyTitle == wgTitle);
 
 				var h1 = document.getElementsByTagName("h1")[0];
 				if (h1 && isPasteable) {
 					h1.innerHTML = containsTooMuchHTML ? realTitleText : realTitleHTML;
 					if (!containsTooMuchHTML)
 						realTitleBanner.style.display = "none";
 				}
 				document.title = realTitleText + " — Википедия";
 			}
 		}
 	} catch (e) {
 		/* Something went wrong. */
 	}
 });

 /* Замена неправильного заголовка правильным by SergV *****************************************
 */
  
  // Все неправильные заголовки
  title_restr_alerts = ["trestrictions_replace", "trestrictions_alert"];

  // Только шаблон title
  //title_restr_alerts = ["trestrictions_replace"];

  function display_correct_title () {
    var title_restr_alert1, a1;
    if (document.getElementsByTagName && document.getElementById) {  
      for(var i=0; i < title_restr_alerts.length; i++) { 
        title_restr_alert1 = title_restr_alerts[i];
        a1 = document.getElementById(title_restr_alert1);
        if(a1) {
          ct = document.getElementById("trestrictions_correct");
          if(ct) {
            document.getElementsByTagName("h1")[0].innerHTML  = ct.innerHTML;
            a1.style.display = "none";
            document.getElementById("trestrictions_replaced").style.display = "block";
          }
          break;
        }
      }
    }
  }

 addOnloadHook(display_correct_title);

 /* Другое   *****************************************
 */
 window.przyciskiOpisDone = false;
 var auto_comment = 0;

 /* подгрузка файла со скриптами для редактирования  *****************************************
 */
 if (document.URL.indexOf("action=edit") > 0 || document.URL.indexOf("action=submit") > 0)
 {
        if (wgCanonicalNamespace != "Special")
        {
                document.write('<script type="text/javascript" src="/w/index.php?title=MediaWiki:Onlyifediting.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');
        }
 }

 /* При загрузке [[Special:Upload]] вставляет в поле описания [[Шаблон:Изображение]]
    По ссылке добавляет в поле [[Шаблон:Обоснование добросовестного использования]] и увеличивает размер поля
    Автор - [[User:Alex Smotrov]]
 */
function uploadPage(){
 //автоматически вставить {Изображение}
 var desc = document.getElementById('wpUploadDescription')
 var temp = document.getElementById('imageinfo')
 if (temp && desc && !desc.value) desc.value = temp.innerHTML
 //создать ссылку для вставки {Обоснование}
 var span = document.getElementById('insertlink')
 if (!span) return
 var a = document.createElement('a')
 a.href = 'javascript:addRationaleTemplate()'
 span.parentNode.insertBefore(a, span)
 a.appendChild(span)
 span.style.display = 'inline'
}
 
function addRationaleTemplate(){
 var desc = document.getElementById('wpUploadDescription')
 var temp = document.getElementById('rationale')
 if(!desc || !temp) return
 if (desc.value.indexOf(temp.innerHTML.substring(0,8)) == -1){
   desc.value += '\n' + temp.innerHTML
   desc.rows = 15
 }
}

if (wgCanonicalNamespace == 'Special' && wgCanonicalSpecialPageName == 'Upload')
 addOnloadHook(uploadPage);

// add resmark
importScript('MediaWiki:Resmark.js');

// </source>