e2ce166b9b1e75095ae2f8306e2280a518444bb3
[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 replacementText = document.body.innerHTML.replace(judgeExp, '<a class="judge" name="$1">$1$2</a>');
71     document.body.innerHTML = replacementText;
72
73 };
74
75 austlii.paintMenu = function() {
76
77     // create menu 
78     var $menu = $('<div id="menu"><ol></ol></div>');
79     $("body").append($menu);
80
81     // add fixed link to headnote
82     $("#menu ol").append('<li><a href="#top">Headnote</a></li>')
83
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);
88     });
89
90     // add fixed link to footnotes
91     $("#menu ol").append('<li><a href="#fn1">Footnotes</a></li>');
92
93     // add annotation features
94     //$menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
95
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'});
100         });
101     });
102
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);
110             }
111         });
112         $("#menu ol li a").not(austlii.$currentSection).removeClass("current");
113         austlii.$currentSection.addClass("current");
114     }); $(window).scroll(); // and run it once
115
116     $("#trigger-highlight").click(austlii.highlight);
117
118 };
119
120 austlii.highlight = function(event) {
121     event.preventDefault();
122     var sel = document.getSelection();
123     if (sel.type == "Range") {
124         // add highlight
125         var range = sel.getRangeAt(0);
126         var $note = $('<span style="background-color: yellow;" />');
127         range.surroundContents($note[0]);
128
129         console.log(sel);
130         var $menu = $('<div style="position: absolute;" />');
131         var $removeLink = $('<a href="#">Remove</a>');
132         $note.append($menu);
133         $menu.append($removeLink);
134         $removeLink.click((function($hilite) {
135             var text = $hilite.text();
136             $hilite.after(text).remove();
137         })($(this)));
138     }
139     else {console.log("You haven't highlighted anything.")}
140 };
141
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);
145 } else {
146     alert("austlii.js will not run when there are other scripts on the page.");
147 }

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