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

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

Support for Catalog page added

File size: 10.0 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// @match https://catalog.hathitrust.org/Search/Home*
13// ==/UserScript==
14
15
16var https_servlet_url="https://bedrock.resnet.cms.waikato.ac.nz/vol-checker/";
17
18function mashupInit() {
19
20 var $fieldset = $('form fieldset').first();
21 var $search_button = $fieldset.find('button');
22 $search_button.css("width","47px");
23 $search_button.css("right","-55px");
24 $search_button.css("font-size","95%");
25 $search_button.find('span').first().hide();
26
27 $fieldset.append('<button id="htrc-bw" class="button search" style="width:47px; right: -105px; background-image:url('+https_servlet_url + 'bookworm.png);" title="Bookworm"></button>');
28
29 var $bw_button = $('#htrc-bw');
30 $bw_button.click(function(event) {
31 var input=$('#q1-input').val();
32 if (input === "") {
33 return;
34 }
35 event.preventDefault();
36
37 var url_front = "https://bookworm.htrc.illinois.edu/#?%7B%22counttype%22%3A%22WordsPerMillion%22%2C%22search_limits%22%3A%5B";
38 var url_end = "%5D%7D";
39
40 var word_front = "%7B%22word%22%3A%5B%22";
41 var word_end = "%22%5D%2C%22date_year%22%3A%7B%22%24gte%22%3A1750%2C%22%24lte%22%3A1923%7D%7D";
42
43 var input_words = input.split(' ');
44 var word_len = input_words.length;
45
46 var full_url = url_front;
47
48 for (var i=0; i<word_len; i++) {
49 var word = input_words[i];
50 if (i>0) { full_url += "%2C"; } // append comma
51 full_url += word_front + word + word_end;
52 }
53
54 full_url += url_end;
55
56 // 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";
57
58 window.location.href = full_url;
59
60 });
61
62 var url = window.location.href;
63 var home_url_test = url.replace(/^https:\/\/www.hathitrust.org\//,"");
64 //console.log("*** home urltest = " + home_url_test);
65 if (home_url_test !== "") {
66 var $login_button = $('#login-button');
67 if ($login_button.length) {
68 $login_button.css('font-size','95%');
69 $login_button.css('right','-45px');
70 $login_button.css('width','80px');
71 }
72 }
73
74
75
76 /*
77 each(function() {
78
79 $('form button').each(function() {
80 console.log("*** this = " + this + "jquery this = " + $(this).val());
81 });
82 */
83
84
85}
86
87function mashupAugmentResults()
88{
89 //console.log("**** mashupAugmentResults() called");
90 var $results_a = $('#results_A');
91 var results_a_exists = $results_a.length;
92
93 var result_class=".result";
94 var result_access_link_class=".result-access-link";
95
96 if (!results_a_exists) {
97 // Not a main search result page
98 // => See if part of the collections page area
99 // => If it is, set things up so it will use that instead
100 $results_a = $('#form1');
101 results_a_exists = $results_a.length;
102 }
103
104 if (!results_a_exists) {
105 // Perhaps a catalog page search?
106 $results_a = $('form[name="addForm"]');
107 results_a_exists = $results_a.length;
108 result_class = ".resultitem";
109 result_access_link_class = ".AccessLink";
110 }
111
112 if (results_a_exists) {
113 //console.log("*** results_a = " + $results_a.html());
114
115 var ids = [];
116 //var file_safe_ids = [];
117
118 //$results_a.find('.result').each(function() {
119 // var $result_access_link = $(this).find('.result-access-link');
120
121 $results_a.find(result_class).each(function() {
122 var $result_access_link = $(this).find(result_access_link_class);
123
124 //console.log("*** result access link = " + $result_access_link.html());
125
126 var $id_link = $result_access_link.find('ul>li>a').first();
127 //console.log("*** id_link = " + $id_link.html());
128 var data_clicklog = $id_link.attr("data_clicklog");
129 if (!data_clicklog) {
130 // fake one from the second <a> href (if present)
131 $id_link2 = $result_access_link.find('ul>li>a:eq(1)');
132 if ($id_link2.length>0) {
133 //console.log("*** id_link2 = " + $id_link2.html());
134 var id2_href = $id_link2.attr("href");
135 var id2 = id2_href.replace(/^https:\/\/hdl.handle.net\/\d+\//,"");
136 data_clicklog = "{\"id\":\"" + id2 + "\"}";
137 }
138 else {
139 data_clicklog="";
140 }
141 }
142 //console.log("*** data clicklog= " + data_clicklog);
143
144
145 var data_json_str = data_clicklog.replace(/^[a-z]+\|/,"");
146 //console.log("*** data json= " + data_json_str);
147
148 var id = null;
149 if (data_json_str !== "") {
150 var data_json = JSON.parse(data_json_str);
151 id = data_json.id;
152 //console.log("*** (catalog extracted) id= '" + id + "'");
153 }
154 else {
155 var $second_id_link = $result_access_link.find('ul>li>a').eq(1);
156 //console.log("*** 2nd link len = " + $second_id_link);
157 var id_href = $second_id_link.attr("href");
158 //console.log("*** id href = " + id_href);
159 if (id_href) {
160 //console.log("*** id href = " + id_href);
161 var encoded_id = id_href.replace(/^.*id=/,"");
162 id = decodeURIComponent(encoded_id);
163 }
164 }
165 if (id) {
166 ids.push(id);
167
168 //var file_safe_id = id.replace(/:/g,"+").replace(/\//g,"=");
169 //file_safe_ids.push(file_safe_id);
170
171 $result_access_link.attr("id","htrc-mashup-" + id);
172 //$result_access_link.attr("id","htrc-mashup-" + file_safe_id);
173 }
174 });
175
176 var ids_str = ids.join(",");
177 //var file_safe_ids_str = file_safe_ids.join(",");
178
179 //console.log("*** ids= " + JSON.stringify(ids));
180 //console.log("*** ids_str = " + ids_str);
181
182 console.log("**** Away to request: " + https_servlet_url + "VolumeCheck?ids=" + encodeURIComponent(ids_str));
183
184 var check_ids_url = https_servlet_url + "VolumeCheck";
185 //var check_data = { "ids": encodeURIComponent(ids_str) };
186 var check_data = { "ids": ids_str };
187
188 $.post(check_ids_url,check_data)
189 .done(function(data,textStatus,response) {
190 console.log("Adding in HTRC cross-checks");
191 var ids_exist = data;
192 for (var k in ids_exist) {
193 //console.log("*** k = '" + k + "'");
194
195 //var id_str = '#htrc-mashup-'+k;
196 //console.log("*** id str = " + id_str);
197 // var $id_div = $(id_str);
198
199 //var $id_div = $('#'+'htrc-mashup-'+k);
200 var id_div = document.getElementById('htrc-mashup-'+k);
201 //console.log("*** id_div = " + id_div);
202 var $id_div = $(id_div);
203
204 if (ids_exist[k]) {
205 var encoded_id=encodeURIComponent(k);
206 var ef_url = https_servlet_url + "VolumeCheck?download-id=" + encoded_id;
207 var atag = "<a href=\""+ ef_url +"\"><span class=\"icomoon icomoon-download\"></span>Download Extracted Features</a>";
208
209 $id_div.find("ul").append("<li title=\""+k+"\" style=\"color: #924a0b;\">"+atag+"</li>"); // ✓
210 }
211 else {
212 // ✗, ✘
213 $id_div.find("ul").append("<li title=\""+k+"\" style=\"color: red;\">HTRC unfriendly ✘</li>");
214 }
215
216 //console.log("*** id div len = " + $id_div.length);
217
218 //$.append("<li>HTRC friendly</li>");
219 }
220 })
221 .fail(function(esponse,textStatus,errorThrown) {
222 alert( "error:" + errorThrown);
223 });
224
225
226 //console.log("*** GM httpRequest made ");
227 }
228
229 // Look for collection set download button
230
231
232 var $col_download_button = $('form button[data-tracking-action="MB Download Metadata');
233 if ($col_download_button.length >0) {
234 var $download_form=$col_download_button.parent();
235 $download_form.append("<button id=\"col-to-workset-download\" class=\"btn btn-mini\" style=\"margin-top: 12px;\"><i class=\"icomoon icomoon-download\"></i> Convert to HTRC Workset</button>");
236 var $workset_download_button = $('#col-to-workset-download');
237 $workset_download_button.click(function(event) {
238 event.preventDefault();
239
240 var action = $download_form.attr("action");
241 //var action_url = action;
242
243 var action_url = https_servlet_url + "VolumeCheck";
244
245 // Extract hidden elems from, e.g.
246 // <input type="hidden" name="c" value="464226859" />
247 // <input type="hidden" name="a" value="download" />
248 // <input type="hidden" name="format" value="text" />
249 var $hidden_inputs = $download_form.find("input[type=\"hidden\"]");
250 for (var i=0; i<$hidden_inputs.length; i++) {
251 var hidden_input = $hidden_inputs[i];
252
253 if (i===0) {
254 action_url += "?";
255 }
256 else {
257 action_url += "&";
258 }
259
260 var $hidden_input = $(hidden_input);
261 var hi_name = $hidden_input.attr("name");
262 var hi_val = $hidden_input.attr("value");
263 if (hi_name === "c") {
264 hi_name = "convert-col";
265 }
266 action_url += hi_name + "=" + hi_val;
267
268 }
269
270 // <title>Collections: Ancestry and Genealogy | HathiTrust Digital Library</title>
271 var all_col_title = $('title').text();
272 var col_title = all_col_title.replace(/^Collections: /,"").replace(/ \|.*?$/,"");
273 action_url += "&col-title=" + encodeURIComponent(col_title);
274
275 console.log("workset download url: " + action_url);
276 window.location.href = action_url;
277 });
278
279 }
280
281
282}
283
284mashupInit();
285
286mashupAugmentResults();
287
Note: See TracBrowser for help on using the repository browser.