Initial commit.
[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     main: function() {
21         austlii.prune("austlii", "jquery");
22         if (window.location.host.slice(-14) != "austlii.edu.au" ||
23             window.location.pathname.slice(0,10) != "/au/cases/" ||
24             window.location.pathname.slice(-5) != ".html")
25             console.log("You're not looking at a case on AustLII.");
26         else
27             austlii.paintMenu();
28     },
29     prune: function() {
30         // Remove all but the newest script tag containing the queries passed in as arguments.
31         for (var i = 0; i < arguments.length; i++) {
32             var $els = $("head script[src*="+arguments[i]+"]");
33             var toRemove = $els.slice(0,-1);
34             if (toRemove.length > 0) {
35                 toRemove.remove();
36                 console.log("Removed "+toRemove.length+" stale scripts matching '"+arguments[i]+"'...");
37             }
38         }
39     },
40 };
41
42 austlii.paintMenu = function() {
43
44     austlii.style("http://splintax.ucc.asn.au/austlii/austlii.css");
45
46     // remove text nodes ([] surrounding "Download", etc)
47     var textNodes = $("body").contents().filter(function(){return this.nodeType == 3;}).remove();
48     // hide other unnecessary menu elements
49     var $els = $("body").children(); // excludes text nodes
50     var index = 0;
51     while ($els[index].tagName != "H1") {
52         $els[index].style.display = "none"; index++;
53     }
54     // add back to top
55     $("h1").eq(1).wrap('<a name="top">');
56
57     // highlight case title
58     $("center i").first().parent().addClass("citation");
59
60     // find and markup judges
61     var judgeExp = /([A-Zc, ]+)((&nbsp;| )C?JJ?\.)/g;
62     var replacementText = document.body.innerHTML.replace(judgeExp, '<a class="judge" name="$1">$1$2</a>');
63     document.body.innerHTML = replacementText;
64
65     // add menu
66     var $menu = $('<div id="menu"><a href="#top">Top</a></div>');
67     // add jumplinks for each judge
68     var $judges = $('<ol id="judges"></ol>');
69     $("a.judge").each(function(){
70         var $li = $('<li><a href="#' + this.name + '">' + $(this).html() + '</a></li>');
71         $judges.append($li);
72     }); $menu.append($judges);
73     // add annotation features
74     $menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
75     $("body").append($menu);
76
77     // enable smooth scrolling on anchors
78     austlii.load("http://splintax.ucc.asn.au/cases/jquery.scrollTo-1.4.2-min.js", function() {
79         austlii.load("http://splintax.ucc.asn.au/cases/jquery.localscroll-1.2.7-min.js", function() {
80             $.localScroll({duration: '200'});
81         });
82     });
83
84
85     $("#trigger-highlight").click(austlii.highlight);
86
87 };
88
89 austlii.highlight = function(event) {
90     event.preventDefault();
91     var sel = document.getSelection();
92     if (sel.type == "Range") {
93         // add highlight
94         var range = sel.getRangeAt(0);
95         var $note = $('<span style="background-color: yellow;" />');
96         range.surroundContents($note[0]);
97
98         console.log(sel);
99         var $menu = $('<div style="position: absolute;" />');
100         var $removeLink = $('<a href="#">Remove</a>');
101         $note.append($menu);
102         $menu.append($removeLink);
103         $removeLink.click((function($hilite) {
104             var text = $hilite.text();
105             $hilite.after(text).remove();
106         })($(this)));
107     }
108     else {console.log("You haven't highlighted anything.")}
109 };
110
111 console.log("Loaded austlii.js v"+austlii.VERSION+".");
112 austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.main);

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