X-Git-Url: https://git.ucc.asn.au/?p=sjy%2Faustlii.git;a=blobdiff_plain;f=austlii.js;h=9cd2a0c79be8da1eca5b27f9a045750ebe0da90c;hp=e506188145da3078030dcef3a9567a4d7b01c3e6;hb=HEAD;hpb=f36beed7e8739a9d037fa04c947fffb0c9277fb2 diff --git a/austlii.js b/austlii.js index e506188..9cd2a0c 100644 --- a/austlii.js +++ b/austlii.js @@ -1,10 +1,10 @@ /* austlii.js - * Bookmarklet to add annotation tools to AustLII cases. - * Invoke with: + * Author: Scott Young + * Bookmarklet to add annotation tools to AustLII cases. Invoke with: * javascript:var d=document;var s=d.createElement("script");s.src="http://splintax.ucc.asn.au/austlii/austlii.js";s.type="text/javascript";d.getElementsByTagName('head').item(0).appendChild(s); */ +// create a namespace for this tool window.austlii = { - VERSION: '0.1', load: function(url, cb) { // Load an arbitrary JavaScript file and fire cb. var s = document.createElement("script"); @@ -17,75 +17,126 @@ window.austlii = { s.rel = "stylesheet"; s.type = "text/css"; s.href = url; document.getElementsByTagName('head').item(0).appendChild(s); return; }, - main: function() { - austlii.prune("austlii", "jquery"); - if (window.location.host.slice(-14) != "austlii.edu.au" || - window.location.pathname.slice(0,10) != "/au/cases/" || - window.location.pathname.slice(-5) != ".html") - console.log("You're not looking at a case on AustLII."); - else - austlii.paintMenu(); - }, - prune: function() { - // Remove all but the newest script tag containing the queries passed in as arguments. - for (var i = 0; i < arguments.length; i++) { - var $els = $("head script[src*="+arguments[i]+"]"); - var toRemove = $els.slice(0,-1); - if (toRemove.length > 0) { - toRemove.remove(); - console.log("Removed "+toRemove.length+" stale scripts matching '"+arguments[i]+"'..."); - } - } - }, + url: function(relative_path) { + // return url relative to the source of this file, eg. http://splintax.ucc.asn.au/austlii/ + var prefix = $('script[src$="austlii.js"]').attr('src').replace(/austlii\.js/, ""); + return prefix + relative_path; + } }; -austlii.paintMenu = function() { +// Initialises once jQuery is loaded. +austlii.main = function() { + austlii.style(austlii.url("austlii.css")); // inject css + // set viewport width so iPad doesn't zoom out + $("head").append(''); + $("a").slice(10,16).css({"margin-right": "1em"}); // space out "Database Search", ... links - austlii.style("http://splintax.ucc.asn.au/austlii/austlii.css"); + austlii.markup(); + austlii.menu(); + $(window).scroll(austlii.scrollHandler).scroll(); - // remove text nodes ([] surrounding "Download", etc) - var textNodes = $("body").contents().filter(function(){return this.nodeType == 3;}).remove(); - // hide other unnecessary menu elements - var $els = $("body").children(); // excludes text nodes - var index = 0; - while ($els[index].tagName != "H1") { - $els[index].style.display = "none"; index++; - } - // add back to top - $("h1").eq(1).wrap(''); + // enable smooth scrolling on anchors + // TODO: doing this synchronously makes for slow injection on mobile browsers + austlii.load(austlii.url("jquery.scrollTo-1.4.2-min.js"), function() { + austlii.load(austlii.url("jquery.localscroll-1.2.7-min.js"), function() { + $.localScroll({duration: '200'}); + }); + }); + + // TODO: user annotations + $("#trigger-highlight").click(austlii.highlight); +} + +// Cleans up page and adds semantic markup. +austlii.markup = function() { + // remove cruft + $("br").remove(); + $('img[alt="AustLII"]').detach().prependTo("body"); + $("table").remove(); - // highlight case title - $("center i").first().parent().addClass("citation"); + $("h1").wrap(''); // add back-to-top + $('a[name="fn1"]').addClass("section"); // mark beginning of footnotes // find and markup judges - var judgeExp = /([A-Zc, ]+)(( | )C?JJ?\.)/g; - var replacementText = document.body.innerHTML.replace(judgeExp, '$1$2'); + var judgeExp = /([A-Zc]{3})((?:[A-Z\s,]| )+C?J?J\.)/g + /* This regex is confusing and I keep breaking it. Remember: + * [A-Zc] is used to match McHUGH J. + * Since we're matching innerHTML   is not part of \s. + * The first 3 characters are captured, hopefully because they are ASCII non-whitespace + * suitable for use in the . */ + var replacementText = document.body.innerHTML.replace(judgeExp, '$1$2'); document.body.innerHTML = replacementText; +}; - // add menu - var $menu = $(''); - // add jumplinks for each judge - var $judges = $('
    '); - $("a.judge").each(function(){ - var $li = $('
  1. ' + $(this).html() + '
  2. '); - $judges.append($li); - }); $menu.append($judges); - // add annotation features - $menu.append($('Highlight')); - $("body").append($menu); +// Updates responsive menu whenever the page is scrolled. +austlii.scrollHandler = function() { + var currentPosition = $(window).scrollTop() + $(window).height()/2; + + var $anchors = $("#container a.section"); + var $links = $("#menu ol li a"); + var $pars = $("#container ol li").not("ol ol li"); - // enable smooth scrolling on anchors - austlii.load("http://splintax.ucc.asn.au/cases/jquery.scrollTo-1.4.2-min.js", function() { - austlii.load("http://splintax.ucc.asn.au/cases/jquery.localscroll-1.2.7-min.js", function() { - $.localScroll({duration: '200'}); - }); - }); + // remove existing section highlight and citation + $links.removeClass("current"); + $("#menu .pinpoint").remove(); + // find the current document section + var $anchor = $anchors.filter(function(){ + // is this anchor above the fold? + return currentPosition > $(this).offset().top; + }).last(); // current section = last anchor above fold + // highlight the link to the current document section + $links.filter('[href="#' + $anchor.attr("name") + '"]').addClass("current"); - $("#trigger-highlight").click(austlii.highlight); + // check whether we're in the judgment body + if ( currentPosition > $pars.first().offset().top && + currentPosition < $pars.last().offset().top ) { + // we are, so find the current paragraph and add a pinpoint citation + var $par = $pars.filter(function(){ + // is this paragraph above the fold? + return currentPosition > $(this).offset().top; + }).last(); // current paragraph = last paragraph above fold + if ( $par.val() != 0 ) { // don't add unofficial paragraph citations on old cases + // TODO: make it work anyway, but add a warning? + var $pinpoint = $(' at ['+ $par.val() +']'); + $("#menu a.current").after($pinpoint); // add citation + } + } +}; +// Create the responsive menu. +austlii.menu = function() { + // wrap the source body content in a container + $("body").children().wrapAll('
    '); + + // create and prepend menu to the page + var $menu = $(''); + $('

    '+ $("h2").first().html() +'

    ').prependTo($menu); // add header + $('img[alt="AustLII"]').detach().prependTo($menu); + $menu.prependTo("body"); + + // create and insert links in the menu + var $links = $('
      '); + // headnote + $links.append('
    1. Headnote·
    2. ') + // judgments + var $judges = $("a.section").slice(1,-1); + $judges.each(function(i){ + var li = '
    3. ' + $(this).html().slice(0,-1) + ''; + if ( i < $judges.length ) + li += '·' + li += '
    4. '; + $links.append(li); + }); + // footnotes + $links.append('
    5. Footnotes
    6. '); + $menu.append($links); + + // add annotation features + //$menu.append($('Highlight')); }; +// FIXME Highlight the current selection. austlii.highlight = function(event) { event.preventDefault(); var sel = document.getSelection(); @@ -108,5 +159,17 @@ austlii.highlight = function(event) { else {console.log("You haven't highlighted anything.")} }; -console.log("Loaded austlii.js v"+austlii.VERSION+"."); -austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.main); +// don't run on other sites +if (window.location.host.slice(-14) != "austlii.edu.au" || + window.location.pathname.slice(0,10) != "/au/cases/" || + window.location.pathname.slice(-5) != ".html") + console.error("You're not looking at a case on AustLII."); + +else if (document.getElementsByTagName("script").length > 1) + console.error("austlii.js will not run when there are other scripts on the page."); + +else { + console.log("Loaded austlii.js."); + // load jQuery and initialise the bookmarklet + austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.main); +}