-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
64 lines (56 loc) · 1.92 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
/* User click in the addon icon */
browser.browserAction.onClicked.addListener(_ => {
browser.tabs.create({
'url': './public/config.html'
});
});
// when the used add or delete words, update all the lists (usually called by the script in the page)
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action == 'get_list') {
browser.storage.local.get('list_words').then((data) => {
if (data.list_words)
sendResponse(data.list_words);
else
sendResponse({});
});
// tells the browser to wait async call from sendResponse
return true;
}
});
// normalize how the notifications will be created in the addon
function displayNotification(msg) {
return browser.notifications.create('', {
'type': 'basic',
'iconUrl': browser.extension.getURL('icon.png'),
'title': 'TextFast Notification',
'message': msg
});
}
/* check first time running */
browser.storage.local.get('list_words').then(local_words => {
local_words = local_words.list_words;
// exist some words
if (typeof local_words != 'undefined' && Object.keys(local_words).length !== 0) {
return;
}
// check first time (autoload some words as example)
browser.storage.local.get('tutorial_first_time').then((val) => {
val = val.tutorial_first_time;
if (typeof val == 'undefined') {
fetch(browser.extension.getURL('example.json'))
.then(r => r.json())
.then(json => {
let data = {};
// preparing data
json.forEach(i => data[i.replace] = i.with);
// saveing locally
browser.storage.local.set({'list_words': data})
.then(() => displayNotification('Default shortcuts added to help you.'));
});
}
});
});
// always open config page when the user click the notification
browser.notifications.onClicked.addListener(_ => {
browser.tabs.create({url: browser.extension.getURL('public/config.html')});
});