HistoryCrumbs.user.js

From CSSEMediaWiki
Revision as of 12:58, 26 July 2010 by Paul Clark (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Modifications

I just changed the crumbListShow() function to

  1. Show add the breadcrumbs as the first child element of the div with id='content', and
  2. Not show pages in the list multiple times, and
  3. Just save the first part of the title (using ' - ' as a delimiter).

The second modification is a bit messy, and some people probably wont want it.

I also added the UserScript block at the top and the top level function call.

/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
*/
// ==UserScript==
// @name		historyCrumb
// @namespace	http://no.name.space/
// @description	Display percentage of plays and statistics on user profile
// @include	http://wiki3.cosc.canterbury.ac.nz/*
// ==/UserScript==


var delim = '|'; // used in the cookie
var trailDelim = ' | '; // used in the breadcrumb
var titleDelim = ' - '; // we will take the first token of the title when it is split with this
var trailLength = 5;
var chunks;
var DAY = 24 * 60 * 60 * 1000;



doCrumbs();
function doCrumbs(){
  if(cookieTest('xxx')){
    crumbList = new CrumbList();
    if(getCookie('trailLinks')){
      var staleLinkCrumbs = getCookie('trailLinks').split(delim);
      var staleTextCrumbs = getCookie('trailText').split(delim);
      var startPos=
        (staleTextCrumbs.length<trailLength ||
        document.location==staleLinkCrumbs[staleLinkCrumbs.length-1])
        ?0:1;
      for(i=startPos;i<staleLinkCrumbs.length;i++){
        crumbList.add(staleLinkCrumbs[i],staleTextCrumbs[i]);
      }
    }
    if(document.location!=crumbList.links[crumbList.links.length-1]){
      crumbList.add(document.location,document.title);
    }
    setCookie('trailLinks',crumbList.links.join(delim),1);
    setCookie('trailText',crumbList.text.join(delim),1);
    crumbList.output();
  }
}
function CrumbList(){
  this.links=new Array();
  this.text=new Array();
  this.add = crumbListAdd;
  this.output = crumbListShow;
}
  function crumbListAdd(href,text){
    this.links[this.links.length]=href;
    this.text[this.text.length]=text;
  }
  function crumbListShow(){
  var contentDiv = document.getElementById('content');
  var linkParen = document.createElement('span');
  linkParen.id = "histroyCrumbs";
  
    for(var i in this.links){
	    var haveWeBeenHereBefore = 'false';
		for(var j in this.links){
		    if (haveWeBeenHereBefore != 'true' && j < i && this.links[j] == this.links[i]) {
				// we have been here before
				haveWeBeenHereBefore = 'true';
			} else if (this.links[j] == this.links[i] && j < i ) {
				// first time here, add the link
				haveWeBeenHereBefore = 'false';
			}
		}
		if (haveWeBeenHereBefore == 'false') {
			var linkElem = document.createElement('a');
			if(i==this.links.length-1){
			    // if this is the last link then it is the page we are currently on, dont bother adding an href
			    if (i != 0) {
			        linkParen.appendChild(document.createTextNode(trailDelim));
			    } 
			    linkElem.innerText = this.text[i].split(titleDelim)[0] ;
		    } else {
			    if (i != 0) {
			        linkParen.appendChild(document.createTextNode(trailDelim));
			    } 
				linkElem.href = this.links[i];
				linkElem.innerText = this.text[i].split(titleDelim)[0];
		    }
			linkParen.appendChild(linkElem);
		}
    }
	contentDiv.insertBefore(linkParen,contentDiv.firstChild);
  }


function cookieTest(name){
  try{
    setCookie(name,'true',1);
    chunks = document.cookie.split("; ");
    return (getCookie(name)=='true');
  }catch(e){
    return false;
  }
}

function getCookie(name) {
  var returnVal = null;
  for (var i in chunks) {
    var chunk = chunks[i].split("=");
    returnVal = (chunk[0] == name)
      ?unescape(chunk[1])
      :returnVal;
  }
  return returnVal;
}

function setCookie(name, value, days) {
  if (value != null && value != "" && days > 0){
    var expiry=
      new Date(new Date().getTime() + days * DAY);
    document.cookie=
      name +"="+ escape(value) +"; expires="
      + expiry.toGMTString();
    chunks = document.cookie.split("; ");
  }
}

function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

Personal tools