Difference between revisions of "User:Seb26/valve.js"

From Team Fortress Wiki
Jump to: navigation, search
(y i bother)
 
(18 intermediate revisions by 2 users not shown)
Line 79: Line 79:
  
 
     addOnloadHook(function () {
 
     addOnloadHook(function () {
         addPortletLink("p-cactions", wgServer + wgScriptPath.replace("$1", "Special:Upload?wpDestFile=" + wgPageName + "g&wpForReUpload=1"),
+
         addPortletLink("p-cactions", wgServer + wgScript.replace("/w/index.php", "/w/index.php?title=Special:Upload&wpDestFile=" + wgTitle),
 +
                      "Upload", "ca-upload", "upload");
 +
    });
 +
 
 +
    addOnloadHook(function () {
 +
        addPortletLink("p-cactions", wgServer + wgScript.replace("/w/index.php", "/w/index.php?title=Special:Upload&wpDestFile=" + wgTitle + "&wpForReUpload=1"),
 
                       "Reupload", "ca-reupload", "reupload");
 
                       "Reupload", "ca-reupload", "reupload");
 
     });
 
     });
 
}
 
}
 +
 +
// Adds a "Replace" tab which pops up two prompt boxes; one for a regexp and one for a replacement
 +
function wpTextboxReplace()
 +
{
 +
    var s = prompt("Search regexp:");
 +
    if(s){
 +
        var r = prompt("Replace /"+s+"/ with:");
 +
        if(!r && r != '') return;
 +
        var txt = document.editform.wpTextbox1;
 +
        txt.value = txt.value.replace(new RegExp(s, "mg"), r);
 +
    }
 +
}
 +
addOnloadHook(function () {
 +
    if (document.forms.editform) {
 +
        addPortletLink('p-cactions', 'javascript:wpTextboxReplace()', 'Replace', 'ca-replace',
 +
                      'Regexp replace for the edit window', 'R', document.getElementById('ca-history'));
 +
    }
 +
});
  
 
// ajaxPreview.js
 
// ajaxPreview.js
Line 231: Line 254:
 
if (wgCanonicalNamespace!='Special' && (wgAction=='edit' || wgAction=='submit'))
 
if (wgCanonicalNamespace!='Special' && (wgAction=='edit' || wgAction=='submit'))
 
  addOnloadHook(ajaxPreviewInit)
 
  addOnloadHook(ajaxPreviewInit)
 +
 +
function addTranslateTab() {
 +
    if ( wgNamespaceNumber == 0 ) {
 +
        var m = /^(.+)\/([^/]+)$/i.exec(wgTitle);
 +
        var wBasePageName = m[1];
 +
        var wSubPageName = m[2];
 +
        addPortletLink('p-cactions', wgScript + '?title=' + wgTitle + '&action=edit&preload=' + wBasePageName, 'Translate', 'ca-translate');
 +
    }
 +
}
 +
addOnloadHook(addTranslateTab);

Latest revision as of 07:34, 26 February 2011

// Add date and time to your monobook "personal menu" list at the very top of the page.
// Created by [[User:Mathwiz2020]]

// Indicate where you would like the time to appear:
// 1 is first (before username), 2 is second (before talk link), ... 7 is last (after log out link)
insertBeforeNum = 7;

// Do NOT edit below this line unless you're experiened in javascript
insertBeforeArr = new Array("","pt-userpage","pt-mytalk","pt-preferences","pt-watchlist","pt-mycontris","pt-logout","");
insertBefore = insertBeforeArr[insertBeforeNum];

function makeTime()
{
  var li = document.createElement( 'li' );
  li.id = 'pt-time';

  var mySpan = document.createElement( 'span' );
  mySpan.appendChild( document.createTextNode( 'date and time' ) );

  li.appendChild( mySpan );

  if ( insertBefore )
  {
    var before = document.getElementById( insertBefore );
    before.appendChild( li, before );
  }
  else // append to end (right) of list
  {
    document.getElementById( 'pt-logout' ).parentNode.appendChild( li );
  }

  getTime();
}

if      ( window.addEventListener ) window.addEventListener ( 'load', makeTime, false );
else if ( window.attachEvent      ) window.attachEvent      ( 'onload', makeTime      );

function getTime()
{
    var time    = new Date();
    var date    = time.getUTCDate();
    var months  = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
        month   = months[time.getUTCMonth()];
    var year    = time.getUTCFullYear();
    var hours   = '0' + time.getUTCHours();
        hours   = hours.substr(hours.length-2, hours.length);
    var minutes = '0' + time.getUTCMinutes();
        minutes = minutes.substr(minutes.length-2, minutes.length);
    var seconds = '0' + time.getUTCSeconds();
        seconds = seconds.substr(seconds.length-2, seconds.length);
    var curTime = hours + ":" + minutes + ":" + seconds + ", " + date + " " + month + " " + year + " (UTC)";
    datePlace   = document.getElementById('pt-time').childNodes[0].childNodes[0];
                  datePlace.replaceData(0, datePlace.length, curTime);
    doTime      = window.setTimeout("getTime()", 1000);
}
//


// addPurge
addOnloadHook(function () {
    var hist; var url;
    if (!(hist = document.getElementById('ca-history') )) return;
    if (!(url = hist.getElementsByTagName('a')[0] )) return;
    if (!(url = url.href )) return;
    addPortletLink('p-cactions', url.replace(/([?&]action=)history([&#]|$)/, '$1purge$2'),
                   'Purge', 'ca-purge', 'Purge server cache for this page', '0');
});

//

    addOnloadHook(function () {
        addPortletLink("p-cactions", wgServer + wgArticlePath.replace("$1", "Special:Prefixindex/" + wgPageName),
                       "Prefix index", "ca-prefixindex", "prefixindex");
    });

//

if ( wgNamespaceNumber == 6 ) {

    addOnloadHook(function () {
        addPortletLink("p-cactions", wgServer + wgScript.replace("/w/index.php", "/w/index.php?title=Special:Upload&wpDestFile=" + wgTitle),
                       "Upload", "ca-upload", "upload");
    });

    addOnloadHook(function () {
        addPortletLink("p-cactions", wgServer + wgScript.replace("/w/index.php", "/w/index.php?title=Special:Upload&wpDestFile=" + wgTitle + "&wpForReUpload=1"),
                       "Reupload", "ca-reupload", "reupload");
    });
}

// Adds a "Replace" tab which pops up two prompt boxes; one for a regexp and one for a replacement
function wpTextboxReplace()
{
    var s = prompt("Search regexp:");
    if(s){
        var r = prompt("Replace /"+s+"/ with:");
        if(!r && r != '') return;
        var txt = document.editform.wpTextbox1;
        txt.value = txt.value.replace(new RegExp(s, "mg"), r);
    }
}
addOnloadHook(function () {
    if (document.forms.editform) {
        addPortletLink('p-cactions', 'javascript:wpTextboxReplace()', 'Replace', 'ca-replace',
                       'Regexp replace for the edit window', 'R', document.getElementById('ca-history'));
    }
});

// ajaxPreview.js

function ajaxPreviewInit(){
 if ((wgNamespaceNumber % 2 == 0) && /\.(js|css)$/.test(wgTitle)) return
 if (typeof ajaxPreviewKey != 'string') ajaxPreviewKey = 'p'
 if (typeof ajaxDiffKey != 'string') ajaxDiffKey  = 'v'
 ajaxPreviewPos = window.ajaxPreviewPos || 'right'
 
 if (ajaxPreviewPos != 'bottom'){
   var d = document.createElement('div'); d.style.cssText = 'width:100%; clear:both'
   var tNew = document.createElement('div'); tNew.style.cssText = 'float:'+ ajaxPreviewPos
   if( typeof wgWikiEditorPreferences != 'undefined' && wgWikiEditorPreferences['toolbar'] ) {
     var tOld = document.getElementById('wikiPreview');
     tOld.parentNode.insertBefore(d, tOld.nextSibling);
     tOld.parentNode.insertBefore(tNew, d.nextSibling);
   } else {
     var tOld = document.getElementById('toolbar') || document.getElementById( 'editform' );
     tOld.style.clear = 'none'
     tOld.parentNode.insertBefore(d, tOld)
     tOld.parentNode.insertBefore(tNew, tOld)
   }
 }
 addBtn(window.ajaxPreviewButton, 'wpPreview', ajaxPreviewKey)
 addBtn(window.ajaxDiffButton, 'wpDiff', ajaxDiffKey)
 
 function addBtn(btnName, id, akey){ 
  var btnOld = document.getElementById(id);
  if (!btnOld) return;
  var btn = document.createElement('input');
  btn.type = 'button';
  btn.onclick = ajaxPreviewClick;
  btn.id = id + 'Live';
  if (!btnName){ //extract last word from standard buttons
    var btnName = btnOld.value.split(' '); btnName = btnName[btnName.length-1];
    btnName = btnName.substring(0,1).toUpperCase() + btnName.substring(1);
  }
  btn.value = btnName;
  btn.title = btnOld.value + ' (Ajax)'
  if (ajaxPreviewPos == 'bottom'){
    btnOld.parentNode.insertBefore(btn, btnOld)
    btn.value = btnOld.value
    btnOld.value = '>'
  }else{
    btn.style.cssText = 'height:22px; padding:0 1px'
    tNew.appendChild(btn)
  }
  if (akey){ //reassign acces key
    if (btnOld.accessKey == akey){ 
      btnOld.accessKey = ''
      btnOld.title = btnOld.title.replace(tooltipAccessKeyRegexp, '')
    }
	btn.accessKey = akey
    btn.title += ' ['+tooltipAccessKeyPrefix+akey+']'
  }
  btn.value2 = btn.value
 }
}
 
 
 
function ajaxPreviewClick(){ajaxPreviewRun(this)}
 
function ajaxPreviewRun(btn){
 var wkPreview = document.getElementById('wikiPreview')
 var previewform = document.editform;
 var aj = sajax_init_object()
 if (!wkPreview || !previewform || !aj) return
 var oldHeight = wkPreview.offsetHeight
 var el;
 var htm;
 var isDiff = (btn.id=='wpDiffLive');
 
 wkPreview.style.opacity = '0.3';
 wkPreview.style.color = 'gray';
 document.body.style.cursor = 'wait'
 if (el=document.getElementById('wikiDiff')) el.style.display = 'none'
 if (el=document.getElementById('newarticletext')) el.style.display = 'none'
 btn.style.width = Math.max(btn.scrollWidth, btn.offsetWidth) + 'px';
 btn.value = '...'
 
 //prepare
 var txt = previewform.wpTextbox1.value;
 var action = wgScriptPath + "/api.php";
 var params = '';
 
 if (isDiff){
   params = "action=query&format=json&prop=revisions&rvprop=&indexpageids=&titles="+ encodeURIComponent( wgPageName );
   params += "&rvdifftotext=" + encodeURIComponent( txt );
   if (!window.ajaxPreview_CSS) ajaxPreview_CSS = importStylesheetURI('/skins-1.5/common/diff.css') 
 }else{
   params = "action=parse&format=json&pst=&prop=text&title=" + encodeURIComponent( wgPageName );
   if (previewform.wpSection && previewform.wpSection.value) txt += '\n<br /><references />'
   params += "&text=" + encodeURIComponent( txt );
 }
 
 //send
 aj.open('POST', action, true);
 aj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 aj.onreadystatechange = function(){
  if (aj.readyState != 4) return
  wkPreview.style.display = 'block'
  var jsonObject = eval('(' + aj.responseText + ')');
 
  if (isDiff){
   try {
    var htm = '<h2>Changes (Live)</h2>';
    htm += '<table class="diff"><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content">';
    var index = jsonObject['query']['pageids'][0];
    htm += jsonObject['query']['pages'][index]['revisions'][0]['diff']['*'];
    htm += '</table>';
   } catch( err ) {
    var htm = 'error: ' + err;
   }
  }else{
   try {
    var htm = '<h2>Preview (Live)</h2>';
    htm += jsonObject['parse']['text']['*'];
   } catch( err ) {
    var htm = "error: "+err;
   }
  }
  wkPreview.innerHTML = htm
  btn.value = btn.value2
  btn.blur()
 
  if (window.ajaxPreviewScrollTop && wkPreview.scrollIntoView) wkPreview.scrollIntoView()
  else document.documentElement.scrollTop +=  wkPreview.offsetHeight - oldHeight 
 
  wkPreview.style.opacity = ''; wkPreview.style.color = ''; document.body.style.cursor = ''
  if (!isDiff) ajaxPreviewFinish(wkPreview)
 }
 aj.send(params);
}
 
 
function ajaxPreviewFinish(el){
 sortables_init()
 if (window.createCollapseButtons){//en.wp
   createCollapseButtons()
   createNavigationBarToggleButton()
 } 
 if (window.ajaxPreviewExec) ajaxPreviewExec(el)
}
 
 
if (wgCanonicalNamespace!='Special' && (wgAction=='edit' || wgAction=='submit'))
 addOnloadHook(ajaxPreviewInit)

function addTranslateTab() {
    if ( wgNamespaceNumber == 0 ) { 
        var m = /^(.+)\/([^/]+)$/i.exec(wgTitle);
        var wBasePageName = m[1];
        var wSubPageName = m[2];
        addPortletLink('p-cactions', wgScript + '?title=' + wgTitle + '&action=edit&preload=' + wBasePageName, 'Translate', 'ca-translate');
    }
}
addOnloadHook(addTranslateTab);