summaryrefslogtreecommitdiff
path: root/Google Scholar.js
blob: b997f89505304113bb4ef6e9a373e71839297065 (plain)
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
{
	"translatorID": "57a00950-f0d1-4b41-b6ba-44ff0fc30289",
	"label": "Google Scholar",
	"creator": "Simon Kornblith, Frank Bennett, Aurimas Vinckevicius",
	"target": "^https?://scholar\\.google\\.(?:com|cat|(?:com?\\.)?[a-z]{2})/(?:scholar(?:_case)?\\?|citations\\?)",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-07-25 21:01:15"
}

/*
 * Test pages
 *
 * Searches of Google Scholar with the following terms should yield a folder
 * icon that works.  Check that unlinked ([CITATION]) items that provide
 * no BibTeX data (there is currently one under "Marbury v. Madison",
 * and "clifford" seems to be a good source of garbage) are
 * dropped from the listings:
 *
 *   marbury v madison
 *   kelo
 *   smith
 *   view of the cathedral
 *   clifford
 *
 * "How cited" pages should NOT yield a page or folder icon.  The
 * Urls to these currently look like this:
 *
 *   http://scholar.google.co.jp/scholar_case?about=1101424605047973909&q=kelo&hl=en&as_sdt=2002
 *
 * Case pages should present a document icon that works:
 *
 *   http://scholar.google.co.jp/scholar_case?case=18273389148555376997&hl=en&as_sdt=2002&kqfp=13204897074208725174&kql=186&kqpfp=16170611681001262513#kq
 */

var bogusItemID = 1;
var __old_CF, __result_counter=0;

var detectWeb = function (doc, url) {
	// Icon shows only for search results and law cases
	if (url.indexOf('/scholar_case?') != -1 &&
		url.indexOf('about=') == -1) {
			return "case";
	} else if(url.indexOf('/citations?') != -1) {
		//individual saved citation
		var link = ZU.xpathText(doc, '//a[@class="gsc_title_link"]/@href');
		if(!link) return;
		
		if(link.indexOf('/patents?') != -1) {
			return 'patent';
		} else if(link.indexOf('/scholar_case?') != -1) {
			return 'case';
		} else {
			//Can't distinguish book from journalArticle
			//Both have "Journal" fields
			return 'journalArticle';
		}
	} else if( getViableResults(doc).length ) {
		return "multiple";
	}
}

/*********************************
 * Cookie manipulation functions *
 *********************************/

//sets Google Scholar Preference cookie
function setGSPCookie(doc, cf) {
	var m = doc.cookie.match(/\bGSP=[^;]+/);
	var cookie = m ? m[0] : '';
	if(!cookie) return;
	
	Z.debug('Changing cookie: ' + cookie);
	
	if(cookie.search(/\bCF=/) != -1) {
		cookie = cookie.replace(/\s*\bCF=\d*(:?)/,cf ? 'CF=' + cf + '$1' : '');
	} else {
		cookie += ':CF=' + cf;
	}

	// Make sure we capture "0-" in
	// http://0-scholar.google.co.za.innopac.up.ac.za/...
	var domain = doc.location.href
				.match(/https?:\/\/[^\/]*?([^.\/]*scholar\.google\.[^:\/]+)/i)[1];
	cookie += '; domain=.' + domain +
				'; expires=Sun, 17 Jan 2038 19:14:09 UTC';	//this is what google scholar uses
	doc.cookie = cookie;
	Z.debug('Cookie set to: ' + cookie);
}

//set cookie using Googles Scholar preferences page
function setCookieThroughPrefs(doc, callback) {
	url = doc.location.href.replace(/hl\=[^&]*&?/, "")
			.replace("scholar?",
				"scholar_settings?");
	ZU.doGet(url, function(scisigDoc) {
		var scisig = /<input\s+type="?hidden"?\s+name="?scisig"?\s+value="([^"]+)"/
					.exec(scisigDoc);
		if(!scisig) {
			Z.debug('Could not locate scisig');
			var form = scisigDoc.match(/<form.+?<\/form>/ig);
			if(!form) {
				Z.debug('No forms found on page.');
				Z.debug(scisigDoc);
			} else {
				Z.debug(form.join('\n\n'));
			}
		}
		url = url.replace("scholar_settings?", "scholar_setprefs?")
			+ "&scis=yes&scisf=4&submit=&scisig="+scisig[1];
		//set prefernces
		Z.debug('Submitting settings to Google Scholar: ' + url);
		ZU.doGet(url, function(response) { callback(doc); });
	});
}

function prepareCookie(doc, callback) {
	// Google Scholar always sets GSP
	if(doc.cookie.search(/\bGSP=/) != -1) {
		//check if we need to change cookie
		var m = doc.cookie.match(/\bGSP=[^;]*?\bCF=(\d+)/);
		__old_CF = undefined;
		if(!m || m[1] != '4') {
			__old_CF = (m && m[1]) || '';
			setGSPCookie(doc, '4');
		}
		callback(doc);
	} else {
		Z.debug("Attempting to set cookie through GS Settings page");
		//some proxies do not pass cookies through, so we need to set this by
		//going to the preferences page
		setCookieThroughPrefs(doc, callback);
	}
}

function restoreCookie(doc) {
	if(__old_CF != undefined) {
		setGSPCookie(doc, __old_CF);
	}
}

function decrementCounter(doc) {
	/**Possible race condition!! But there should never be any
	 * lock-ups or detremental effects as long as only one
	 * instance of the translator can be run at a time and
	 * we do not change __old_cookie after setting it initially
	 */
	__result_counter--;
	if(__result_counter<1) restoreCookie(doc);
}

/*****************************
 * Other accessory functions *
 *****************************/

//determine item type from a result node
function determineType(result) {
	var titleHref =  ZU.xpathText(result, './/h3[@class="gs_rt"]/a[1]/@href');

	if(titleHref) {
		if(titleHref.indexOf('/scholar_case?') != -1) {
			return 'case';
		} else if(titleHref.indexOf('/patents?') != -1) {
			return 'patent';
		} else if(titleHref.indexOf('/books?') != -1) {
			return 'book';
		} else if(titleHref.indexOf('/citations?') == -1){
			//not a saved citation
			return 'article';
		}
	}

	/**if there is no link (i.e. [CITATION]), or we're looking at saved citations
	 * we can determine this by the second line.
	 * Patents have the word Patent here
	 * Cases seem to always start with a number
	 * Books just have year after last dash
	 * Articles are assumed to be everything else
	 * 
	 * This is probably not going to work with google scholar in other languages
	 */
	var subTitle = ZU.xpathText(result, './/div[@class="gs_a"]');
	if(!subTitle) return 'article';
	
	subTitle = subTitle.trim();

	if(subTitle.search(/\bpatent\s+\d/i) != -1) {
		return 'patent';
	}

	if(subTitle.search(/^\d/) != -1) {
		return 'case';
	}
	
	if(subTitle.search(/-\s*\d+$/) != -1) {
		return 'book';
	}

	return 'article';
}

function getAttachment(url, title) {
	//try to determine mimeType from title
	var m = title.match(/^\s*\[([^\]]+)\]/);
	if(!m) return;

	m = m[1].toUpperCase();
	var mimeType = getAttachment.mimeTypes[m];
	if(!mimeType) return;

	return {title: title, url: url, mimeType: mimeType};
}

getAttachment.mimeTypes = {
	'PDF': 'application/pdf',
	'DOC': 'application/msword',
	'HTML': 'text/html'
};

/*********************
 * Scraper functions *
 *********************/

function getViableResults(doc) {
	 return ZU.xpath(doc, '//div[@class="gs_r"]\
					[.//div[@class="gs_fl"]/a[contains(@href,"q=info:")\
						or contains(@href,"q=related:")\
						or contains(@onclick, "gs_ocit(event")]]');
}

function scrapeArticleResults(doc, articles) {
	for(var i=0, n=articles.length; i<n; i++) {
		//using closure so we can link a doGet response to a PDF url
		(function(doc, article) {
			ZU.doGet(article.bibtexUrl,
				function(text) {
					var translator = Zotero.loadTranslator('import');
					translator.setTranslator('9cb70025-a888-4a29-a210-93ec52da40d4');
					translator.setString(text);
	
					translator.setHandler('itemDone', function(obj, item) {
						if(item.creators.length) {
							var lastCreatorIndex = item.creators.length-1,
								lastCreator = item.creators[lastCreatorIndex];
							if(lastCreator.lastName === "others" && lastCreator.firstName === "") {
								item.creators.splice(lastCreatorIndex, 1);
							}
						}
						
						//clean author names
						for(var j=0, m=item.creators.length; j<m; j++) {
							if(!item.creators[j].firstName) continue;
	
							item.creators[j] = ZU.cleanAuthor(
								item.creators[j].lastName + ', ' +
									item.creators[j].firstName,
								item.creators[j].creatorType,
								true);
						}

						//attach linked page as snapshot if available
						var snapshotUrl = ZU.xpath(article.result,
							'(.//h3[@class="gs_rt"]/a\
							|.//a[@class="gsc_title_link"])[1]');
						if(snapshotUrl.length) {
							snapshotUrl = snapshotUrl[0].href;
						} else {
							snapshotUrl = undefined;
						}
						
						//don't attach snapshots of the citation view in google scholar
						if(snapshotUrl && snapshotUrl.indexOf('/citations?') != -1) {
							snapshotUrl = undefined;
						}
						
						var linkTitle = ZU.xpathText(article.result,
							'(.//h3[@class="gs_rt"]|.//a[@class="gsc_title_link"])[1]');

						var attachment;
						if(linkTitle && snapshotUrl) {
							//try to get an attachment
							//based on google supplied tag
							attachment = getAttachment(snapshotUrl, linkTitle);
							if(attachment) {
								attachment.title = "Full Text";
							}
						}

						//take a snapshot if we didn't attach as file
						if(snapshotUrl && !attachment) {
							attachment = {
								title: 'Snapshot',
								url: snapshotUrl,
								mimeType: 'text/html'
							};
						}

						//attach files linked on the right
						var pdf = ZU.xpath(article.result,
							'(./div[contains(@class,"gs_fl")]\
								//a[.//span[@class="gs_ctg2"]]\
							|.//div[contains(@class,"gsc_title_ggi")]\
								//a[.//span[@class="gsc_title_ggt"]])');
						for(var i=0, n=pdf.length; i<n; i++) {
							var title = pdf[i].childNodes[0];
							if(title.classList.contains('gs_ctg2')
								|| title.classList.contains('gsc_title_ggt')) {
								//we actually want parent here on saved citation pages
								title = title.parentNode;
							}
							var attach = getAttachment(pdf[i].href, title.textContent);
							
							if(!attach) continue;

							//drop attachment linked by the main link
							// if it's the same url
							if(attachment && attach.url==attachment.url) {
								attachment = undefined;
							}

							item.attachments.push(attach);
						}

						if(attachment) {
							item.attachments.push(attachment);
						}

						//add linked url to the URL field
						if(snapshotUrl) {
							item.url = snapshotUrl;
						}

						item.complete();
					});
	
					translator.translate();
				},
				//restore cookie when the last doGet is done
				function() {
					decrementCounter(doc);
				}
			);
		})(doc, articles[i]);
	}
}

function scrapeCaseResults(doc, cases) {
	for(var i=0, n=cases.length; i<n; i++) {
		var titleString = ZU.xpathText(cases[i].result, './/h3[@class="gs_rt"]')
			|| ZU.xpathText(cases[i].result, './/a[@class="gsc_title_link"]');
		var citeletString = ZU.xpathText(cases[i].result, './/div[@class="gs_a"]')
			|| ZU.xpathText(cases[i].result, './/div[@class="gsc_merged_snippet"]/div[./following-sibling::div]');
		
		if(citeletString) citeletString = ZU.trimInternal(citeletString);
	
		var attachmentFrag = ZU.xpathText(cases[i].result,
			'(.//h3[@class="gs_rt"]/a|.//a[@class="gsc_title_link"])[1]/@href');
		if(attachmentFrag.indexOf('/citations?') != -1) {
			attachmentFrag = null;
			//build attachment link when importing from saved citations
			var caseId = ZU.xpathText(cases[i].result, '(.//div[@class="gs_fl"]\
				/a[contains(@href,"cites=") or contains(@href,"about=")]/@href)[1]');
			if(caseId) caseId = caseId.match(/\b(?:cites|about)=(\d+)/);
			if(caseId) caseId = caseId[1];
			if(caseId) {
				attachmentFrag = '/scholar_case?case=' + caseId;
			}
		}
		
		if (attachmentFrag) {
			var attachmentLinks = [attachmentFrag];
		} else {
			var attachmentLinks = [];
		}
	
		// Instantiate item factory with available data
		var factory = new ItemFactory(citeletString, attachmentLinks,
										titleString, cases[i].bibtexUrl);
	
		if (!factory.hasUsefulData()) {
			decrementCounter(doc)
			continue;
		}

		factory.getCourt();
		factory.getVolRepPag();
		if (factory.hasReporter()) {
			// If we win here, we get by without fetching the BibTeX object at all.
			factory.saveItem();
			decrementCounter(doc);
			continue;
		} else {
			//use closure since this is asynchronous
			(function(doc, factory) {
				ZU.doGet(factory.bibtexLink, function(text) {
					//check if the BibTeX file has title
					if(!text.match(/title={{}}/i)) {
						var bibtexTranslator = Zotero.loadTranslator("import");
						//BibTeX
						bibtexTranslator.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4");
						bibtexTranslator.setString(text);
		
						bibtexTranslator.setHandler("itemDone", function(obj, item) {
							item.attachments = factory.getAttachments("Page");
							item.complete();
						});
		
						bibtexTranslator.translate();
					} else {
						// If BibTeX is empty, this is some kind of case, if anything.
						// Metadata from the citelet, supplemented by the target
						// document for the docket number, if possible.
						if (!factory.hasReporter() && factory.attachmentLinks[0]) {
							//fetch docket from the case page
							__result_counter++;
							(function(doc, factory) {
								ZU.processDocuments(factory.attachmentLinks[0], 
									function(doc) {
										factory.getDocketNumber(doc);
										factory.saveItem();
									},
									function() {
										decrementCounter(doc);
									});
							})(doc, factory);
						} else {
							factory.saveItem();
						}
					}
				},
				function() {
					//restore cookie when the last doGet is done
					decrementCounter(doc);
				});
			})(doc, factory);
		}
	}
}

function scrapePatentResults(doc, patents) {
	for(var i=0, n=patents.length; i<n; i++) {
		(function(doc, bibtexUrl, result) {
			ZU.doGet(bibtexUrl, function(text) {
				var bibtexTranslator = Zotero.loadTranslator("import");
				//BibTeX
				bibtexTranslator.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4");
				bibtexTranslator.setString(text);

				bibtexTranslator.setHandler("itemDone", function(obj, item) {
					item.itemType = 'patent';

					//fix case for patent titles in all upper case
					if(item.title.toUpperCase() == item.title) {
						item.title = ZU.capitalizeTitle(item.title);
					}

					//authors are inventors
					for(var i=0, n=item.creators.length; i<n; i++) {
						item.creators[i].creatorType = 'inventor';
					}

					//country and patent number end up in extras
					if(item.extra) {
						var m = item.extra.split(/\s*Patent\s*/i);
						if(m.length == 2) {
							item.country = m[0];
							item.patentNumber = m[1];
							delete item.extra;
						}
					}

					//attach google patents page
					var attachmentDone = false;
					if(item.patentNumber && item.country) {
						attachmentDone = true;
						item.attachments.push({
							title: 'Google Patents PDF',
							url: 'http://patentimages.storage.googleapis.com/pdfs/'
								+ item.country.toUpperCase()
								+ item.patentNumber.replace(/\D+/g, '')
								+ '.pdf',
							mimeType: 'application/pdf'
						});
					}
					
					var patentUrl;
					if(!attachmentDone && result) {
						patentUrl = ZU.xpathText(result,
							'(.//h3[@class="gs_rt"]/a[1]|//a[@class="gsc_title_link"])[1]/@href');
					}
					
					if(patentUrl) {
						item.attachments.push({
							title: 'Google Patents page',
							url: patentUrl,
							mimeType: 'text/html'
						});
					}

					item.complete();
				});

				bibtexTranslator.translate();
			},
			function() {
				decrementCounter(doc);
			});
		})(doc, patents[i].bibtexUrl, patents[i].result);

/** Until SOP is "fixed" we'll scrape as much information as we can from the
 * result block instead of using Google Patents translator
		var patentUrl = ZU.xpathText(patents[i].result,
									'.//h3[@class="gs_rt"]/a[1]/@href');

		if(patentUrl) {
			(function(doc, url) {
				ZU.processDocuments(url, function(doc) {
						/**This is broken. Google Patents cannot access doc,
						 * due to SOP.
						 * /
						var translator = Zotero.loadTranslator('web');
						//Google Patents
						translator.setTranslator('d71e9b6d-2baa-44ed-acb4-13fe2fe592c0');
						translator.setDocument(doc);
						translator.translate();
					},
					function() {
						decrementCounter(doc);
					});
			})(doc, patentUrl);
		} else {
			/**TODO: handle patents without links (if those even exist)* /
			decrementCounter(doc);
		}
 */
	}
}


function doWeb(doc, url) {
	var type = detectWeb(doc, url);
	if(type != 'multiple' && url.indexOf('/citations?') != -1) {
		//individual saved citation
		var citationKey = ZU.xpathText(doc,
			'(//div[@id="gsc_md_cont"]/form|//form[@id="gsc_eaf"])[1]\
				/input[@name="s"]/@value'
		);
		if(!citationKey) throw new Error("Could not find citation key");
		
		var data = {
			bibtexUrl: '/citations?view_op=export_citations&hl=en&citilm=1&cit_fmt=0'
				+ '&citation_for_view=' + citationKey
				+ '&s=' + citationKey,
			result: doc.getElementById('gsc_ccl')
		};
		
		switch(type) {
			case 'patent':
				scrapePatentResults(doc, [data]);
			break;
			case 'case':
				scrapeCaseResults(doc, [data]);
			break;
			case 'book':
			case 'journalArticle':
				scrapeArticleResults(doc, [data]);
		}
	} else if(type == 'case') {
		// Invoke the case or the listing scraper, as appropriate.
		// In a listings page, this forces use of bibtex data and English page version
		scrapeCase(doc, url);
	} else {
		/**Having RefWorks set in the preferences as the default citation export
		 * producess export links, which don't work very well with our translator.
		 * Having no citation manager, does not generate export links at all.
		 * We should always be able to build bibtex links from the Related articles
		 * link.
		 */
		var results = getViableResults(doc);

		var items = new Object();
		var resultDivs = new Object();
		var bibtexUrl;
		for(var i=0, n=results.length; i<n; i++) {
			bibtexUrl = ZU.xpathText(results[i],
					'.//div[@class="gs_fl"]/a[contains(@href,"q=info:")\
						or contains(@href,"q=related:")][1]/@href');
			if(bibtexUrl) {
				bibtexUrl = bibtexUrl.replace(/\/scholar.*?\?/,'/scholar.bib?')
					.replace(/=related:/,'=info:')
					+ '&ct=citation&cd=1&output=citation';
			} else {
				Z.debug('Could not find a good link to construct BibTeX URL\n'
					+ 'Creating BibTeX URL from scratch');
				bibtexUrl = ZU.xpathText(results[i],
					'.//div[@class="gs_fl"]/a[contains(@onclick, "gs_ocit(event")]\
						[1]/@onclick');
				var id;
				if(!bibtexUrl
					|| !(id = bibtexUrl.match(/gs_ocit\(event,\s*(['"])([^)]+?)\1/)) ) {
					if(!bibtexUrl) Z.debug('onclick not found');
					else Z.debug('Could not determine id from: ' + bibtexUrl);
					Z.debug('Could not build a BibTeX URL');
					continue;
				}
				
				bibtexUrl = '/scholar.bib?q=info:' + id[2]	
					+ ':scholar.google.com/&ct=citation&cd=1&output=citation';
				Z.debug('Constructed BibTeX URL: ' + bibtexUrl);
			}
			items[bibtexUrl] = ZU.xpathText(results[i], './/h3[@class="gs_rt"]');

			//keep the result div for extra information
			resultDivs[bibtexUrl] = results[i];
		}

		Zotero.selectItems(items, function(selectedItems) {
			if(!selectedItems) return true;

			//different types are handled differently
			var selectedArticles = new Array();
			var selectedCases = new Array();
			var selectedPatents = new Array();
			var selectedBooks = new Array()

			for(var i in selectedItems) {
				/**Global counter so we know when we can finally restore original
				 * cookie.
				 */
				__result_counter++;
				switch(determineType(resultDivs[i])) {
					case 'case':
						selectedCases.push({bibtexUrl: i, result: resultDivs[i]});
						break;
					case 'book':
						selectedBooks.push({bibtexUrl: i, result: resultDivs[i]});
						break;
					case 'patent':
						//patents cannot be supported at this time due to SPO
						selectedPatents.push({bibtexUrl: i, result: resultDivs[i]});
						//__result_counter--;
						break;
					case 'article':
					default:
						selectedArticles.push({bibtexUrl: i, result: resultDivs[i]});
				}
			}

			prepareCookie(doc, function(){
				scrapeAll(doc, {
					articles: selectedArticles,
					cases: selectedCases,
					patents: selectedPatents,
					books: selectedBooks
				});
			});
		});
	}
}

function scrapeAll(doc, selected) {
	scrapeArticleResults(doc, selected.articles);
	scrapeCaseResults(doc, selected.cases);
	scrapePatentResults(doc, selected.patents);
	//for now, handle books the same way as articles, since they are on
	//a different domain
	scrapeArticleResults(doc, selected.books);
}

/*
 * #########################
 * ### Scraper Functions ###
 * #########################
 */
var scrapeCase = function (doc, url) {
	// Citelet is identified by
	// id="gsl_reference"
	var refFrag = doc.evaluate('//div[@id="gsl_reference"] | //div[@id="gs_reference"]',
					doc, null, XPathResult.ANY_TYPE, null).iterateNext();
	if (refFrag) {
		// citelet looks kind of like this
		// Powell v. McCormack, 395 US 486 - Supreme Court 1969
		var item = new Zotero.Item("case");
		var factory = new ItemFactory(refFrag.textContent, [url]);
		factory.repairCitelet();
		factory.getDate();
		factory.getCourt();
		factory.getVolRepPag();
		if (!factory.hasReporter()) {
			// Look for docket number in the current document
			factory.getDocketNumber(doc);
		}
		factory.getTitle();
		factory.saveItem();
	}
};


/*
 * ####################
 * ### Item Factory ###
 * ####################
 */

var ItemFactory = function (citeletString, attachmentLinks, titleString, bibtexLink) {
	// var strings
	this.v = {};
	this.v.title = titleString;
	this.v.number = false;
	this.v.court = false;
	this.v.extra = false;
	this.v.date = undefined;
	this.v.jurisdiction = false;
	this.v.docketNumber = false;
	this.vv = {};
	this.vv.volRepPag = [];
	// portable array
	this.attachmentLinks = attachmentLinks;
	// working strings
	this.citelet = citeletString;
	this.bibtexLink = bibtexLink;
	this.bibtexData = undefined;
	this.trailingInfo = false;
	// simple arrays of strings
	this.hyphenSplit = false;
	this.commaSplit = false;
};


ItemFactory.prototype.repairCitelet = function () {
	if (!this.citelet.match(/\s+-\s+/)) {
		this.citelet = this.citelet.replace(/,\s+([A-Z][a-z]+:)/, " - $1");
	}
};


ItemFactory.prototype.repairTitle = function () {
	// All-caps words of four or more characters probably need fixing.
	if (this.v.title.match(/(?:[^a-z]|^)[A-Z]{4,}(?:[^a-z]|$)/)) {
		this.v.title = ZU.capitalizeTitle(this.v.title.toLowerCase())
						.replace(/([^0-9a-z])V([^0-9a-z])/, "$1v$2");
	}
};


ItemFactory.prototype.hasUsefulData = function () {
	if (this.getDate()) {
		return true;
	}
	if (this.hasInitials()) {
		return true;
	}
	return false;
};


ItemFactory.prototype.hasInitials = function () {
	if (this.hyphenSplit.length && this.hyphenSplit[0].match(/[A-Z] /)) {
		return true;
	}
	return false;
};


ItemFactory.prototype.hasReporter = function () {
	if (this.vv.volRepPag.length > 0) {
		return true;
	}
	return false;
};


ItemFactory.prototype.getDate = function () {
	var i, m;
	// Citelet parsing, step (1)
	if (!this.hyphenSplit) {
		this.hyphenSplit = this.citelet.split(/\s+-\s+/);
		this.trailingInfo = this.hyphenSplit.slice(-1);
	}
	if (!this.v.date && this.v.date !== false) {
		this.v.date = false;
		for (i = this.hyphenSplit.length - 1; i > -1; i += -1) {
			m = this.hyphenSplit[i].match(/(?:(.*)\s+)*([0-9]{4})$/);
			if (m) {
				this.v.date = m[2];
				if (m[1]) {
					this.hyphenSplit[i] = m[1];
				} else {
					this.hyphenSplit[i] = "";
				}
				this.hyphenSplit = this.hyphenSplit.slice(0, i + 1);
				break;
			}
		}
	}
	return this.v.date;
};


ItemFactory.prototype.getCourt = function () {
	var s, m;
	// Citelet parsing, step (2)
	s = this.hyphenSplit.pop().replace(/,\s*$/, "").replace(/\u2026\s*$/, "Court");
	m = s.match(/(?:([a-zA-Z]+):\s*)*(.*)/);
	if (m) {
		this.v.court = m[2].replace(/_/g, " ");
		if (m[1]) {
			this.v.extra = "{:jurisdiction: " + m[1] + "}";
		}
	}
	return this.v.court;
};


ItemFactory.prototype.getVolRepPag = function () {
	var i, m;
	// Citelet parsing, step (3)
	if (this.hyphenSplit.length) {
		this.commaSplit = this.hyphenSplit.slice(-1)[0].split(/\s*,\s+/);
		var gotOne = false;
		for (i = this.commaSplit.length - 1; i > -1; i += -1) {
			m = this.commaSplit[i].match(/^([0-9]+)\s+(.*)\s+(.*)/);
			if (m) {
				var volRepPag = {};
				volRepPag.volume = m[1];
				volRepPag.reporter = m[2];
				volRepPag.pages = m[3].replace(/\s*$/, "");
				this.commaSplit.pop();
				if (!volRepPag.pages.match(/[0-9]$/) && (i > 0 || gotOne)) {
					continue;
				}
				gotOne = true;
				this.vv.volRepPag.push(volRepPag);
			} else {
				break;
			}
		}
	}
};


ItemFactory.prototype.getTitle = function () {
	// Citelet parsing, step (4) [optional]
	if (this.commaSplit) {
		this.v.title = this.commaSplit.join(", ");
	}
};


ItemFactory.prototype.getDocketNumber = function (doc) {
	var docNumFrag = doc.evaluate(
		'//center[preceding-sibling::center//h3[@id="gsl_case_name"]]\
		| //div[@class="gsc_value" and preceding-sibling::div[text()="Docket id"]]',
		doc, null, XPathResult.ANY_TYPE, null).iterateNext();
	if (docNumFrag) {
		this.v.docketNumber = docNumFrag.textContent
								.replace(/^\s*[Nn][Oo](?:.|\s+)\s*/, "")
								.replace(/\.\s*$/, "");
	}
};

ItemFactory.prototype.getAttachments = function (doctype) {
	var i, ilen, attachments;
	attachments = [];
	for (i = 0, ilen = this.attachmentLinks.length; i < ilen; i += 1) {
		attachments.push({title:"Google Scholar Linked " + doctype, type:"text/html",
							  url:this.attachmentLinks[i]});
	}
	return attachments;
};


ItemFactory.prototype.pushAttachments = function (doctype) {
	this.item.attachments = this.getAttachments(doctype);
};

/*
ItemFactory.prototype.getBibtexData = function (callback) {
	if (!this.bibtexData) {
		if (this.bibtexData !== false) {
			Zotero.Utilities.doGet(this.bibtexLink, function(bibtexData) {
				if (!bibtexData.match(/title={{}}/)) {
					this.bibtexData = bibtexData;
				} else {
					this.bibtexData = false;
				}
				callback(this.bibtexData);
			});
			return;
		}
	}
	callback(this.bibtexData);
};
*/

ItemFactory.prototype.saveItem = function () {
	var i, ilen, key;
	if (this.v.title) {
		this.repairTitle();
		if (this.vv.volRepPag.length) {
			var completed_items = [];
			for (i = 0, ilen = this.vv.volRepPag.length; i < ilen; i += 1) {
				this.item = new Zotero.Item("case");
				for (key in this.vv.volRepPag[i]) {
					if (this.vv.volRepPag[i][key]) {
						this.item[key] = this.vv.volRepPag[i][key];
					}
				}
				this.saveItemCommonVars();
				if (i === (this.vv.volRepPag.length - 1)) {
					this.pushAttachments("Judgement");
				}
				this.item.itemID = "" + bogusItemID;
				bogusItemID += 1;
				completed_items.push(this.item);
			}
			for (i = 0, ilen = completed_items.length; i < ilen; i += 1) {
				for (j = 0, jlen = completed_items.length; j < jlen; j += 1) {
					if (i === j) {
						continue;
					}
					completed_items[i].seeAlso.push(completed_items[j].itemID);
				}
				completed_items[i].complete();
			}
		} else {
			this.item = new Zotero.Item("case");
			this.saveItemCommonVars();
			this.pushAttachments("Judgement");
			this.item.complete();
		}
	}
};


ItemFactory.prototype.saveItemCommonVars = function () {
	for (key in this.v) {
		if (this.v[key]) {
			this.item[key] = this.v[key];
		}
	}
};


/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://scholar.google.com/scholar?q=marbury&hl=en&btnG=Search&as_sdt=1%2C22&as_sdtp=on",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://scholar.google.com/scholar?hl=en&q=kelo&btnG=Search&as_sdt=0%2C22&as_ylo=&as_vis=0",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://scholar.google.com/scholar?hl=en&q=smith&btnG=Search&as_sdt=0%2C22&as_ylo=&as_vis=0",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://scholar.google.com/scholar?hl=en&q=view+of+the+cathedral&btnG=Search&as_sdt=0%2C22&as_ylo=&as_vis=0",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://scholar.google.com/scholar?hl=en&q=clifford&btnG=Search&as_sdt=0%2C22&as_ylo=&as_vis=0",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://scholar.google.com/scholar_case?case=9834052745083343188&q=marbury+v+madison&hl=en&as_sdt=2,5",
		"items": [
			{
				"itemType": "case",
				"creators": [],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Google Scholar Linked Judgement",
						"type": "text/html",
						"url": false
					}
				],
				"volume": "5",
				"reporter": "US",
				"pages": "137",
				"title": "Marbury v. Madison",
				"court": "Supreme Court",
				"date": "1803",
				"itemID": "1",
				"libraryCatalog": "Google Scholar"
			}
		]
	}
]
/** END TEST CASES **/