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 replacementText = document.body.innerHTML.replace(judgeExp, '<a class="judge" name="$1">$1$2</a>');
71 document.body.innerHTML = replacementText;
75 austlii.paintMenu = function() {
78 var $menu = $('<div id="menu"><ol></ol></div>');
79 $("body").append($menu);
81 // add fixed link to headnote
82 $("#menu ol").append('<li><a href="#top">Headnote</a></li>')
84 // add jumplinks for each judge
85 $("a.judge").each(function(){
86 var $li = $('<li><a href="#' + this.name + '">' + $(this).html() + '</a></li>');
87 $("#menu ol").append($li);
90 // add fixed link to footnotes
91 $("#menu ol").append('<li><a href="#fn1">Footnotes</a></li>');
93 // add annotation features
94 //$menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
96 // enable smooth scrolling on anchors
97 austlii.load("http://splintax.ucc.asn.au/austlii/jquery.scrollTo-1.4.2-min.js", function() {
98 austlii.load("http://splintax.ucc.asn.au/austlii/jquery.localscroll-1.2.7-min.js", function() {
99 $.localScroll({duration: '200'});
103 // update right menu on scroll
104 $(window).scroll(function() {
105 $("#menu ol li a").each(function(){
106 // if the relevant link appearsmore than half way down the page
107 var $anchor = $('a[name="'+$(this).attr("href").slice(1)+'"]');
108 if ($(window).scrollTop() + $(window).height()/2 > $anchor.offset().top) {
109 austlii.$currentSection = $(this);
112 $("#menu ol li a").not(austlii.$currentSection).removeClass("current");
113 austlii.$currentSection.addClass("current");
114 }); $(window).scroll(); // and run it once
116 $("#trigger-highlight").click(austlii.highlight);
120 austlii.highlight = function(event) {
121 event.preventDefault();
122 var sel = document.getSelection();
123 if (sel.type == "Range") {
125 var range = sel.getRangeAt(0);
126 var $note = $('<span style="background-color: yellow;" />');
127 range.surroundContents($note[0]);
130 var $menu = $('<div style="position: absolute;" />');
131 var $removeLink = $('<a href="#">Remove</a>');
133 $menu.append($removeLink);
134 $removeLink.click((function($hilite) {
135 var text = $hilite.text();
136 $hilite.after(text).remove();
139 else {console.log("You haven't highlighted anything.")}
142 if (document.getElementsByTagName("script").length == 1) {
143 console.log("Loaded austlii.js v"+austlii.VERSION+".");
144 austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.init);
146 alert("austlii.js will not run when there are other scripts on the page.");