source: other-projects/hathitrust/wcsa/vol-checker/WebContent/HT-HTRC_Mashup.user.js@ 31330

Last change on this file since 31330 was 31330, checked in by davidb, 7 years ago

Initial cut a files that explain how to install the user-script

File size: 6.8 KB
Line 
1// ==UserScript==
2// @name HT-HTRC Mashup
3// @author David Bainbridge
4// @namespace org.hathitrust.researchcenter.mashup
5// @description Hybrid interface between Hathitrust and HTRC
6// @version 1.0
7// @grant GM_xmlhttpRequest
8// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js
9// @match https://www.hathitrust.org/*
10// @match https://babel.hathitrust.org/cgi/ls*
11// @match https://babel.hathitrust.org/cgi/mb*
12// ==/UserScript==
13
14// grant GM_xmlhttpRequest
15// grant GM_getValue
16// grant GM_setValue
17// grant GM_addStyle
18
19
20function mashupInit() {
21
22 var $fieldset = $('form fieldset').first();
23 var $search_button = $fieldset.find('button');
24 $search_button.css("width","47px");
25 $search_button.css("right","-55px");
26 $search_button.css("font-size","95%");
27 $search_button.find('span').first().hide();
28
29 $fieldset.append('<button id="htrc-bw" class="button search" style="width:47px; right: -105px; background-image:url(http://www.cs.waikato.ac.nz/~davidb/htrc-mashup/bookworm.png);" title="Bookworm"></button>');
30
31 var $bw_button = $('#htrc-bw');
32 $bw_button.click(function(event) {
33 var input=$('#q1-input').val();
34 if (input === "") {
35 return;
36 }
37 event.preventDefault();
38
39 var url_front = "https://bookworm.htrc.illinois.edu/#?%7B%22counttype%22%3A%22WordsPerMillion%22%2C%22search_limits%22%3A%5B";
40 var url_end = "%5D%7D";
41
42 var word_front = "%7B%22word%22%3A%5B%22";
43 var word_end = "%22%5D%2C%22date_year%22%3A%7B%22%24gte%22%3A1750%2C%22%24lte%22%3A1923%7D%7D";
44
45 var input_words = input.split(' ');
46 var word_len = input_words.length;
47
48 var full_url = url_front;
49
50 for (var i=0; i<word_len; i++) {
51 var word = input_words[i];
52 if (i>0) { full_url += "%2C"; } // append comma
53 full_url += word_front + word + word_end;
54 }
55
56 full_url += url_end;
57
58 // window.location.href = "https://bookworm.htrc.illinois.edu/#?%7B%22search_limits%22%3A%5B%7B%22word%22%3A%5B%22"+input+"%22%5D%2C%22date_year%22%3A%7B%22%24gte%22%3A1750%2C%22%24lte%22%3A1923%7D%7D%5D%7D";
59
60 window.location.href = full_url;
61
62 });
63
64 var url = window.location.href;
65 var home_url_test = url.replace(/^https:\/\/www.hathitrust.org\//,"");
66 //console.log("*** home urltest = " + home_url_test);
67 if (home_url_test !== "") {
68 var $login_button = $('#login-button');
69 if ($login_button.length) {
70 $login_button.css('font-size','95%');
71 $login_button.css('right','-45px');
72 $login_button.css('width','80px');
73 }
74 }
75
76
77
78 /*
79 each(function() {
80
81 $('form button').each(function() {
82 console.log("*** this = " + this + "jquery this = " + $(this).val());
83 });
84 */
85
86
87}
88
89function mashupAugmentResults()
90{
91 //console.log("**** mashupAugmentResults() called");
92 var $results_a = $('#results_A');
93 var results_a_exists = $results_a.length;
94
95 if (!results_a_exists) {
96 // Not a main search result page
97 // => See if part of the collections page area
98 // => If it is, set things up so it will use that instead
99 $results_a = $('#form1');
100 results_a_exists = $results_a.length;
101 }
102
103 if (results_a_exists) {
104 //console.log("*** results_a = " + $results_a.html());
105
106 var ids = [];
107 //var file_safe_ids = [];
108
109 $results_a.find('.result').each(function() {
110 var $result_access_link = $(this).find('.result-access-link');
111 console.log("*** result access link = " + $result_access_link.html());
112
113 var $id_link = $result_access_link.find('ul>li>a').first();
114 //console.log("*** id_link = " + $id_link.html());
115 var data_clicklog = $id_link.attr("data_clicklog");
116 //console.log("*** data clicklog= " + data_clicklog);
117
118 var data_json_str = data_clicklog.replace(/^[a-z]+\|/,"");
119 //console.log("*** data json= " + data_json_str);
120
121 var id = null;
122 if (data_json_str !== "") {
123 var data_json = JSON.parse(data_json_str);
124 id = data_json.id;
125 //console.log("*** (catalog extracted) id= '" + id + "'");
126 }
127 else {
128 var $second_id_link = $result_access_link.find('ul>li>a').eq(1);
129 console.log("*** 2nd link len = " + $second_id_link);
130 var id_href = $second_id_link.attr("href");
131 console.log("*** id href = " + id_href);
132 id = id_href.replace(/^.*id=/,"");
133 }
134
135 ids.push(id);
136 //var file_safe_id = id.replace(/:/g,"+").replace(/\//g,"=");
137 //file_safe_ids.push(file_safe_id);
138
139 $result_access_link.attr("id","htrc-mashup-" + id);
140 //$result_access_link.attr("id","htrc-mashup-" + file_safe_id);
141
142 });
143
144 var ids_str = ids.join(",");
145 //var file_safe_ids_str = file_safe_ids.join(",");
146
147 //console.log("*** ids= " + JSON.stringify(ids));
148 //console.log("*** ids_str = " + ids_str);
149
150 console.log("**** Away to request: " + "http://bedrock.resnet.cms.waikato.ac.nz:8686/vol-checker/VolumeCheck?ids=" + encodeURIComponent(ids_str));
151
152
153
154
155 GM_xmlhttpRequest ({
156 method: "GET",
157 url: "http://bedrock.resnet.cms.waikato.ac.nz:8686/vol-checker/VolumeCheck?ids=" + encodeURIComponent(ids_str),
158 //url: "http://localhost:8080/htrc-ef-exists/VolumeCheck?ids=" + encodeURIComponent(file_safe_ids_str),
159 //headers: {
160 //"Content-Type": "application/x-www-form-urlencoded"
161 // },
162 onload: function (response) {
163
164 console.log("Adding in HTRC cross-checks");
165 var text = response.responseText;
166 //console.log ( "Response text: " + text);
167 var ids_exist = JSON.parse(text);
168
169 for (var k in ids_exist) {
170 //console.log("*** k = '" + k + "'");
171
172 //var id_str = '#htrc-mashup-'+k;
173 //console.log("*** id str = " + id_str);
174 // var $id_div = $(id_str);
175
176 //var $id_div = $('#'+'htrc-mashup-'+k);
177 var id_div = document.getElementById('htrc-mashup-'+k);
178 //console.log("*** id_div = " + id_div);
179 var $id_div = $(id_div);
180
181 if (ids_exist[k]) {
182 $id_div.find("ul").append("<li title=\""+k+"\" style=\"color: #924a0b;\"><a>Download Extracted Features</a></li>"); // ✓
183 }
184 else {
185 // ✗, ✘
186 $id_div.find("ul").append("<li title=\""+k+"\" style=\"color: red;\">HTRC unfriendly ✘</li>");
187 }
188
189 //console.log("*** id div len = " + $id_div.length);
190
191 //$.append("<li>HTRC friendly</li>");
192 /*
193 if (ids_exist.hasOwnProperty(k)) {
194 //console.log("*** processing id: " + k);
195 //$('#htrc-mashup-'+k).css("background-color:","red");
196 $('#htrc-mashup-'+k).append("<p>HTRC friendly</p>");
197 }*/
198 }
199
200 }
201 });
202
203 //console.log("*** GM httpRequest made ");
204 }
205}
206
207mashupInit();
208
209mashupAugmentResults();
210
Note: See TracBrowser for help on using the repository browser.