-
Notifications
You must be signed in to change notification settings - Fork 0
/
octopus.js
418 lines (359 loc) · 20.5 KB
/
octopus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Octopus
// James Misson
// Current verison: 1.2: 15/07/22
// v1.1: 23/06/22
// v1.0: March 2022
// New in 1.2
// GB proximity search corrected
// multiple keywords work for all fields
// Fixed: A bug that produced 'undefined' values when both before and after used.
// New in 1.1
// EEBO module added (automates variant search by adding curly brackets)
// Link out to JSTOR fixed
// ocr: prefix added to JSTOR searches
// multiple keywords can now be defined in the keyword box, separated by commas (for, e.g. plurals, verb conjugations). This only works for before, or after, but not BOTH before and after because the loop seems to get stuck
//TODO:
// add NOT box
// add switch for NEAR or PRE for prox searches
// a bug: when do a no-dated search, then clear, then do a new dated search, the no-dated search also appears in results; also old prox stuff not clearing? perhaps now fixed by refreshing generic_string nd search_strings in add()
// seeing as we want the search links to be dynamic (update when textfield is changed), maybe they don't need to be determined in the db functions at all? Just update on textfield change? hmm but the update link function doesnt work when its only that running so seems like it only works via manual input
// make update function also include the dates again
// give a warning for dates out of range on certain DBs (i.e. if dates < 2007, no twitter for instance). If whole DB is out of date range, untick box
// something weird going on when add search with no dates, then add another search with dates. dates wrong way round?? gb goes 31 of dec to 1 jan?? cant replicate it now
// some field validation/strip trailing white space from inputs?
// work out mechanism for splitting overlong search strings
// option to save the output (new section below Output block)?!
// need an undo button/function for when adding searches to string?
let before = ''
let keyword = ''
let after = ''
let prox = 0
let prox_kw = ''
let dateLower;
let dateUpper;
let ngrams = []
let search_strings = []
let eebo_search_strings = []
// var regex = / NEAR\/\d{1,2} /;
const regex = new RegExp(/ NEAR\/\d{1,2} /, 'g')
function add() {
//clear ngrams
ngrams = []
eebo_ngrams = []
// clear generic string? this might mess something up further down?
generic_string = ''
eebo_string = ''
//clear search_strings
search_strings = []
eebo_earch_strings = []
//get inputs
before = document.querySelector('#before').value;
keyword = document.querySelector('#keyword').value;
after = document.querySelector('#after').value;
prox_kw = document.querySelector('#proximity-keyword').value;
//check that date inputs are dates before assigning value, if one is blank make it 0000 or 2022
//not sure if 0000 words in db URLs yet
if (/^\d{4}$/.test(document.querySelector('#date_lower').value) && /^\d{4}$/.test(document.querySelector('#date_upper').value)) {
dateLower = document.querySelector('#date_lower').value;
dateUpper = document.querySelector('#date_upper').value;
console.log('both numbers')
} else if (/^\d{4}$/.test(document.querySelector('#date_lower').value)) {
dateLower = document.querySelector('#date_lower').value;
dateUpper = '2022'
} else if (/^\d{4}$/.test(document.querySelector('#date_upper').value)) {
dateLower = '0000'
dateUpper = document.querySelector('#date_upper').value;
} else {
dateLower = '';
dateUpper = '';
}
//check that prox is a 2 digit number
if (/^\d{1,2}$/.test(document.querySelector('#proximity').value)) {
prox = document.querySelector('#proximity').value;
} else {
prox = '';
}
//run functions on that input
// console.log('running')
parseInput()
gb()
proq()
twi()
gale()
jstor()
hathi()
nex()
eebo()
}
function parseInput() {
// takes inputs of before, keyword, and after, combines them correctly, adds each to ngrams array
if (before.length !== 0) {
var beforeArray = before.split(', ');
};
if (keyword.length !== 0) {
var keywordArray = keyword.split(', ');
};
if (after.length !== 0) {
var afterArray = after.split(', ');
};
// compile arrays of ngrams depending on whether values are before, after, or before and after (this seems very heavy handed, must be an easier way!)
if (beforeArray && afterArray) {
for (var a = 0; a < afterArray.length; a++) {
for (var b = 0; b < beforeArray.length; b++) {
for (var c = 0; c < keywordArray.length; c++) {
let ngram = beforeArray[b] + ' ' + keywordArray[c] + ' ' + afterArray[a];
ngrams.push(ngram);
}
}
}
} else if (beforeArray) {
for (var b = 0; b < beforeArray.length; b++) {
for (var c = 0; c < keywordArray.length; c++) {
let ngram = beforeArray[b] + ' ' + keywordArray[c];
ngrams.push(ngram);
}
}
} else if (afterArray) {
for (var a = 0; a < afterArray.length; a++) {
for (var c = 0; c < keywordArray.length; c++) {
let ngram = keywordArray[c] + ' ' + afterArray[a];
ngrams.push(ngram);
}
}
} else {
for (var c = 0; c < keywordArray.length; c++) {
ngrams.push(keywordArray[c])
}
}
// combining ngrams with OR or proximity syntax as appropriate, to create a generic string that is later converted
if (prox) {
let prox_kwArray = prox_kw.split(', ')
for (let n = 0; n < ngrams.length; n++) {
for (let p = 0; p < prox_kwArray.length; p++) {
search_string = '("' + ngrams[n] + '" NEAR/' + prox + ' ' + '"' + prox_kwArray[p] + '")'
search_strings.push(search_string)
generic_string = search_strings.join(" OR ")
}
}
} else {
generic_string = '"' + ngrams.join('" OR "') + '"';
}
// doing the same as above but for EEBO which needs special handling for variants
//adding curley brackets and quot marks to ngrams of more than 1 word (EEBO needs single words searched without these to access variants)
let eebo_ngrams = []
let eebo_prox_kwArray = []
let prox_kwArray = prox_kw.split(', ');
for (let n = 0; n < ngrams.length; n++) {
if (ngrams[n].includes(' ')) {
eebo_ngrams.push('"{' + ngrams[n] + '}"')
} else {
eebo_ngrams.push(ngrams[n])
}
}
//same again for prox: add curley brackets and quot marks to proximity keywords of more than 1 word (EEBO needs single words searched without these to access variants)
if (prox) {
console.log(prox_kwArray)
for (let n = 0; n < prox_kwArray.length; n++) {
if (prox_kwArray[n].includes(' ')) {
eebo_prox_kwArray.push('"{' + prox_kwArray[n] + '}"')
} else {
eebo_prox_kwArray.push(prox_kwArray[n])
}
}
}
// then combine as for generic string but not with quot marks this time
if (prox) {
for (let n = 0; n < eebo_ngrams.length; n++) {
for (let p = 0; p < eebo_prox_kwArray.length; p++) {
eebo_search_string = '(' + eebo_ngrams[n] + ' NEAR/' + prox + ' ' + prox_kwArray[p] + ')'
eebo_search_strings.push(eebo_search_string)
eebo_string = eebo_search_strings.join(" OR ")
}
}
} else {
eebo_string = eebo_ngrams.join(' OR ');
}
}
//functions for updating db textareas and links (the repetition between them could be wrapped up in its own function?)
function gb() {
//if proximity
if (prox_kw) {
let prox_kwArray = prox_kw.split(', ')
google_string = '("' + ngrams.join('" OR "') + '") AROUND(' + prox + ') ("' + prox_kwArray.join('" OR "') + '")'
} else {
google_string = generic_string
}
//push string to text area, or add to what's already there
if (document.getElementById("google_result").value) {
document.getElementById("google_result").value += ' OR ' + google_string
google_string = document.getElementById("google_result").value
} else {
document.getElementById("google_result").value = google_string
google_string = document.getElementById("google_result").value
}
// update link out
if (dateLower && dateUpper) {
document.getElementById("google_link").href = 'https://www.google.com/search?tbo=p&tbm=bks&q=' + google_string.replace(' ', '+') + '&tbs=,cdr:1,cd_min:Jan+1_2+' + dateLower + ',cd_max:Dec+31_2+' + dateUpper + '&num=100';
} else {
document.getElementById("google_link").href = 'https://www.google.com/search?tbo=p&tbm=bks&q=' + google_string.replace(' ', '+') + '&num=100'
}
}
function proq() {
// generic_string is already same as proq string
proq_string = generic_string
//push string to text area, or add to what's already there
if (document.getElementById("proq_result").value) {
document.getElementById("proq_result").value += ' OR ' + proq_string
proq_string = document.getElementById("proq_result").value
} else {
document.getElementById("proq_result").value = proq_string
proq_string = document.getElementById("proq_result").value
}
// update link out NB search link not there so this doesn't do anything
// if (dateLower && dateUpper) {
// document.getElementById("proq_link").href = 'INSERT PROQUEST URL' + proq_string.replace(' ', '+') + '&tbs=,cdr:1,cd_min:Jan+1_2+' + dateLower + ',cd_max:Dec+31_2+' + dateUpper + '&num=100';
// } else {
// document.getElementById("proq_link").href = 'INSERT PROQUEST URL' + proq_string.replace(' ', '+') + '&num=100'
// }
}
function twi() {
//replace proximity in generic string NB TWITTER JUST MAKES IT AN 'AND' AS TWITTER DOESNT HAVE PROX, AND TWEETS ARE SHORT ANYWAY
twi_string = generic_string.replaceAll(regex, ' AND ');
//push string to text area, or add to what's already there
if (document.getElementById("twi_result").value) {
document.getElementById("twi_result").value += ' OR ' + twi_string
twi_string = document.getElementById("twi_result").value
} else {
document.getElementById("twi_result").value = twi_string
twi_string = document.getElementById("twi_result").value
}
// update link out
if (dateLower && dateUpper) {
//work out date stuff
document.getElementById("twi_link").href = 'https://twitter.com/search?q=' + twi_string.replace(' ', '%20') + '%20until%3A' + dateUpper + '-12-31%20since%3A' + dateLower + '-01-01&src=typed_query';
} else {
document.getElementById("twi_link").href = 'https://twitter.com/search?q=' + twi_string.replace(' ', '%20') + '&src=typed_query&f=top'
}
}
function gale() {
//replace proximity in generic string
gale_string = generic_string.replaceAll(regex, ' n' + prox + ' ');
//push string to text area, or add to what's already there
if (document.getElementById("gale_result").value) {
document.getElementById("gale_result").value += ' OR ' + gale_string
gale_string = document.getElementById("gale_result").value
} else {
document.getElementById("gale_result").value = gale_string
gale_string = document.getElementById("gale_result").value
}
// update link out
if (dateLower && dateUpper) {
document.getElementById("gale_link").href = 'https://ezproxy-prd.bodleian.ox.ac.uk:2201/ps/advancedSearch.do?operators%5B999%5D=And&inputFieldValues%5B999%5D=&inputFieldNames%5B999%5D=TXT&fuzzyLevels%5BOQE%5D=HIGH&fuzzyLevels%5BTXT%5D=HIGH&inputFieldValues%5B0%5D=' + gale_string.replace(' ', '+') + '&inputFieldNames%5B0%5D=TXT&operators%5B1%5D=And&inputFieldValues%5B1%5D=&inputFieldNames%5B1%5D=TXT&operators%5B2%5D=And&inputFieldValues%5B2%5D=&inputFieldNames%5B2%5D=TXT&_fuzzyEnabled=on&limiterFieldValues%5BDB%5D=GDSC&limiterFieldValues%5BDB%5D=BNCN&limiterFieldValues%5BDB%5D=DMHA&limiterFieldValues%5BDB%5D=ECCO&limiterFieldValues%5BDB%5D=FTHA&limiterFieldValues%5BDB%5D=NCCO&limiterFieldValues%5BDB%5D=NCNP&limiterFieldValues%5BDB%5D=NCUK&limiterFieldValues%5BDB%5D=PLEX&limiterFieldValues%5BDB%5D=BBCN&limiterFieldValues%5BDB%5D=NICN&limiterFieldValues%5BDB%5D=ECON&limiterFieldValues%5BDB%5D=ILN&limiterFieldValues%5BDB%5D=MOML&limiterFieldValues%5BDB%5D=MOME&limiterFieldValues%5BDB%5D=STHA&limiterFieldValues%5BDB%5D=TGRH&limiterFieldValues%5BDB%5D=TTDA&limiterFieldValues%5BDB%5D=TLSH&limiterFieldValues%5BDB%5D=USDD&limiterTypes%5BDB%5D=OR&dateIndices=DA&dateLimiterValues%5BDA%5D.dateMode=4&dateLimiterValues%5BDA%5D.fromDay=00&dateLimiterValues%5BDA%5D.fromMonth=00&dateLimiterValues%5BDA%5D.fromYear=' + dateLower + '&dateLimiterValues%5BDA%5D.toDay=00&dateLimiterValues%5BDA%5D.toMonth=00&dateLimiterValues%5BDA%5D.toYear=' + dateUpper + '&standAloneLimiters=DA&_nonDatedLimiterValues%5BDA%5D=on&standAloneLimiters=DG&limiterTypes%5BDG%5D=OR&standAloneLimiters=TY&limiterTypes%5BTY%5D=OR&limiterFieldValues%5BPU%5D=&standAloneLimiters=PU&standAloneLimiters=GS&limiterTypes%5BGS%5D=OR&standAloneLimiters=PUB_COUNTRY&limiterTypes%5BPUB_COUNTRY%5D=OR&standAloneLimiters=PUB_STATE_COUNTRY&limiterTypes%5BPUB_STATE_COUNTRY%5D=OR&standAloneLimiters=PUB_CITY_STATE_COUNTRY&limiterTypes%5BPUB_CITY_STATE_COUNTRY%5D=OR&standAloneLimiters=LG&limiterTypes%5BLG%5D=OR&standAloneLimiters=IMG_TY&limiterTypes%5BIMG_TY%5D=OR&standAloneLimiters=SRC_INST&limiterTypes%5BSRC_INST%5D=OR&method=doSearch&noOfRows=3&searchType=AdvancedSearchForm&userGroupName=oxford&prodId=GDCS&nwf=y&searchResultsType=MultiTab';
} else {
document.getElementById("gale_link").href = 'https://ezproxy-prd.bodleian.ox.ac.uk:2201/ps/basicSearch.do?inputFieldNames%5B0%5D=OQE&inputFieldValues%5B0%5D=' + gale_string.replace(' ', '+') + '&nwf=y&searchType=BasicSearchForm&userGroupName=oxford&prodId=GDCS&spellCheck=true&method=doSearch&dblist=&stw.option=&ebook=&dateMode%5BDA%5D=all&_nonDatedLimiterValues%5BDA%5D=on&limiterTypes%5BDB%5D=OR';
}
}
function nex() {
//replace proximity in generic string
nex_string = generic_string.replaceAll(regex, ' near/' + prox + ' ');
//push string to text area, or add to what's already there
if (document.getElementById("nex_result").value) {
document.getElementById("nex_result").value += ' OR ' + nex_string
nex_string = document.getElementById("nex_result").value
} else {
document.getElementById("nex_result").value = nex_string
nex_string = document.getElementById("nex_result").value
}
// update link out
if (dateLower && dateUpper) {
document.getElementById("nex_link").href = 'https://advance.lexis.com/search/?pdsearchtype=SearchBox&pdtypeofsearch=searchboxclick&pdstartin=&pdsearchterms=' + nex_string.replace(' ', '+') + '&pdtimeline=01%2F01%2F' + dateLower + '+to+31%2F12%2F' + dateUpper + '%7Cbetween%7CDD%2FMM%2FYYYY&pdpsf=&pdquerytemplateid=&pdsf=&ecomp=bdbhkkk&prid=be7489dc-9481-42cb-9429-8e64a7e650d1';
} else {
document.getElementById("nex_link").href = 'https://advance.lexis.com/search/?pdsearchtype=SearchBox&pdtypeofsearch=searchboxclick&pdstartin=&pdsearchterms=' + nex_string.replace(' ', '+');
}
}
function jstor() {
// currently just converts proximities to ANDs, the real prox stuff needs some more attention (needs a warning system and multiple tab opening? I guess print the ("x y"~10) on separate lines?)
jstor_string = generic_string.replaceAll(regex, ' AND ');
//push string to text area, or add to what's already there
if (document.getElementById("jstor_result").value) {
document.getElementById("jstor_result").value += ' OR ' + jstor_string
jstor_string = document.getElementById("jstor_result").value
} else {
document.getElementById("jstor_result").value = jstor_string
jstor_string = document.getElementById("jstor_result").value
}
// update link out
if (dateLower && dateUpper) {
document.getElementById("jstor_link").href = 'https://ezproxy-prd.bodleian.ox.ac.uk:2116/action/doAdvancedSearch?group=none&so=old&q0=ocr%3A%28' + jstor_string.replace(' ', '%20') + '%29&q1=&q2=&q3=&q4=&q5=&q6=&sd=' + dateLower + '&ed=' + dateUpper + '&pt=&isbn=&f0=all&c1=AND&f1=all&c2=AND&f2=all&c3=AND&f3=all&c4=AND&f4=all&c5=AND&f5=all&c6=AND&f6=all&acc=on&la=';
} else {
document.getElementById("jstor_link").href = 'https://ezproxy-prd.bodleian.ox.ac.uk:2116/action/doAdvancedSearch?group=none&so=old&q0=ocr%3A%28' + jstor_string.replace(' ', '%20') + '%29&q1=&q2=&q3=&q4=&q5=&q6=&sd=&pt=&isbn=&f0=all&c1=AND&f1=all&c2=AND&f2=all&c3=AND&f3=all&c4=AND&f4=all&c5=AND&f5=all&c6=AND&f6=all&acc=on&la=';
}
}
function hathi() {
//replace proximity in generic string NB hathi JUST MAKES IT AN 'AND' AS hathi DOESNT HAVE PROX
hathi_string = generic_string.replaceAll(regex, ' AND ');
//push string to text area, or add to what's already there
if (document.getElementById("hathi_result").value) {
document.getElementById("hathi_result").value += ' OR ' + hathi_string
hathi_string = document.getElementById("hathi_result").value
} else {
document.getElementById("hathi_result").value = hathi_string
hathi_string = document.getElementById("hathi_result").value
}
// update link out
if (dateLower && dateUpper) {
//work out date stuff
document.getElementById("hathi_link").href = 'https://babel.hathitrust.org/cgi/ls?a=srchls&q1=' + hathi_string.replace(' ', '+') + '&field1=ocr&anyall1=all&yop=between&pdate_start=' + dateLower + '&pdate_end=' + dateUpper;
} else {
document.getElementById("hathi_link").href = 'https://babel.hathitrust.org/cgi/ls?a=srchls&q1=' + hathi_string.replace(' ', '+') + '&field1=ocr&anyall1=all&yop=after';
}
}
function eebo() {
// the eebo string has already been defined in the 'add' function because variants needed special handling from the source
//push string to text area, or add to what's already there
if (document.getElementById("eebo_result").value) {
document.getElementById("eebo_result").value += ' OR ' + eebo_string
proq_string = document.getElementById("eebo_result").value
} else {
document.getElementById("eebo_result").value = eebo_string
eebo_string = document.getElementById("eebo_result").value
}
}
//other functions
function searchSelected() {
console.log('searching selected')
//NB popups need to be allowed in Chrome
var checkboxIDs = ['gb-checkbox', 'twi-checkbox', 'gale-checkbox', 'hathi-checkbox', 'jstor-checkbox', 'nex-checkbox']
var locs = [document.getElementById("google_link").href, document.getElementById("twi_link").href, document.getElementById("gale_link").href, document.getElementById("hathi_link").href, document.getElementById("jstor_link").href, document.getElementById("nex_link").href]
for (let i = 0; i < checkboxIDs.length; i++) {
if (document.getElementById(checkboxIDs[i]).checked) {
window.open(locs[i])
}
}
};
function update_link(elementId) {
//NB DOESNT CURRENTLY UPDATE DATES TOO!
//update the link to GB if the gb field is edited
let new_input = document.getElementById(elementId).value
document.getElementById("google_link").href = 'https://www.google.com/search?tbo=p&tbm=bks&q=' + new_input.replace(' ', '+') + '&num=100';
}
function copystring(elementId) {
//copy text to clipboard
var copyText = document.getElementById(elementId)
navigator.clipboard.writeText(copyText.value)
}
function clearResults() {
ngrams=[]
document.getElementById("proq_result").value = ''
document.getElementById("google_result").value = ''
document.getElementById("twi_result").value = ''
document.getElementById("gale_result").value = ''
document.getElementById("jstor_result").value = ''
document.getElementById("hathi_result").value = ''
document.getElementById("nex_result").value = ''
document.getElementById("eebo_result").value = ''
}
// set eventlistener for content is loaded, and only then do the convert function when form is submitted
document.addEventListener('DOMContentLoaded', function(){
document.querySelector('form').addEventListener('submit', add);
});