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

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

Reworked to use CORS and $.ajax() so TamperMonkey doesn't interceed with security check page. Also relocated bookworm PNG from external site to be within the servlet area.

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