-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
69 lines (58 loc) · 1.83 KB
/
main.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
var gmail;
var $;
function refresh(f) {
if( (/in/.test(document.readyState)) || (typeof Gmail === undefined) ) {
setTimeout('refresh(' + f + ')', 10);
} else {
f();
}
}
var main = function(){
gmail = new Gmail();
console.log('Hello,', gmail.get.user_email());
registerToComposeEvent(gmail)
}
function registerToSendEvent(gmail){
gmail.observe.before('send_message', function(url, body, data, xhr){
var body_params = xhr.xhrParams.body_params;
console.log(data);
console.log(data.body);
});
}
function registerToComposeEvent(gmail){
// DOM observers
gmail.observe.on("compose", function(compose, type) {
// type can be compose, reply or forward
console.log('api.dom.compose object:', compose, 'type is:', type ); // gmail.dom.compose object
// Add compose button
addComposeButton(gmail);
});
}
function addComposeButton(gmail){
var compose_ref = gmail.dom.composes()[0];
gmail.tools.add_compose_button(compose_ref, '<button class="T-I J-J5-Ji T-I-atl L3 T-I-Zf-aw2"> Analyse </button>', function() {
// Code here
//Fire webservice call and show results
console.log(compose_ref.$el.context.innerText);
callWatsonAPI(compose_ref.$el.context.innerText);
}, '');
}
function callWatsonAPI(text){
var data = {
"text" : text
};
jQuery.ajax({
type: "POST",
url: "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19",
data: data,
dataType: "json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
success: function(success_data){
console.log(success_data);
}
});
}
refresh(main);