-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
80 lines (55 loc) · 2.64 KB
/
script.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
function start() {
document.getElementById('buttonB').addEventListener('click', function() {
var arrayOfSentences = []; // stores the sentences within the inputted paragraph
var previous = 0; // keeps track of where to begin new sentence
var count = 0; // keeps track of the number of sentences
var input = document.getElementsByName("tagB")[0].value; // takes in input from textarea
for (var i = 0; i<input.length-1; i++) {
if (input[i]==='.') {
arrayOfSentences.push(input.substring(previous, i+1));
count++;
previous += i + 1 - previous;
}
}
var thing = function(sentence) { gapi.client.init({
'apiKey': 'AIzaSyAAUG25gfbw5GcFuMSSt-FeRHMSoq-7OhA',
'discoveryDocs': ['https://language.googleapis.com/$discovery/rest?version=v1beta1']
}).then(function() {
return gapi.client.language.documents.analyzeEntities({
"document":{
"type":"PLAIN_TEXT",
"content": sentence // sentences don't work :(
},
"encodingType":"UTF8"
});
}).then(function(resp) {
// send to dataLayer here
console.log(resp.result);
//resp.body displays json as string
var len = 0;
for (var o in resp.result.entities) {
len++;
}
// adds a bullet point
document.getElementById('point').innerHTML += "• ";
// split with commas
for (var i=0; i<len; i++) {
document.getElementById('point').innerHTML += resp.result.entities[i].mentions[0].text.content;
if (i<len-1) {
document.getElementById('point').innerHTML += ", ";
}
}
// adds new line
document.getElementById('point').innerHTML += "<br>";
// *** spits out wikipedia link: document.getElementById('point').innerHTML += resp.result.entities[0].metadata.wikipedia_url;
// *** correct format for finding element in JSON console file: document.getElementById('point').innerHTML += resp.result.entities[0].mentions[0].text.content;
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
};
for (var i=0; i<count; i++) {
thing(arrayOfSentences[i]);
}
});
};
gapi.load('client', start);