Misplaced Pages

User:Lupin/popups.js: Difference between revisions

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 Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 22:52, 2 August 2005 view sourceLupin (talk | contribs)19,513 editsmNo edit summary← Previous edit Revision as of 23:02, 2 August 2005 view source Lupin (talk | contribs)19,513 edits exclude section editing links, handle #anchors properly, less moronic namespace removal, link from talk page links to corresponding articlesNext edit →
Line 10: Line 10:


var re=/*:\/\/en\.wikipedia\.org\/w(iki\/|\/index\.php\?title=)(*)/ var re=/*:\/\/en\.wikipedia\.org\/w(iki\/|\/index\.php\?title=)(*)/

var exceptions=/(title=|\/)Special:/
// omit Special and section editing links

var exceptions=/((title=|\/)Special:|section=)/
var contributions=/(title=|\/)Special:Contributions&target=(.*)/ var contributions=/(title=|\/)Special:Contributions&target=(.*)/
var talk=/Talk:/i var talk=/Talk:/i
Line 22: Line 25:
function titledWikiLink(article, action, text, title) { function titledWikiLink(article, action, text, title) {
base = titlebase + article; base = titlebase + article;
url = base + '&action=' + action; url=base;
// no need to add action&view, and this confuses anchors
if (action != 'view') url = base + '&action=' + action;
if (title == null || title == '') if (title == null || title == '')
hint = '' hint = ''
Line 47: Line 52:


} }

function articleFromTalkPage(talkpage) {
i=talkpage.indexOf('Talk:');
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) { function userName(article) {
Line 67: Line 82:
// this isn't very sophisticated // this isn't very sophisticated
// it just removes everything up to the final : // it just removes everything up to the final :
ret = article; list = article.split(':');
return list;
i=null;
while ( i != -1 ) {
i=ret.indexOf(':');
ret=ret.substring(i+1);
}
return ret;
} }


Line 136: Line 146:
var dremoveTitles=true; var dremoveTitles=true;
var removeTitles; var removeTitles;


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

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

return head;
}


function mouseOverWikiLink() { function mouseOverWikiLink() {
Line 159: Line 182:
html += titledWikiLink(article, 'view', myDecodeURI(article),hint); html += titledWikiLink(article, 'view', myDecodeURI(article),hint);
html +='</b>'; html +='</b>';

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

if (userName(article) != null) { if (userName(article) != null) {
html += ' ' + contribsLink(article, 'contribs'); html += ' ' + contribsLink(article, 'contribs');
Line 170: Line 197:
if (t != null) html += ' ' + wikiLink(t, 'view', 'talk') + if (t != null) html += ' ' + wikiLink(t, 'view', 'talk') +
' ' + wikiLink(t, 'edit', 'editTalk') + ' ' + wikiLink(t, 'edit', 'editTalk') +
' ' + wikiLink(t, 'edit&section=new', 'newTopic'); html += '<br>' + specialLink(article, 'Whatlinkshere', 'whatLinksHere'); ' ' + wikiLink(t, 'edit&section=new', 'newTopic');

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'); html += ' ' + specialLink(article, 'Recentchangeslinked', 'relatedChanges');



Revision as of 23:02, 2 August 2005

////////////////////////////////////////////////////////////////////
// Popup stuff
////////////////////////////////////////////////////////////////////

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

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

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

var re=/*:\/\/en\.wikipedia\.org\/w(iki\/|\/index\.php\?title=)(*)/

// omit Special and section editing links

var exceptions=/((title=|\/)Special:|section=)/
var contributions=/(title=|\/)Special:Contributions&target=(.*)/
var talk=/Talk:/i
var titlebase='http://en.wikipedia.org/search/?title=';

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

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

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

function specialLink(article, specialpage, text) {
  base = titlebase +  'Special:'+specialpage;
  url = base + '&target=' + article;
  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;

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

}

function articleFromTalkPage(talkpage) {
  i=talkpage.indexOf('Talk:');
  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) {
  i=article.indexOf('User');
  j=article.indexOf(':');
  if  (i != 0 || j < -1) return null;
  k=article.indexOf('/');
  if (k==-1) return article.substring(j+1);
  else return article.substring(j+1,k);
}

function isInNamespace(article, namespace) {
  i=article.indexOf(namespace+':');
  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 :
  list = article.split(':');
  return list;
}

function imageHTML(article) {
  if (isInNamespace(article, 'Image')) {
    stripped=stripNamespace(article);
    forhash=myDecodeURI(stripped).split(' ').join('_');
    hash=hex_md5(forhash);

    // pathComponent is the mysterious bit of the image url, like 4/4a/

    pathComponent=hash.substring(0,1) + '/' + hash.substring(0,2) + '/';
    imgurl="http://upload.wikimedia.org/wikipedia/en/" + pathComponent + stripped;
  }
  else imgurl=null;

  ret='';

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

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

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) {
  ret=decodeURI(str);
  for ( i=0; i<decodeExtras.length; ++i) {
    from=decodeExtras.from;
    to=decodeExtras.to;
    ret=ret.split(from).join(to);
  }
  return ret;
}

// defaults
var dpopupDelay=2;
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
  i=article.indexOf('#');
  if (i == -1) return article;

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

  return head;
}

function mouseOverWikiLink() {
  a=this;
  h=a.href;

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

  hint=a.originalTitle;
  if (hint == '' || hint == null)
    hint = myDecodeURI(article);
  
  html='';
  
  html +=imageHTML(article);
  html +='<b>';
  html += titledWikiLink(article, 'view', myDecodeURI(article),hint);
  html +='</b>';

  // 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');
  t=talkPage(article);
  if (t != null) html += ' ' + wikiLink(t, 'view', 'talk') + 
                         ' ' + wikiLink(t, 'edit', 'editTalk') +
                         ' ' + wikiLink(t, 'edit&section=new', 'newTopic');        

  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);
}

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

  for (i=0; i<anchors.length; ++i)
  {
    a=anchors;
    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();
  }
}