-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
141 lines (125 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
/*
Zhongwen - A Chinese-English Popup Dictionary
Copyright (C) 2012 Christian Schiller
https://chrome.google.com/extensions/detail/kkmlkkjojmombglmlpbpapmhcaljjkde
*/
chrome.browserAction.onClicked.addListener(zhongwenMain.enableToggle);
chrome.tabs.onActiveChanged.addListener(zhongwenMain.onTabSelect);
chrome.extension.onRequest.addListener(function(request, sender, response) {
switch(request.type) {
case 'enable?':
zhongwenMain.onTabSelect(sender.tab.id);
break;
case 'search':
var e = zhongwenMain.search(request.text);
response(e);
break;
case 'cangjieSearch':
var cangjieCode = zhongwenMain.cangjieSearch(request.text);
// console.log(request.text + " -> " + cangjieCode);
response(cangjieCode);
break;
case 'open':
var tabID = zhongwenMain.tabIDs[request.tabType];
if (tabID) {
chrome.tabs.get(tabID, function(tab) {
if (tab && (tab.url.substr(-13) == 'wordlist.html')) {
chrome.tabs.reload(tabID);
chrome.tabs.update(tabID, {
active: true
});
} else {
chrome.tabs.create({
url: request.url
}, function(tab) {
zhongwenMain.tabIDs[request.tabType] = tab.id;
if (request.tabType == 'wordlist') {
// make sure the table is sized correctly
chrome.tabs.reload(tab.id);
}
});
}
});
} else {
chrome.tabs.create({
url: request.url
}, function(tab) {
zhongwenMain.tabIDs[request.tabType] = tab.id;
if (request.tabType == 'wordlist') {
// make sure the table is sized correctly
chrome.tabs.reload(tab.id);
}
});
}
break;
case 'copy':
var txt = document.createElement('textarea');
txt.style.position = "absolute";
txt.style.left = "-100%";
txt.value = request.data;
document.body.appendChild(txt);
txt.select();
document.execCommand('copy');
document.body.removeChild(txt);
break;
case 'add':
var json = localStorage['wordlist'];
var wordlist;
if (json) {
wordlist = JSON.parse(json);
} else {
wordlist = []
}
for (var i in request.entries) {
var entry = {}
entry.simplified = request.entries[i].simplified;
entry.traditional = request.entries[i].traditional;
entry.pinyin = request.entries[i].pinyin;
entry.definition = request.entries[i].definition;
wordlist.push(entry);
}
localStorage['wordlist'] = JSON.stringify(wordlist);
var tabID = zhongwenMain.tabIDs['wordlist'];
if (tabID) {
chrome.tabs.get(tabID, function(tab) {
if (tab) {
chrome.tabs.reload(tabID);
}
});
}
break;
case 'iframe':
chrome.tabs.executeScript(sender.tab.id, {
file: 'content.js',
allFrames: true
})
break;
default:
// ignore
}
});
function initStorage(key, defaultValue) {
var currentValue = localStorage[key];
if (!currentValue) {
localStorage[key] = defaultValue;
}
}
initStorage("popupcolor", "yellow");
initStorage("tonecolors", "yes");
initStorage("fontSize", "small");
initStorage("skritterTLD", "com");
initStorage("zhuyin", "no");
initStorage("cangjie", "en");
zhongwenMain.config = {};
zhongwenMain.config.css = localStorage["popupcolor"];
zhongwenMain.config.tonecolors = localStorage["tonecolors"];
zhongwenMain.config.fontSize = localStorage["fontSize"];
zhongwenMain.config.skritterTLD = localStorage.skritterTLD;
zhongwenMain.config.zhuyin = localStorage.zhuyin;
zhongwenMain.config.cangjie = localStorage.cangjie;
if (localStorage['enabled'] == 1) {
zhongwenMain.loadDictionary();
zhongwenMain.enabled = 1;
} else {
zhongwenMain.enabled = 0;
}