-
Notifications
You must be signed in to change notification settings - Fork 18
/
background.js
113 lines (87 loc) · 2.75 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
/*---------------------------------------------------------------
>>> BACKGROUND
-----------------------------------------------------------------
# Global variable
# Localization
# Listeners
# Storage change
# Message
---------------------------------------------------------------*/
/*--------------------------------------------------------------
# GLOBAL VARIABLE
--------------------------------------------------------------*/
var extension = {
locale: {},
storage: {}
};
/*--------------------------------------------------------------
# LOCALIZATION
--------------------------------------------------------------*/
function getLocalization(code) {
var language = code || navigator.language;
if (language.indexOf('en') === 0) {
language = 'en';
}
fetch(chrome.runtime.getURL('_locales/' + language + '/messages.json')).then(function (response) {
if (response.ok) {
response.json().then(function (response) {
extension.locale = {};
for (var key in response) {
extension.locale[key] = response[key].message;
}
});
} else {
fetch(chrome.runtime.getURL('_locales/en/messages.json')).then(function (response) {
if (response.ok) {
response.json().then(function (response) {
extension.locale = {};
for (var key in response) {
extension.locale[key] = response[key].message;
}
});
}
});
}
});
}
/*---------------------------------------------------------------
# LISTENERS
---------------------------------------------------------------*/
/*--------------------------------------------------------------
# STORAGE CHANGE
--------------------------------------------------------------*/
chrome.storage.onChanged.addListener(function (changes) {
for (var key in changes) {
extension.storage[key] = changes[key].newValue;
}
getLocalization(extension.storage.language);
});
/*---------------------------------------------------------------
# MESSAGE
---------------------------------------------------------------*/
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var action = message.action;
if (action === 'get-locale') {
var response = extension.locale;
sendResponse(response);
return response;
} else if (action === 'tab-connected') {
var response = {
url: new URL(sender.tab.url).hostname,
id: sender.tab.id
};
sendResponse(response);
return response;
} else if (action === 'options-page-connected') {
sendResponse({
isPopup: sender.hasOwnProperty('tab') === false
});
}
});
/*--------------------------------------------------------------
# INITIALIZATION
--------------------------------------------------------------*/
chrome.storage.local.get(function (items) {
extension.storage = items;
getLocalization(items.language);
});