Misplaced Pages

User:Lupin/popups.js

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Lupin

This is an old revision of this page, as edited by Lupin (talk | contribs) at 17:15, 3 August 2005 (update to utf-aware hash function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 17:15, 3 August 2005 by Lupin (talk | contribs) (update to utf-aware hash function)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Lupin/popups.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Misplaced Pages:Bypass your cache.
////////////////////////////////////////////////////////////////////
// Popup stuff
////////////////////////////////////////////////////////////////////

document.write('<script type="text/javascript" src="http://en.wikipedia.org/search/?title=User:Lupin/md5-2.2alpha.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');

// this shouldn't be needed. maybe my cache needs purging...
function md5_hex(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }

document.write('<script type="text/javascript" src="http://en.wikipedia.org/search/?title=User:Lupin/overlib.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');

// we're not set up for interwiki stuff yet - only affect en links

var exceptions=/((title=|\/)Special:|section=)/
var contributions=/(title=|\/)Special:Contributions&target=(.*)/
var talk=/Talk:/i

var whichWiki=window.location.href.split('.org/');
var re; 
var titlebase;

if (whichWiki=='http://commons.wikimedia') {
  titlebase='http://commons.wikimedia.org/search/?title=';
  re=/*:\/\/commons\.wikimedia\.org\/w(iki\/|\/index\.php\?title=)(*)/
} else {
  titlebase='http://en.wikipedia.org/search/?title=';
  re=/*:\/\/en\.wikipedia\.org\/w(iki\/|\/index\.php\?title=)(*)/
} 

// alert ('whichWiki='+whichWiki+'\ntitlebase='+titlebase);

function wikiLink(article, action, text) {
  hint = myDecodeURI(article + '&action=' + action);
  return titledWikiLink(article, action, text, hint);
}

function titledWikiLink(article, action, text, title) {
  var base = titlebase +  article;
  var url=base;
  // no need to add action&view, and this confuses anchors
  if (action != 'view') url = base + '&action=' + action;

  var hint;
  if (title == null || title == '')
    hint = ''
  else 
    hint = 'title="' + title + '"'; 

  return '<a href="' + url + '"' + hint + '>' + text + '</a>';
}

function specialLink(article, specialpage, text) {
  var base = titlebase +  'Special:'+specialpage;
  var url = base + '&target=' + article;
  var hint = myDecodeURI(specialpage+':'+article) ;
  return '<a href="' + url + '" title="' + hint + '">' + text + '</a>';
}

function talkPage(article) {
  if (article.indexOf('Talk:') > -1 || article.indexOf('talk:') > -1 ) 
    return null;

  var i=article.indexOf(':');
  if (i == -1) return 'Talk:'+article;
  else return article.substring(0,i)+'_talk:' + article.substring(i+1);
}

function articleFromTalkPage(talkpage) {
  var i=talkpage.indexOf('Talk:');
  var j=talkpage.indexOf('_talk:');
  if ( i == -1 && j  == -1 ) 
    return null;
  if ( i > -1 ) return talkpage.substring(i+5);
  return talkpage.split('_talk:').join(':');
}

function userName(article) {
  var i=article.indexOf('User');
  var j=article.indexOf(':');
  if  (i != 0 || j < -1) return null;
  var k=article.indexOf('/');
  if (k==-1) return article.substring(j+1);
  else return article.substring(j+1,k);
}

function isInNamespace(article, namespace) {
  var i=article.indexOf(namespace+':');
  var j=article.indexOf(namespace+'_talk:');
  if  (i == -1 && j == -1) return false;
  return true;
}

function stripNamespace(article) {
  // this isn't very sophisticated 
  // it just removes everything up to the final :
  var list = article.split(':');
  return list;
}

function contribsLink(article, text) {
  return specialLink(userName(article), 'Contributions', text);
}

function imagePathComponent(article) {
  if (isInNamespace(article, 'Image')) {
    var stripped=stripNamespace(article);
    var forhash=myDecodeURI(stripped).split(' ').join('_');
    var hash=md5_hex(forhash);
    var pathcpt=hash.substring(0,1) + '/' + hash.substring(0,2) + '/';
    return pathcpt;
  }
  else return null;
}

function imageURL(article, wiki) {
  var imgurl=null;
  if (isInNamespace(article, 'Image')) {
    var pathcpt = imagePathComponent(article);
    var stripped=stripNamespace(article);
    var imgurl="http://upload.wikimedia.org/wikipedia/" + wiki + '/'
            + pathcpt + stripped;
  }
  return imgurl;
}

function imageThumbURL(article, wiki, width) {
  //  
  // eg http://upload.wikimedia.org/wikipedia/en/thumb/6/61/ 
  //           Rubiks_cube_solved.jpg/120px-Rubiks_cube_solved.jpg

  var imgurl=null;
  if (isInNamespace(article, 'Image')) {
    var pathcpt = imagePathComponent(article);
    var stripped=stripNamespace(article);
    var imgurl="http://upload.wikimedia.org/wikipedia/" + wiki + "/thumb/"
            + pathcpt + stripped + '/' + width +"px-" + stripped;
  }
  return imgurl;
}
   
var imageSources=new Array ();

imageSources.push(
                  {wiki: 'en',      thumb: true,  width: 60},
                  {wiki: 'en',      thumb: true,  width: 180},
                  {wiki: 'en',      thumb: true,  width: 120},
                  {wiki: 'en',      thumb: false, width: 0},
                  {wiki: 'commons', thumb: true,  width: 60},
                  {wiki: 'commons', thumb: true,  width: 180},
                  {wiki: 'commons', thumb: true,  width: 120},
                  {wiki: 'commons', thumb: false, width: 0}
                 );

//imageSources.push(
//                  {wiki: 'en',      thumb: true,  width: 120},
//                  {wiki: 'en',      thumb: false, width: 0}
//                 );


var imageArray=new Array();
var popupImageTimer=null;

function loadImages(article) {
 
  if(! isInNamespace(article, 'Image') ) {
    return;
  } 

  for (var i=0; i<imageSources.length; ++i) {

    var url;

    if (imageSources.thumb) 
      url=imageThumbURL(article, imageSources.wiki, imageSources.width);
    else 
      url=imageURL(article, imageSources.wiki);
    
    imageArray=new Image();
    imageArray.src=url;

  }
  if (popupImageTimer != null) {
    clearInterval(popupImageTimer);
    counter=0;
  }
  popupImageTimer=setInterval("checkImages()", 250);
  return;
}

function isImageOk(img)
{   
    // IE test
    if (!img.complete)
        return false;

    // gecko test
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        return false;

    // No other way of checking: assume it's ok.
    return true;
}

var counter=0;
var checkImagesTimer=null;
var loopcounter=0;

function checkImages() {

  if (checkImagesTimer!=null) {
    clearInterval(checkImagesTimer);
    checkImagesTimer=null;
    if (loopcounter > 10); {loopcounter=0; return;}
    loopcounter++;
  } else counter++;

  // document.title=counter + ',' + loopcounter;
  var popupImageStatus=
           document.getElementById("popupImageStatus"+popupImageId);
  if (popupImageStatus != null) {
    var status =  ( counter % 2 ) ? '*' : '.' ;
    popupImageStatus.innerHTML=status;
  }
  


  if (counter > 100) {counter = 0; clearInterval(popupImageTimer);}

  var popupImage=null;
  popupImage=document.getElementById("popupImage"+popupImageId);
  if (popupImage == null) {
    // this doesn't seem to happen any more in practise for some reason
    // still, I'll leave it in
    checkImagesTimer=setInterval("checkImages()",333);
    return;
  }
      
  // get the first image to successfully load
  // and put it in the popupImage
  for(var i = 0; i < imageArray.length; i++) {
    if(isImageOk(imageArray)) {

      clearInterval(popupImageTimer);

      popupImage.src=imageArray.src;
      if (popupImageStatus != null) popupImageStatus.innerHTML = '';
      // reset evil nonconstant globals
      var l=imageArray.length;
      imageArray=null; imageArray=new Array();
      popupImageTimer=null;

      // document.title='got '+md5_hex(popupImage.src)+' after '+counter+' tics';
      counter=0;
      loopcounter=0;
      return popupImage.src; 
    }
  }
}

var popupImageId=0;

function imageHTML(article) {
  var imgurl=''; // imageURL(article, 'en');
  var ret='';

  if (imgurl != null) {
    popupImageId++;
    ret += '<img src="' + imgurl + 
           '" width=60 align="right" valign="top" + id="popupImage' 
           + popupImageId + '"></img>';
  }
  return ret;
}

var kateBase='http://kohl.wikimedia.org/~kate/cgi-bin/count_edits' 
             + '?dbname=enwiki&user='

function kateLink(article, text) {
  return '<a href="' + kateBase + userName(article) + '" title="'
          + text + '">' + text + '</a>';
}


var decodeExtras = new Array ();
decodeExtras = {from: '%2C', to: ',' };
decodeExtras = {from: '_',   to: ' ' };
decodeExtras = {from: '%26',   to: '&' };


function myDecodeURI (str) {
  var ret=decodeURI(str);
  for (var i=0; i<decodeExtras.length; ++i) {
    var from=decodeExtras.from;
    var to=decodeExtras.to;
    ret=ret.split(from).join(to);
  }
  return ret;
}

// defaults
var dpopupDelay=1.5;
var dpopupFgColor='#CCCCFF';
var dpopupBgColor='#333399';

var popupDelay;
var popupFgColor;
var popupBgColor;

var dremoveTitles=true;
var  removeTitles;

function removeAnchor(article) {
  // is there a #? if not, we're done
  var i=article.indexOf('#');
  if (i == -1) return article;

  // head#tail
  var head = article.substring(0,i);
  var tail = article.substring(i+1);

  return head;
}

function mouseOverWikiLink() {
  // FIXME: should not generate the HTML until the delay has elapsed,
  //        and then popup immediately. Can be a CPU hog otherwise.

  var a=this;
  var h=a.href;

  var contribs=contributions.exec(h);
  if (contribs != null) {
    article='User:'+contribs;
  } else {
    var m=re.exec(h);
    article=m;
  }
  var hint=a.originalTitle;
  if (hint == '' || hint == null)
    hint = myDecodeURI(article);

  var html='';
  
  html +=imageHTML(article);

  html+='<b>';
  html+=titledWikiLink(article, 'view', myDecodeURI(article),hint);
  html+='</b>';

  html+='<span id="popupImageStatus'+popupImageId+'"></span>';

  // Get rid of anchor now
  article=removeAnchor(article);

  if (userName(article) != null) {
    html += ' ' + contribsLink(article, 'contribs');
    html += ' ' + kateLink(article, 'count');
  }

  html += '<br>' + wikiLink(article, 'edit', 'edit');
  html += ' ' + wikiLink(article, 'history', 'history');
  html += ' (' + wikiLink(article, 'unwatch', 'un') + ')';
  html += wikiLink(article, 'watch', 'watch');
  var t=talkPage(article);
  if (t != null) html += ' ' + wikiLink(t, 'view', 'talk') + 
                         ' ' + wikiLink(t, 'edit', 'editTalk') +
                         ' ' + wikiLink(t, 'edit&section=new', 'newTopic');
  var ta=articleFromTalkPage(article);
  if (ta != null) html +=' ' + 
                         wikiLink(article, 'edit&section=new', 'newTopic') +
                         ' ' + wikiLink(ta, 'view', 'article') + 
                         ' ' + wikiLink(ta, 'edit', 'editArticle');
                         
  html += '<br>' + specialLink(article, 'Whatlinkshere', 'whatLinksHere');
  html += ' ' + specialLink(article, 'Recentchangeslinked', 'relatedChanges');

  if (popupDelay==null) popupDelay=dpopupDelay;
  if (popupFgColor==null) popupFgColor=dpopupFgColor;
  if (popupBgColor==null) popupBgColor=dpopupBgColor;

  overlib(html, STICKY, MOUSEOFF, WRAP, CELLPAD, 5, 
	  OFFSETX, 2, OFFSETY, 2, DELAY, popupDelay*1000,
          FGCOLOR, popupFgColor, BGCOLOR, popupBgColor);

  loadImages(article);
}

function setupTooltips() {
  var anchors=document.getElementsByTagName('A');
  // alert(anchors.length + 'anchors');
  var s='';
  
  if (removeTitles==null) removeTitles=dremoveTitles;

  for (var i=0; i<anchors.length; ++i)
  {
    var a=anchors;
    var h=a.href;
    var contribs=contributions.exec(h);
    var exc=exceptions.exec(h);
    var m=re.exec(h);
    if (contribs != null || (exc == null && m != null) ) {
      a.onmouseover=mouseOverWikiLink;
      a.onmouseout=function () {  if ( o3_showingsticky == 0 ) cClick(); }
      if (removeTitles) {
        a.originalTitle=a.title;
        a.title='';
      }
    }
  }
}

////////////////////////////////////////////////////////////////////
// Run things
////////////////////////////////////////////////////////////////////

if (window.addEventListener)
  window.addEventListener("load",setupTooltips,false);
else if (window.attachEvent) 
  window.attachEvent("onload",setupTooltips);
else {
  window._old_ABCD_onload = window.onload;
  window.onload = function() {
    window._old_ABCD_onload();
    setupTooltips();
  }
}