-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
234 lines (203 loc) · 8.2 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
console.log("background.js processing");
var settings = new Store("settings", {
"speech_enabled": false,
"speech_enabled_live": true,
"speech_enabled_hover_loan": false,
"speech_enabled_hover_lender": true,
"speech_enabled_hover_team": true,
"speech_enabled_startup": false,
"speech_enabled_analyze_loan": true,
"speech_enabled_analyze_loan_relate_to_portfolio": true,
"speech_enabled_analyze_loan_repayment_terms": false,
"speech_enabled_analyze_loan_attributes": true,
"speech_enabled_analyze_loan_partner": false,
"speech_enabled_analyze_lender": true,
"speech_enabled_analyze_team": true,
"speech_enabled_analyze_partner": false,
"add_on_repayment_loan_card": true,
"add_on_always_show_partner_on_lend_tab": true,
"add_on_always_show_partner_on_loan": true,
"add_on_always_show_repayments_on_loan": true,
"add_on_omnibar": true,
"display_atheist_scores": true,
"custom_button_kivalens": true,
"custom_button_all_teams": false,
"custom_button_my_teams": false,
"custom_button_live": false,
"high_months_to_payback": 12,
"low_months_to_payback": 5,
"debug_output_to_console": false
});
if (chrome.pageAction) {
chrome.pageAction.onClicked.addListener(function (tab) {
chrome.tabs.create({url: "options_custom/index.html"})
//chrome.tabs.create({ "url": "chrome://extensions/?options=" + chrome.runtime.id });
})
}
///OMNIBOX
if (settings.toObject().add_on_omnibar) {
chrome.omnibox.onInputChanged.addListener(
function (text, suggest) {
// Add suggestions to an array
var suggestions = [];
suggestions.push({content: 'http://www.kiva.org/lend', description: "Kiva Loan Search"});
suggestions.push({content: 'https://www.kiva.org/portfolio', description: "Kiva Portfolio"});
suggestions.push({
content: 'https://www.kiva.org/portfolio/estimated-repayments',
description: "Kiva Estimated Repayments"
});
// Set first suggestion as the default suggestion
chrome.omnibox.setDefaultSuggestion({description: suggestions[0].description});
// Remove the first suggestion from the array since we just suggested it
suggestions.shift();
// Suggest the remaining suggestions
suggest(suggestions);
}
);
chrome.omnibox.onInputEntered.addListener(
function (text) {
chrome.tabs.getSelected(null, function (tab) {
var url;
if ((text.substr(0, 7) == 'http://') || ((text.substr(0, 8) == 'https://'))) {
url = text;
} else {
url = 'http://www.kiva.org/lend?queryString=' + text;
}
chrome.tabs.update(tab.id, {url: url});
});
}
);
}
//
var newLoansInList =[]
//integration with KivaLens
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request) {
if (request.getFeatures) {
sendResponse({features:['setAutoLendPartners','setAutoLendPCS','getManifest','getVersion','getLenderId','notify','speak']})
}
if (request.getManifest){
sendResponse({manifest: chrome.runtime.getManifest()})
}
if (request.getVersion){
sendResponse({version: chrome.runtime.getManifest().version})
}
if (request.speak){
narrate(request.params)
sendResponse({received:true})
}
if (request.notify){
new Audio('sounds/sweep.mp3').play()
chrome.notifications.create("KLA.KL.notify", {iconUrl: 'icons/icon128.png', type: 'basic', title: 'Notice from KivaLens', message: request.params}, notifyId => {})
sendResponse({received: true})
}
if (request.setAutoLendPartners) { //deprecated
chrome.tabs.create({ url: `https://www.kiva.org/settings/credit?kivalens=true&partner_ids=${request.setAutoLendPartners.join(',')}` })
sendResponse({received:true})
}
if (request.setAutoLendPCS) {
var o = request.setAutoLendPCS
chrome.tabs.create({ url: `https://www.kiva.org/settings/credit?kla=true&partner_ids=${o.partners.join(',')}&countries=${encodeURI(o.countries.join(','))}§ors=${encodeURI(o.sectors.join(','))}` })
sendResponse({received:true})
}
if (request.getLenderId){
chrome.storage.local.get("lender_id", function(result){
sendResponse({lender_id: result.lender_id})
})
}
}
return true; //leave channel open?
})
//background message receiver
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.utterance) {
narrate(request.utterance, request.context, request.follow_up, request.interrupt, sendResponse);
return true; //keeps the channel open for the response;
} else if (request.funct) {
eval(request.funct)(request.params);
sendResponse("Received message to call function.")
} else if (request.cache == 'get'){
sendResponse(get_session_cache(request.key, request.max_age));
} else if (request.cache == 'set'){
set_session_cache(request.key, request.value)
} else if (request.cache == 'clear') {
clear_session_cache();
} else if (request.get_settings){
console.log("background",settings);
sendResponse(settings.toObject());
} else if (request.location_icon){
////"default_popup": "options_custom/index.html"
//needs to show on any matching page, clicking takes you to options.
chrome.tabs.getSelected(null, function (tab) {
chrome.pageAction.show(tab.id);
});
}
console.log(request);
});
var last_utterances = [];
var session_cache = {};
function clear_session_cache(){
session_cache = {};
}
function set_session_cache(key, value){
session_cache[key] = {value: value, added: Date.now()};
}
function get_session_cache(key, max_age){
var entry = session_cache[key];
var day = 1000 * 60 * 60 * 24;
var max_age = max_age || day;
if (entry && (Date.now() - entry.added < max_age)) {
return entry.value;
} else {
return undefined;
}
}
var last_context;
function narrate(utterance, context, follow_up, interrupt, callback) {
if (settings.toObject().speech_enabled === false) {
//cl('Skipped narration');
return;
}
//a "follow up" message must match the same context or it gets skipped.
if (typeof context == 'object') {
context = JSON.stringify(context);
}
if (last_utterances.indexOf(context + utterance) > -1) {
callback && callback("Skipped (just said it): " + utterance);
return; //don't say it;
}
if (follow_up && context != last_context){
callback && callback("Skipped (context switched unexpectedly): " + utterance);
return; //don't say it;
}
last_context = context;
last_utterances.push(context + utterance);
chrome.tts.speak(
utterance,
{
voiceName: 'Google US English',
enqueue: !interrupt,
onEvent: function(ttsEvent) {
console.log(ttsEvent);
if(ttsEvent.type == "error") { console.error("TTS Error:", ttsEvent); }
}
},
function() {
if(chrome.runtime.lastError !== undefined) {
console.error("Runtime error:", chrome.runtime.lastError.message);
}
callback && callback("Spoken: " + utterance);
}
);
if (last_utterances.length > 20) {
last_utterances.shift();
}
}
//happens when the extension loads
//narrate(pick_random(["Let's make the world a better place.","Things are looking better every day!", "Isn't it time to check out Kiva again?"]));
//chrome.storage.local.clear();
if (settings.toObject().speech_enabled_startup) {
narrate("Let's make the world a better place.");
}
//chrome.tts.getVoices(voices => {console.log(voices)})