2 * Bookmarklet to add annotation tools to AustLII cases.
4 * 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); */
8 load: function(url, cb) {
9 // Load an arbitrary JavaScript file and fire cb.
10 var s = document.createElement("script");
11 s.src = url; s.type = "text/javascript"; s.onload = cb;
12 document.getElementsByTagName('head').item(0).appendChild(s); return;
14 style: function(url) {
15 // dynamically add a new stylesheet
16 var s = document.createElement("link");
17 s.rel = "stylesheet"; s.type = "text/css"; s.href = url;
18 document.getElementsByTagName('head').item(0).appendChild(s); return;
21 if (window.location.host.slice(-14) != "austlii.edu.au" ||
22 window.location.pathname.slice(0,10) != "/au/cases/" ||
23 window.location.pathname.slice(-5) != ".html")
24 console.log("You're not looking at a case on AustLII.");
29 // Remove all but the newest script tag containing the queries passed in as arguments.
30 for (var i = 0; i < arguments.length; i++) {
31 var $els = $("head script[src*="+arguments[i]+"]");
32 var toRemove = $els.slice(0,-1);
33 if (toRemove.length > 0) {
35 console.log("Removed "+toRemove.length+" stale scripts matching '"+arguments[i]+"'...");
41 austlii.main = function() {
43 // grab austlii.css from the same directory as this JavaScript
44 var cssUrl = $('script[src$="austlii.js"]').attr('src').replace(/austlii\.js/, "austlii.css");
45 austlii.style(cssUrl);
53 austlii.cleanup = function() {
54 // remove text nodes ([] surrounding "Download", etc)
55 // var textNodes = $("body").contents().filter(function(){return this.nodeType == 3;}).remove();
56 // remove unnecessary <br />s
60 austlii.addMarkup = function() {
62 // add back to top around header
63 $("h1").eq(1).wrap('<a name="top">');
65 // highlight case title
66 $("center i").first().parent().addClass("citation");
68 // find and markup judges
69 // var judgeExp = /([A-Zc]+\s)([A-Zc,\s]+\sC?JJ?\.)/g;
70 var judgeExp = /([A-Z]{3})((?:[A-Z\s,]| )+C?J?J\.)/g
71 var replacementText = document.body.innerHTML.replace(judgeExp, '<a class="judge" name="$1">$1$2</a>');
72 document.body.innerHTML = replacementText;
76 austlii.paintMenu = function() {
79 var $menu = $('<div id="menu"><ol></ol></div>');
80 $("body").append($menu);
82 // add fixed link to headnote
83 $("#menu ol").append('<li><a href="#top">Headnote</a></li>')
85 // add jumplinks for each judge
86 $("a.judge").each(function(){
87 var $li = $('<li><a href="#' + this.name + '">' + $(this).html() + '</a></li>');
88 $("#menu ol").append($li);
91 // add fixed link to footnotes
92 $("#menu ol").append('<li><a href="#fn1">Footnotes</a></li>');
94 // add annotation features
95 //$menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
97 // enable smooth scrolling on anchors
98 austlii.load("http://splintax.ucc.asn.au/austlii/jquery.scrollTo-1.4.2-min.js", function() {
99 austlii.load("http://splintax.ucc.asn.au/austlii/jquery.localscroll-1.2.7-min.js", function() {
100 $.localScroll({duration: '200'});
104 // update current judge on scroll
105 $(window).scroll(function() {
106 $("#menu ol li a").each(function(){
107 // if the relevant link appearsmore than half way down the page
108 var $anchor = $('a[name="'+$(this).attr("href").slice(1)+'"]');
109 if ($(window).scrollTop() + $(window).height()/2 > $anchor.offset().top) {
110 austlii.$currentSection = $(this);
113 $("#menu ol li a").not(austlii.$currentSection).removeClass("current");
114 austlii.$currentSection.addClass("current");
115 }); $(window).scroll(); // and run it once
117 $("#trigger-highlight").click(austlii.highlight);
121 austlii.highlight = function(event) {
122 event.preventDefault();
123 var sel = document.getSelection();
124 if (sel.type == "Range") {
126 var range = sel.getRangeAt(0);
127 var $note = $('<span style="background-color: yellow;" />');
128 range.surroundContents($note[0]);
131 var $menu = $('<div style="position: absolute;" />');
132 var $removeLink = $('<a href="#">Remove</a>');
134 $menu.append($removeLink);
135 $removeLink.click((function($hilite) {
136 var text = $hilite.text();
137 $hilite.after(text).remove();
140 else {console.log("You haven't highlighted anything.")}
143 if (document.getElementsByTagName("script").length == 1) {
144 console.log("Loaded austlii.js v"+austlii.VERSION+".");
145 austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.init);
147 alert("austlii.js will not run when there are other scripts on the page.");