-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
165 lines (155 loc) · 5.62 KB
/
background.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
// Function to insert API key into code.
function getApiKey()
{
return "key"
}
// Listener
//When the Chrome Extension icon is pressed.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.storage.sync.get("MicrosoftAPIKey", function(apiKey){
newKey = ''
if(apiKey.MicrosoftAPIKey===null){
var newKey = window.prompt('Please Enter Valid Microsoft Text Analyitics API Key: (https://www.microsoft.com/cognitive-services)');
if (newKey != '')
{
chrome.storage.sync.set({"MicrosoftAPIKey":newKey})
}
}
else{
newKey = apiKey["MicrosoftAPIKey"];
}
//THIS IS TO CHECK IF THE KEY IS VALID. HERE WE SIMPLY RUN AN AJAX CALL AND IF WE GET A RESPONSE WE KNOW THE KEY WORKS. IF NOT WE PROMPT FOR ANOTHER KEY.
var params ={
"documents": [{
"language": "en",
"id": "1",
"text": "Test text to see if key works"
}]};
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param( params ),
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key",newKey);
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
data: JSON.stringify(params)
})
.done(function(data) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
ensureSendMessage(tabs[0].id, {greeting: "hello"}, newKey);
})
}).fail(function(a,b,c){
var newKey = window.prompt('Please Enter Valid TEXT-ANALYTICS API key (obtained at https://www.microsoft.com/cognitive-services)');
chrome.storage.sync.set({"MicrosoftAPIKey":newKey})
})
});
})
//Sometimes the button may be pressed however application.js script is not ready or already injected into the tab. Therefore here we ping between files to see if everything is ready to send data.
function ensureSendMessage(tabId, message, newKey, callback){
chrome.tabs.sendMessage(tabId, {ping: true}, function(response){
if(response) { // Content script ready
chrome.tabs.sendMessage(tabId, message, function(response){
if(response["error"]==""){
makeCall(response['text'], response['parentNode'], response['textHTML'], newKey);
}
else {
alert("Selected text was too long. Please make a smaller selection.")
}
});
} else { // No listener on the other end
chrome.tabs.executeScript(tabId, {file: "application.js"}, function(){
if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
throw Error("Unable to inject script into tab " + tabId);
}
// OK, now it's injected and ready
chrome.tabs.sendMessage(tabId, message, function(response){
if(response["error"]==""){
makeCall(response['text'], response['parentNode'], response['textHTML'], newKey);
}
else {
alert("Selected text was too long. Please make a smaller selection.")
}
});
});
}
});
};
//Function that takes the text and formats it and sends off for the AJAX request to microsoft cognitive text API
function makeCall(text, parent, textHTML, newKey){
var plainText = text;
var parentNode = parent;
var textHTML = textHTML;
var params ={
"documents": [{
"language": "en",
"id": "1",
"text": plainText
}]};
var sentiment_object;
var keyPhrases_object;
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param( params ),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key",newKey);
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
// Request body
data: JSON.stringify(params)
})
.done(function(data) {
//sentiment data being returned here
//num from 0-1
sentiment_object = data;
console.log(data);
});
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases?" + $.param( params ),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key",newKey);
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
// Request body
data: JSON.stringify(params)
})
.done(function(data) {
//key phrase data being returned here
//array of strings
keyPhrases_object = data;
console.log(data);
});
function waitForElement(){
if(sentiment_object && keyPhrases_object){
var message = {sentiment: sentiment_object, keyPhrases: keyPhrases_object, text: plainText, textHTML: textHTML};
// sends code to create new chrome tab with all information compiled
createPage(message);
}
else{
setTimeout(function(){
waitForElement();
},500);
}
}
waitForElement();
}
function createPage(params)
{
//Create New Chrome Tab
chrome.tabs.create({"url": 'response.html'}, function(tab){
//Wait for tab to be fully loaded before doing anything
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tab.id, params)
})
}
})
})
}