-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdictwindow.coffee
210 lines (182 loc) · 7.72 KB
/
dictwindow.coffee
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
define ["jquery",
"utils",
"background/setting",
"background/dict.js",
"background/storage"], ($, utils, setting, dict, storage)->
console.log "[dictwindow] init"
defaultWindowUrl = chrome.extension.getURL('template/apidict.html')
updateWindowDfd = null
injectContentDfd = null
dictInitedDfd = null
windowType = 'popup'
dictWindowManager =
w: null
tid: null
url: null
word: null
open: ()->
dfd = $.Deferred()
# bugfix: dont know how why, windowWidth and windowHeight are saved as number, need integer here.
width = parseInt(setting.getValue('windowWidth'))
height = parseInt(setting.getValue('windowHeight'))
left = Math.round((screen.width / 2) - (width / 2))
top = Math.round((screen.height / 2) - (height / 2))
url = 'http://blog.riverrun.xyz/fairydict.html'
if !@w
@beforeUpdateUrl()
chrome.windows.create({
url: defaultWindowUrl,
# url: 'http://cn.bing.com/dict/search?q=elephant',
type: windowType,
width: width,
height: height,
left: left,
top: top
}, (win)=>
@w = win
@tid = @w.tabs[0].id
@url = defaultWindowUrl
dfd.resolve()
)
else
chrome.windows.update(dictWindowManager.w.id, {
focused: true
})
dfd.resolve()
return dfd
sendMessage: (msg)->
chrome.tabs.sendMessage(@tid, msg) if @tid
lookup: (text)->
dictName = setting.getValue('dictionary')
@open().done ()=>
if text
@sendMessage({type: 'querying', text})
@queryDict(text, dictName)
queryDict: (text, dictName, inHistory)->
@word = text
if not inHistory
@sendMessage({type: 'history', history: storage.history})
console.log "[dictwindow] query #{@word} from #{dictName}"
dict.query(text, dictName).then @sendQueryResult.bind(this)
saveWindowSize: ()->
chrome.windows.get @w.id, null, (w)=>
if w?.width and w?.height
if Math.abs(w.width-setting.getValue('windowWidth')) > 10 or Math.abs(w.height-setting.getValue('windowHeight')) > 10
console.log "[dictwindow] update window size: #{w.width} * #{w.height}"
setting.setValue 'windowWidth', w.width
setting.setValue 'windowHeight', w.height
sendQueryResult: (result)->
@saveWindowSize()
item = storage.isInHistory(@word)
if result
udfd = @updateUrl(result.windowUrl or defaultWindowUrl)
$.when(updateWindowDfd, dictInitedDfd, udfd).then ()=>
console.log "[dictwindow] send query result"
@sendMessage({
type: 'queryResult',
result: result,
text: @word,
inHistory: item?,
rating: item?[@word]})
# if @word is a long sentence, don't keep in history
if not item and @word.split(/\s+/).length <= 5
storage.addHistory(@word)
injectResources: ()->
styles = [
"css/bootstrap.css",
"bower_components/angular-ui/build/angular-ui.css",
"bower_components/angular-bootstrap/ui-bootstrap-csp.css",
"css/font-awesome.css",
"css/dictheader.css"
]
scripts = [
'bower_components/jquery/dist/jquery.js',
'bower_components/underscore/underscore.js',
"bower_components/angular/angular.js",
"bower_components/angular-ui/build/angular-ui.js",
"bower_components/angular-sanitize/angular-sanitize.js",
"bower_components/angular-bootstrap/ui-bootstrap.js",
"utils.js",
"js/starrr.js",
"loader.js",
"dict.js"
]
inject = (t, files, index)=>
files ?= []
index ?= 0
dfd = $.Deferred()
file = files[index]
if file and t and @tid
console.log "[dictwindow] inject #{file}"
chrome.tabs[t] @tid, {file: file}, ()=>
inject(t, files, index+1).then ()=>
dfd.resolve()
else
dfd.resolve()
return dfd
res = dict.getDictResources(setting.getValue('dictionary'))
# chrome-extension:// can't inject files, permission denied.
if @url.indexOf('chrome-extension://') == 0
return inject(null)
return inject('insertCSS', res?.styles).then ()=>
return inject('insertCSS', styles).then ()=>
return inject('executeScript', scripts).then ()=>
return inject('executeScript', res?.scripts)
updateUrl: (url)->
outDfd = $.Deferred()
if url and @url != url
console.log "[dictwindow] update url: #{url}"
@url = url
@beforeUpdateUrl().then ()=>
console.log "[dictwindow] updated url: #{url}"
outDfd.resolve(true)
chrome.tabs.update @tid, {url, active: true}
return outDfd
else
return outDfd.resolve(false)
beforeUpdateUrl: ()->
injectContentDfd = $.Deferred((dfd)=>
dfd.then ()=>
return @injectResources().then ()=>
updateWindowDfd?.resolve()
)
dictInitedDfd = $.Deferred()
updateWindowDfd = $.Deferred()
return updateWindowDfd
onContentInjected: (url, tabId)->
console.log "[dictwindow] manifest's content scripts injected from url: #{url}"
if injectContentDfd?.state() == 'pending'
injectContentDfd.resolve()
# page was reloaded.
else if url and tabId == @tid
d = setting.getValue('dictionary')
w = dict.getWordFromUrl(url, d)
@url = url
if w
@word = w
else
w = @word
dictInitedDfd = $.Deferred()
updateWindowDfd = $.Deferred()
console.log "[dictwindow] reload #{w} url #{url}"
@injectResources().then ()=>
if @url == defaultWindowUrl
dict.query(@word, d).then @sendQueryResult.bind(@)
else
@sendQueryResult()
updateWindowDfd.resolve()
onDictInited: ()->
console.log "[dictwindow] dict inited"
dictInitedDfd?.resolve()
@sendMessage({type: 'history', history: storage.history})
chrome.windows.onRemoved.addListener (wid)->
if dictWindowManager.w?.id == wid
dictWindowManager.w = null
dictWindowManager.tid = null
updateWindowDfd = null
injectContentDfd = null
dictInitedDfd = null
# chrome.tabs.onUpdated.addListener (tabId, info)->
# if dictWindowManager.tid == tabId
# dictWindowManager.onReload(info.url)
return dictWindowManager