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

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

Use new 'convert-col' action

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