-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackground.js
156 lines (137 loc) · 4.77 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
// Background script for AWS AI Services Chrome Extension
// Ceyhun OZGUN
// August 2018
// https://github.com/ceyhunozgun/awsAIChromeExtension
(function() {
var status;
var pollyPreferences;
var translatePreferences;
var readSelectionMenuId = chrome.contextMenus.create({
"title": "Read Selected Text Using Amazon Polly",
"contexts": ["selection"]
});
var translateSelectionMenuId = chrome.contextMenus.create({
"title": "Translate Selected Text Using Amazon Translate",
"contexts": ["selection"]
});
var setAuthyTokenMenuId = chrome.contextMenus.create({
"title": "Set Authy Token From Camera Using Amazon Rekognition",
"contexts": ["editable"]
});
var detectTextFromImgElementMenuId = chrome.contextMenus.create({
"title": "Detect Text Using Amazon Rekognition",
"contexts": ["image"]
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId == readSelectionMenuId)
chrome.tabs.sendMessage(tab.id, {op: 'readSelection', selectedText: info.selectionText});
else if (info.menuItemId == translateSelectionMenuId)
chrome.tabs.sendMessage(tab.id, {op: 'translateSelection', selectedText: info.selectionText});
else if (info.menuItemId == setAuthyTokenMenuId)
chrome.tabs.sendMessage(tab.id, {op: 'setAuthyToken'});
else if (info.menuItemId == detectTextFromImgElementMenuId)
chrome.tabs.sendMessage(tab.id, {op: 'detectTextFromImgElement'});
});
chrome.runtime.onInstalled.addListener(function() {
});
chrome.runtime.onMessage.addListener( function (message, sender, sendResponse) {
if (message.op === 'saveCredentials' ) {
checkAWSCredentials(message.data.awsCredentials, function (err, data) {
if (err)
sendResponse({err: err, data: data});
else
chrome.storage.sync.set(message.data, function() {
sendResponse({});
});
});
return true;
}
else if (message.op === 'getCredentials' ) {
chrome.storage.sync.get(message.dataKey,
function(data) {
sendResponse(data);
}
);
return true;
}
else if (message.op === 'getStatus') {
sendResponse({
data: {
status: status,
pollyPreferences: pollyPreferences,
translatePreferences: translatePreferences
}
});
return true;
}
else if (message.op === 'synthesizeSpeech' ) {
storePollyPreferences(message.lang);
AWSAIServices.synthesizeSpeech(message.text, message.lang, function (err, audioStream) {
var uInt8Array = new Uint8Array(audioStream);
sendResponse({ err: err, data: Uint8ToBase64(uInt8Array)});
});
return true;
}
else if (message.op === 'detectTextInImage' ) {
var base64 = message.data;
var uInt8Array = base64ToByteArray(base64);
var blob = new Blob(uInt8Array);
AWSAIServices.detectTextInImage(blob, function (err, txt) {
sendResponse({ err: err, data: txt});
});
return true;
}
else if (message.op === 'translateText' ) {
storeTranslatePreferences(message.sourceLangCode, message.targetLangCode);
AWSAIServices.translateText(message.text, message.sourceLangCode, message.targetLangCode, function (err, txt) {
sendResponse({ err: err, data: txt});
});
return true;
}
});
function storePollyPreferences(lang) {
pollyPreferences = { lang: lang };
chrome.storage.sync.set({pollyPreferences: pollyPreferences});
}
function storeTranslatePreferences(srcLangCode, trgtLangCode) {
translatePreferences = { sourceLangCode: srcLangCode, targetLangCode: trgtLangCode};
chrome.storage.sync.set({translatePreferences: translatePreferences});
}
function checkAWSCredentials(awsCredentials, callback) {
AWSAIServices.init(awsCredentials.awsAccessKeyId, awsCredentials.awsSecretAccessKey, awsCredentials.awsRegion);
AWSAIServices.synthesizeSpeech('Welcome', 'en', function(err, data) {
if (err)
callback(err, null);
else {
status = 'OK';
callback(null, data);
}
});
}
function initAWSCredentials() {
chrome.storage.sync.get('awsCredentials', function(data) {
var awsCredentials = data.awsCredentials;
if (awsCredentials && awsCredentials.awsAccessKeyId !== '' && awsCredentials.awsSecretAccessKey !== '' && awsCredentials.awsRegion !== '') {
AWSAIServices.init(awsCredentials.awsAccessKeyId, awsCredentials.awsSecretAccessKey, awsCredentials.awsRegion);
status = 'OK';
chrome.storage.sync.get('pollyPreferences', function(res) {
pollyPreferences = res.pollyPreferences;
});
chrome.storage.sync.get('translatePreferences', function(res) {
translatePreferences = res.translatePreferences;
});
}
else
status = 'Not configured AWS Credentials. Please configure.';
}
);
}
function startExtension() {
if (!chrome.storage) {
setTimeout(startExtension, 1000);
return;
}
initAWSCredentials();
}
setTimeout(startExtension, 1000);
})();