summaryrefslogtreecommitdiff
path: root/content-script/check.js
blob: 0644084d6a486f3e9b02cb1d296548f502eb266f (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
(function(){

	var listening = false, loaded = false
	var TYPES = { Status: 0, Connected: 1, Disconnected: 2, Image: 3, Text: 4 }
	var PUNCTUATION_REGEX = /[\[\]\{\}]/g  // i.e. resembles code
	var NULL_ARRAY = []
	var seen = {}
	const toArray = (a) => Array.prototype.slice.call(a)
	const $ = (s) => document.querySelector(s)
	const $$ = (s) => document.querySelectorAll(s)

	function init () {
		if (window.location.href.indexOf("lvh.me") !== -1 || window.location.href.indexOf("localhost") !== -1) return
		bind()
	}
	function bind () {
		chrome.extension.onMessage.addListener(onMessage)
		chrome.extension.sendMessage({ type: TYPES.Status }, gotStatus)
	}
	function gotStatus (response) {
		console.log('got status', response)
		if (response && response.status === "on") {
			console.log('its on', loaded)
			if (! loaded) {
				console.log('started')
				setTimeout(() => { start() }, 5000)
				// send(document.body.innerText)
				// setInterval(function(){
				// 	send(document.body.innerText)
				// }, 10000)
			}
			loaded = true
		}
	}
	function onMessage (request, sender, sendResponse) { 
		switch (request.method) {
			case 'start':
				start()
				break
			case 'stop':
				listening = false
				break
		}
	}
	function start(){
		console.log(window.location.href, listening)
		if (window.location.href.indexOf('schol' + 'ar' + '.go' + 'og' + 'le') === -1) return
		if (listening) return
		listening = true
		energize()
	}

	function energize(){
		console.log('energize')
		const records = toArray($$(".gs_r")).map((el, i) => {
		  let data = {}
		  let link = el.querySelector("h3 a")
		  if (link) {
		    data.link = link.href
		  }
		  let pdfLink = el.querySelector(".gs_or_ggsm a")
		  if (pdfLink) {
		    data.pdfLink = pdfLink.href
		  }
		  let attribution = el.querySelector('.gs_a')
		  if (attribution) {
		    data.attribution = attribution.innerText
		    data.attributionLinks = toArray(attribution.querySelectorAll('a')).map(a => ({
		      href: a.href,
		      name: a.innerText,
		    }))
		  }
		  let snippet = el.querySelector('.gs_a')
		  if (snippet) {
		    data.snippet = snippet.innerText
		  }
		  let citationLink = el.querySelector('.gs_fl a:nth-of-type(3)')
		  if (citationLink && citationLink.innerText.match('Cited by')) {
		    data.citationLink = citationLink.href
		    data.citationCount = parseInt(citationLink.innerText.replace(/^\s*Cited by /, ''), 10) || -1
		  }
		  return data
		})
		let record = {
			title: document.querySelector('title').innerText,
			url: window.location.href,
			records: records,
		}
	  send(JSON.stringify(record))
		let nextLink = $("#gs_n td:last-child a")
		if (nextLink) {
			setTimeout(() => { nextLink.click() }, 19000 + (Math.random() * 21000))
		}
		send("done")
	}
	function send (text) {
		chrome.extension.sendMessage({ type: TYPES.Text, data: text }, function(){})
	}

	init()

})()