forked from creativecommons/search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
580 lines (479 loc) · 13.8 KB
/
search.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
$(function () {
setupQuery();
var queryPos = $("#query").position();
var queryWidth = $("#query").innerWidth();
// $("#engineInfo").css("left", $("#engine").position().left + 7);
// $("#engineInfo").width($("#engine").width() - 32);
// pick a random search engine
//setEngine(engines[Math.floor(Math.random() * engines.length)]);
// hide all the radio buttons if JS is enabled
$(".engine button").focus(function () {
$(".engine").removeClass("focus")
$(this).parents(".engine").addClass("focus");
})
$(".engine button").focusout(function () {
$(".engine").removeClass("focus")
})
$(".engine").click(function () {
setEngine($(this).find("button").first().attr("id"));
doSearch();
});
// sniff browser and determine what information to display
/*
var browser = $.browser;
if (browser.mozilla) {
if (browser.version.substr(0,3) == "1.9") {
$("#remove").show();
} else {
$("#add").show();
$("#searchBar").show();
}
} else {
$("#add").show();
if (browser.msie && browser.version.substr(0,1) > 6) {
$("#searchBar").show();
} else {
$("#addressBar").show();
}
}
*/
$('#addOpenSearch').click(function () {
if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
window.external.AddSearchProvider("http://oldsearch.creativecommons.org/ccsearch.xml");
} else {
alert("Your browser does not support OpenSearch.");
}
return false;
});
$('#lang').change(function () {
/* get value of the language */
var lang_chosen = $("#lang").val();
var new_loc = location.href.split('?')[0];
new_loc = new_loc.split('#')[0]; /* Remove spurious "#" */
new_loc = new_loc + '?lang=' + lang_chosen;
window.location = new_loc;
});
});
/*
* Creative Commons Search Portal Interface
* 1.0 - 2006-07
*
*/
var engines = ["google", "googleimg", "flickr", "jamendo", "openclipart", "wikimediacommons", "fotopedia", "europeana", "youtube", "ccmixter", "soundcloud", "thingiverse"];
//defaults:
var engine = "";
var comm = 1;
var deriv = 1;
var rights = "";
var url = "";
var lang = "";
var default_query = "flowers";
var default_engine = "_random";
//var default_comm = 1;
//var default_deriv = 1;
/*
// DEBUG!!!!!
var d = new Date();
d.setFullYear(2020,0,1);
setCookie("ccsearch", "jamendo", d, '/')
alert("cookie planted! mwahahahaha");
// \DEBUG!!!!!
*/
// mmm, cookies...
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
////
var cookie_name = '__ccsearch';
var cookie_break_text = "[-]";
var cookie_domain = 'oldsearch.creativecommons.org';
//var cookie_domain = '';
function saveSettings() {
var cookieDate = new Date();
cookieDate.setFullYear(2020, 0, 1);
cookieText = engine + cookie_break_text + comm + cookie_break_text + deriv;
setCookie(cookie_name, cookieText, cookieDate, '/', cookie_domain);
}
function getSettings() {
cookieText = getCookie(cookie_name);
if (cookieText && cookieText != '') {
//break it into pieces
cookieCrumbs = cookieText.split(cookie_break_text);
engine = cookieCrumbs[0];
if (1 in cookieCrumbs) {
comm = cookieCrumbs[1];
}
if (2 in cookieCrumbs) {
deriv = cookieCrumbs[2];
}
}
else {
}
if (engine == null || !engine || engine == "") {
//engine = default_engine;
engine = "_random";
//engine = engines[Math.floor(Math.random() * engines.length)];
}
}
// function by Pete Freitag ([email protected])
function getQueryStrVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return '';
}
function show_ffx_msg() {
$('#ffx-search-bar-info').style.display = "block";
}
// initialise app
function setupQuery() {
var query = $("#query");
var qs = getQueryStrVariable('q');
qs = unescape(qs);
var moz = getQueryStrVariable('sourceid');
var e = getQueryStrVariable('engine');
var docom = getQueryStrVariable('commercial');
var doder = getQueryStrVariable('derivatives');
// display firefox branding
if (moz == "Mozilla-search") {
show_ffx_msg();
}
// grab cookie and setup default engine
getSettings();
if (e) {
setEngine(e);
}
else {
setEngine(engine);
}
// set commercial + derivative checkboxes
updateCommDerivCheckboxes(docom, doder);
//lang = getQueryStrVariable('lang');
// Only insert query variable if nothing else is in the search entry
// Should solve back button problems
if (query.val().length < 1 && qs) query.val(qs);
// Since we don't have a submit button, be nice to users and
// set focus in search box.
query.focus();
}
// bell
function wakeQuery() {
var query = $('#q');
if (query.value == d) {
query.val("");
query.addClass("active");
query.removeClass("inactive");
}
}
// whistle
function resetQuery() {
var query = $('#q');
if (query.value == "") {
query.val(d);
query.addClass("inactive");
query.removeClass("active");
}
}
function setEngine(e) {
var previous = engine;
var query = $("#query");
if (query.hasClass("inactive")) {
query.val(default_query);
query.addClass("active");
query.removeClass("inactive");
}
if (typeof e == "string") {
if (e == "_random") {
engine = engines[Math.floor(Math.random() * engines.length)];
} else {
engine = e;
}
} else {
engine = e.value;
}
$("#engineInfo ." + previous).hide();
$("#engineInfo ." + engine).show();
$("button[value=" + engine + "]").attr("checked", true);
$(".engine").removeClass("selected");
$("button[value=" + engine + "]").parents(".engine").addClass("selected");
//if (e == "_random") engine = "_random";
saveSettings();
//doSearch();
}
function setCommDeriv() {
if ($('#comm').attr("checked"))
comm = 1;
else
comm = 0;
if ($('#deriv').attr("checked"))
deriv = 1;
else
deriv = 0;
saveSettings();
}
function updateCommDerivCheckboxes(comOverride, derivOverride) {
if ((comm == 1))
$('#comm').attr("checked", true);
else
$('#comm').attr("checked", false);
if ((deriv == 1))
$('#deriv').attr("checked", true);
else
$('#deriv').attr("checked", false);
}
// build advanced search query strings
// each engine has vastly different ways to do this. :/
function modRights() {
var comm = $("#comm").attr("checked");
var deriv = $("#deriv").attr("checked");
rights = "";
switch (engine) {
case "google":
// Google apparently doesn't like .-() appended to the
// as_rights query string variable, so if neither the
// commercial or deriv checkboxes are checked then rights
// should just be empty.
if ((comm == false) && (deriv == false)) {
rights = "";
break;
}
//.-(cc_noncommercial|cc_nonderived)
rights = ".-(";
if (comm) {
rights += "cc_noncommercial";
}
if (deriv) {
(comm) ? rights += "|" : null;
rights += "cc_nonderived";
}
rights += ")";
break;
case "googleimg":
//.-(cc_noncommercial|cc_nonderived)
// Google apparently doesn't like .-() appended to the
// as_rights query string variable, so if neither the
// commercial or deriv checkboxes are checked then rights
// should just be empty.
if ((comm == false) && (deriv == false)) {
rights = "";
break;
}
rights = ".-(";
if (comm) {
rights += "cc_noncommercial";
}
if (deriv) {
(comm) ? rights += "|" : null;
rights += "cc_nonderived";
}
rights += ")";
break;
case "thingiverse":
/*
If "comm" or "deriv" is provided, then we first apply the filter
to capture only results of type "things" before adding the specifics we require because
the "customizable" and "licence" filters on thingiverse only work alongside the "things" filter
*/
if (comm || deriv) {
rights = "&type=things&sort=relevant";
rights += deriv ? "&customizable=1" : "";
// Used the licence=cc (which on Thingiverse, stands for the Creative Commons Attribution license)
// as the equivalent for the "modify, reuse ..." filter on CC search
rights += comm ? "&license=cc": "";
}
break;
case "yahoo":
rights = "&";
if (comm) {
rights += "ccs=c&";
}
if (deriv) {
rights += "ccs=e";
}
break;
case "flickr":
rights = "l=";
if (comm) {
rights += "comm";
}
if (deriv) {
rights += "deriv";
}
break;
case "jamendo":
if (comm && deriv) {
rights = '-nc%20AND%20-nd';
} else if (comm) {
rights = '-nc';
} else if (deriv) {
rights = '-nd';
}
break;
case "wikimediacommons":
rights = "";
if (rights.length < 5) rights = "";
break;
case "fotopedia":
rights = "";
if (comm && deriv) {
rights = "reuse_commercial_modify";
} else if (comm && !deriv) {
rights = "reuse_commercial";
} else if (!comm && deriv) {
rights = "reuse_modify"
} else {
rights = "reuse";
}
break;
case "europeana":
rights = "";
if (comm && deriv) {
rights = "+AND+RIGHTS%3A*creative*+AND+NOT+RIGHTS%3A*nc*+AND+NOT+RIGHTS%3A*nd*";
} else if (comm && !deriv) {
rights = "+AND+RIGHTS%3A*creative*+AND+NOT+RIGHTS%3A*nc*";
} else if (!comm && deriv) {
rights = "+AND+RIGHTS%3A*creative*+AND+NOT+RIGHTS%3A*nd*"
} else {
rights = "+AND+RIGHTS%3A*creative*+";
}
break;
case "ccmixter":
rights = "";
if (comm && deriv) {
rights = "&lic=by,sa,s,splus,pd,zero";
} else if (comm && !deriv) {
rights = "&lic=open";
} else if (!comm && deriv) {
rights = "&lic=by,nc,sa,ncsa,s,splus,pd,zero"
}
break;
case "soundcloud":
rights = "";
if (comm && deriv) {
rights = "&filter.license=to_modify_commercially";
} else if (comm && !deriv) {
rights = "&filter.license=to_use_commercially";
} else if ((!comm && deriv) || (!comm && !deriv)) {
rights = "&filter.license=to_share";
}
break;
}
}
// "main logic", no turning back.
function doSearch() {
var query = $("#query");
var comm = $("#comm").attr("checked");
var deriv = $("#deriv").attr("checked");
// We never want the search to execute with the default text
if (query.val() == "Enter search query") {
query.val(default_query);
}
url = "";
// search only if there is something to search with
if ((query.val().length > 0)/* && (query.className == "active")*/) {
// set up rights string, works if user hits "go" or a tab.
modRights();
// NOTE: if you make changes here, you should make a similar change in search.php
switch (engine) {
case "openclipart":
url = 'http://openclipart.org/search/?query=' + query.val();
break;
case "jamendo":
if (rights) {
url = 'http://www.jamendo.com/search?qs=fq=license_cc:(' + rights + ')&q=' + query.val();
} else {
url = 'http://www.jamendo.com/search?qs=q=' + query.val();
}
break;
case "flickr":
url = 'http://flickr.com/search/?' + ((rights.length > 2) ? rights : "l=cc") + '&q=' + query.val();
break;
case "owlmm":
url = 'http://www.owlmm.com/?query_source=CC&' + ((rights.length > 13) ? rights : "license_type=cc") + '&q=' + query.val();
break;
case "yahoo":
url = 'http://search.yahoo.com/search?cc=1&p=' + query.val() + rights;
break;
case "googleimg":
url = 'https://www.google.com/search?site=imghp&tbm=isch&q=' + query.val() + '&tbs=sur:f' + ((deriv) ? "m" : "") + ((comm) ? "c" : "") + '%2Cil:cl';
break;
case "wikimediacommons":
url = 'http://commons.wikimedia.org/w/index.php?title=Special%3ASearch&redirs=0&search=' + query.val() + '&fulltext=Search&ns0=1&ns6=1&ns14=1&title=Special%3ASearch&advanced=1&fulltext=Advanced+search';
break;
case "fotopedia":
url = 'http://www.fotopedia.com/search?q=' + query.val() + '&human_license=' + rights;
break;
case "europeana":
url = 'http://www.europeana.eu/portal/search.html?query=' + query.val() + rights;
break;
case "youtube":
url = 'http://www.youtube.com/results?search_query=' + query.val() + ',creativecommons';
break;
case "ccmixter":
url = 'http://ccmixter.org/api/query?datasource=uploads&search_type=all&sort=rank&search=' + query.val() + rights;
break;
case "soundcloud":
url = 'http://soundcloud.com/search/sounds?q=' + query.val() + rights;
break;
case "thingiverse":
url = 'https://www.thingiverse.com/search?q=' + query.val() + rights;
break;
case "google":
default:
url = 'http://google.com/search?as_rights=(cc_publicdomain|cc_attribute|cc_sharealike' +
((comm) ? "" : "|cc_noncommercial") + ((deriv) ? "" : "|cc_nonderived") + ')' +
rights + '&q=' + query.val();
if (lang != null) url += '&hl=' + lang;
break;
}
url = 'https://oldsearch.creativecommons.org/bouncer.php?q=' + query.val() + '&url=' + encodeURIComponent(url);
window.open(
url,
'_blank',
'noopener'
);
// document.getElementBy$('#stat').setAttribute('src','transparent.gif?engine='+engine+'&comm='+comm+'&deriv='+deriv+'&q='+query.value);
}
return false;
}
// i18n
function grabOriginalLanguage() {
return document.getElementsByTagName('html')[0].lang.replace('-', '_');
}
function grabChosenLanguage() {
var select_box = document.getElementById('lang');
for (var i = 0; i < select_box.childNodes.length; i++) {
var select_child = select_box.childNodes[i];
if (select_child.nodeType == 1) {
if (select_child.selected) {
return select_child.value;
}
}
}
return null;
}