forked from melink14/rikaikun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
188 lines (178 loc) · 5.61 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
chrome.browserAction.onClicked.addListener(rcxMain.inlineToggle);
chrome.tabs.onSelectionChanged.addListener(rcxMain.onTabSelect);
chrome.runtime.onMessage.addListener(
function (request, sender, response) {
switch (request.type) {
case 'enable?':
console.log('enable?');
rcxMain.onTabSelect(sender.tab.id);
// chrome.management.getAll(function(result) {
// console.log(result)
// })
break;
case 'xsearch':
console.log('xsearch');
var e = rcxMain.search(request.text, request.dictOption);
response(e);
break;
/** case 'nextDict':
console.log('nextDict');
rcxMain.nextDict();
break;*/
case 'resetDict':
console.log('resetDict');
rcxMain.resetDict();
break;
case 'translate':
console.log('translate');
var e = rcxMain.dict.translate(request.title);
response(e);
break;
case 'makehtml':
console.log('makehtml');
var html = rcxMain.dict.makeHtml(request.entry);
response(html);
break;
case 'switchOnlyReading':
console.log('switchOnlyReading');
rcxMain.config.onlyreading = !rcxMain.config.onlyreading;
chrome.storage.sync.set({
'onlyreading': rcxMain.config.onlyreading,
});
break;
case 'copyToClip':
console.log('copyToClip');
rcxMain.copyToClip(sender.tab, request.entry);
break;
case 'playTTS':
console.log('playTTS');
TTS.prototype.play(request.text);
break;
default:
console.log(request);
}
}
);
rcxMain.config = {};
var optionsList = [
"copySeparator",
"disablekeys",
"highlight",
"kanjicomponents",
"kanjiInfo",
"lineEnding",
"maxClipCopyEntries",
"minihelp",
"onlyreading",
"popupcolor",
"popupDelay",
"popupLocation",
"textboxhl",
"ttsEnabled",
"showOnKey",
];
/** Get option data from cloud and initialize into memory. */
chrome.storage.sync.get(optionsList,
function (items) {
initializeConfigFromCloudOrLocalStorageOrDefaults(items);
// Save right away incase we initialized from localStorage or defaults.
saveOptionsToCloudStorage();
})
/**
* Initializes config with values from one of the following sources in order:
* 1. Cloud Storage, 2. Local Storage, and 3. Default
*
* @param {Object<string, boolean|number|string>} cloudStorage
* config values retrieved from cloud storage.
*/
function initializeConfigFromCloudOrLocalStorageOrDefaults(cloudStorage) {
/**
* Initializes option `key` in `rcxMain.config` from `cloudStorage`, falling
* back to `localStorage` and finally defaulting to `defaultValue`.
*
* @param {string} key
* @param {boolean|number|string} defaultValue
*/
function initConfig(key, defaultValue) {
var currentValue = cloudStorage[key] || normalizeStringValue(localStorage[key]);
if (currentValue === undefined) {
currentValue = defaultValue;
}
rcxMain.config[key] = currentValue;
}
initConfig('copySeparator', 'tab');
initConfig('disablekeys', false);
initConfig('highlight', true);
initConfig('kanjicomponents', true);
initConfig('lineEnding', 'n');
initConfig('maxClipCopyEntries', 7);
initConfig('maxDictEntries', 7);
initConfig('minihelp', true);
initConfig('onlyreading', false);
initConfig('popupcolor', 'blue');
initConfig('popupDelay', 150);
initConfig('popupLocation', 0);
initConfig('showOnKey', '');
initConfig('textboxhl', false);
initConfig('ttsEnabled', false);
/**
* Set kanjiInfo option values
* Check each key in case there are new types of info to be added to the
* config. TODO: Consider a solution that doesn't require this loop.
*/
rcxMain.config.kanjiInfo = {};
var kanjiInfoLabelList = rcxDict.prototype.kanjiInfoLabelList;
for (i = 0; i < kanjiInfoLabelList.length; i+=2) {
var kanjiInfoKey = kanjiInfoLabelList[i];
if (cloudStorage.kanjiInfo && cloudStorage.kanjiInfo[kanjiInfoKey] !== undefined) {
rcxMain.config.kanjiInfo[kanjiInfoKey] =
cloudStorage.kanjiInfo[kanjiInfoKey];
} else if (localStorage[kanjiInfoKey]) {
rcxMain.config.kanjiInfo[kanjiInfoKey] =
normalizeStringValue(localStorage[kanjiInfoKey]);
} else {
rcxMain.config.kanjiInfo[kanjiInfoKey] = true;
}
}
}
/**
* Saves options to Google Chrome Cloud storage
* https://developer.chrome.com/storage
*/
function saveOptionsToCloudStorage() {
chrome.storage.sync.set({
// Saving General options
"disablekeys": rcxMain.config.disablekeys,
"highlight": rcxMain.config.highlight,
"kanjicomponents": rcxMain.config.kanjicomponents,
"kanjiInfo": rcxMain.config.kanjiInfo,
"maxDictEntries": rcxMain.config.maxDictEntries,
"minihelp": rcxMain.config.minihelp,
"onlyreading": rcxMain.config.onlyreading,
"popupcolor": rcxMain.config.popupcolor,
"popupDelay": rcxMain.config.popupDelay,
"popupLocation": rcxMain.config.popupLocation,
"showOnKey": rcxMain.config.showOnKey,
"textboxhl": rcxMain.config.textboxhl,
"ttsEnabled": rcxMain.config.ttsEnabled,
// Saving Copy to Clipboard settings
"copySeparator": rcxMain.config.copySeparator,
"lineEnding": rcxMain.config.lineEnding,
"maxClipCopyEntries": rcxMain.config.maxClipCopyEntries
});
}
/**
* For a given string representation `value` of a boolean, integer or string,
* returns the same value coerced to the proper type.
*
* @param {string} value
* @returns {boolean|number|string}
*/
function normalizeStringValue(value) {
var maybeNumber = parseInt(value, 10);
if (!Number.isNaN(maybeNumber)) return maybeNumber;
if (value === 'true' || value === 'false') {
return value === 'true' ? true : false;
}
return value;
}