1908d47e00b827cc11e68f42509f2ef3632bfe02
[sjy/austlii.git] / austlii.js
1 /* austlii.js
2  * Bookmarklet to add annotation tools to AustLII cases.
3  * Invoke with: 
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); */
5
6 window.austlii = {
7     VERSION: '0.1',
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;
13     },
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;
19     },
20     init: function() {
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.");
25         else
26             austlii.main();
27     },
28     prune: function() {
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) {
34                 toRemove.remove();
35                 console.log("Removed "+toRemove.length+" stale scripts matching '"+arguments[i]+"'...");
36             }
37         }
38     },
39 };
40
41 austlii.main = function() {
42
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);
46
47     austlii.cleanup();
48     austlii.addMarkup();
49     austlii.paintMenu();
50
51 };
52
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
57     $("br").remove();
58 };
59
60 austlii.addMarkup = function() {
61
62     // add back to top around header
63     $("h1").eq(1).wrap('<a name="top">');
64
65     // highlight case title
66     $("center i").first().parent().addClass("citation");
67
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,]|&nbsp;)+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;
73
74 };
75
76 austlii.paintMenu = function() {
77
78     // create menu 
79     var $menu = $('<div id="menu"><ol></ol></div>');
80     $("body").append($menu);
81
82     // add fixed link to headnote
83     $("#menu ol").append('<li><a href="#top">Headnote</a></li>')
84
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);
89     });
90
91     // add fixed link to footnotes
92     $("#menu ol").append('<li><a href="#fn1">Footnotes</a></li>');
93
94     // add annotation features
95     //$menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
96
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'});
101         });
102     });
103
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);
111             }
112         });
113         $("#menu ol li a").not(austlii.$currentSection).removeClass("current");
114         austlii.$currentSection.addClass("current");
115     }); $(window).scroll(); // and run it once
116
117     $("#trigger-highlight").click(austlii.highlight);
118
119 };
120
121 austlii.highlight = function(event) {
122     event.preventDefault();
123     var sel = document.getSelection();
124     if (sel.type == "Range") {
125         // add highlight
126         var range = sel.getRangeAt(0);
127         var $note = $('<span style="background-color: yellow;" />');
128         range.surroundContents($note[0]);
129
130         console.log(sel);
131         var $menu = $('<div style="position: absolute;" />');
132         var $removeLink = $('<a href="#">Remove</a>');
133         $note.append($menu);
134         $menu.append($removeLink);
135         $removeLink.click((function($hilite) {
136             var text = $hilite.text();
137             $hilite.after(text).remove();
138         })($(this)));
139     }
140     else {console.log("You haven't highlighted anything.")}
141 };
142
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);
146 } else {
147     alert("austlii.js will not run when there are other scripts on the page.");
148 }

UCC git Repository :: git.ucc.asn.au