forked from gkovacs/foreign-manga-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb.coffee~
308 lines (286 loc) · 11.2 KB
/
nb.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
root = exports ? this
root.currentText = ''
positionPopup = root.positionPopup = () ->
selectedBubble = $('.selection.selected')
if not selectedBubble? or selectedBubble.length == 0
return
popupDialog = $('.ui-dialog')
if not popupDialog? or popupDialog.length == 0
return
console.log 'setting offset!'
popupDialog.offset({'left': selectedBubble.offset().left, 'top': selectedBubble.offset().top - Math.max(popupDialog.height()+10, 110)})
#popupDialog.css('left', selectedBubble.offset().left).css('bottom', $(window).height() - selectedBubble.offset().top).css('height', '100%')
getLineNumFromText = (selectedText) ->
parenIndexes = (selectedText.indexOf(x) for x in '))' when selectedText.indexOf(x) != -1)
if parenIndexes.length > 0
parenIndex = Math.min.apply(Math, parenIndexes)
lineNum = selectedText[...parenIndex].trim()
if not isNaN(lineNum)
return parseInt(lineNum)
return -1
getCurrentDialog = () ->
for x,i in $('.location-lens')
if $(x).hasClass('selected')
return i
return -1
goToDialog = (idx) ->
$($('.location-lens')[idx]).click()
goToNextDialog = () ->
currentDialog = getCurrentDialog()
if currentDialog == -1
goToDialog(0)
else
goToDialog(currentDialog + 1)
goToPreviousDialog = () ->
currentDialog = getCurrentDialog()
if currentDialog <= 0
goToDialog(0)
else
goToDialog(currentDialog - 1)
trimSelectedText = (selectedText) ->
if selectedText.indexOf('http://geza') != -1
selectedText = selectedText[...selectedText.indexOf('http://geza')]
parenIndexes = (selectedText.indexOf(x) for x in '))' when selectedText.indexOf(x) != -1)
if parenIndexes.length > 0
parenIndex = Math.min.apply(Math, parenIndexes)
lineNum = selectedText[...parenIndex].trim()
if not isNaN(lineNum)
selectedText = selectedText[parenIndex+1..]
return selectedText.trim()
getRawTextForBubble = (bubble_id) ->
if isNaN(parseInt(bubble_id))
bubble_id = bubble_id.attr('id_item')
return $('.location-lens[id_item=' + bubble_id + ']').find('.location-shortbody').text()
getTextForBubble = (bubble_id) ->
rawtext = getRawTextForBubble(bubble_id)
rawtext = (line for line in rawtext.split('\n') when line.indexOf('lang=') != 0 and line.indexOf('showenglish') != 0).join(' ')
return trimSelectedText(rawtext)
$(document).ready(
assignVariable('$', 'jQuery')
assignVariable('callOnceObjectAvailable', callOnceObjectAvailable)
assignVariable('getLineNumFromText', getLineNumFromText)
assignVariable('positionPopup', positionPopup)
assignVariable('getTextForBubble', getTextForBubble)
assignVariable('getRawTextForBubble', getRawTextForBubble)
assignVariable('trimSelectedText', trimSelectedText)
#assignVariable('synthesizeSpeech', synthesizeSpeech)
#assignVariable('getTextForBubble', exposeFunction('getTextForBubble', (textOutput) -> console.log(textOutput)))
executeInPage(() ->
console.log('executing in page!')
console.log(window)
console.log(window['NB$'])
window.foo = () -> console.log(35)
window.callOnceObjectAvailable('NB$', () ->
console.log window.NB$.ui.notepaneView.prototype.options.loc_sort_fct
window.NB$.ui.notepaneView.prototype.options.loc_sort_fct = (o1,o2) ->
o1LineNum = getLineNumFromText(o1.body)
o2LineNum = getLineNumFromText(o2.body)
if o1LineNum == o2LineNum
return o1.right - o2.right
else
if o1LineNum == -1 or o2LineNum == -1
return o1.right - o2.right
else
return o1LineNum - o2LineNum
console.log window.NB$.ui.notepaneView.prototype.options.loc_sort_fct
)
console.log('done executing in page')
)
$(document).keyup((e) ->
console.log e
focused = $(':focus')
if focused.length > 0 and focused[0].type in ['textarea', 'input', 'text']
return false
if e.keyCode == 40 #or e.keyCode == 39
goToNextDialog()
return false
else if e.keyCode == 38 #or e.keyCode == 37
goToPreviousDialog()
return false
)
callOnceElementAvailable('.active-view', () ->
$('.active-view').scroll(() ->
#console.log 'scrolling!'
positionPopup()
)
)
#callOnceElementAvailable('.perspective', () ->
# $('.perspective').css('height', parseInt($('.perspective').css('height').split('px').join('')) - 150)
# $('.perspective').css('top', parseInt($('.perspective').css('top').split('px').join('')) + 150)
#)
#callOnceElementAvailable('.nb-viewport', () ->
# $('.nb-viewport').css('height', parseInt($('.nb-viewport').css('height').split('px').join('')) - 150)
# $('.nb-viewport').css('top', parseInt($('.nb-viewport').css('top').split('px').join('')) + 150)
#)
root.serverLocation = 'http://geza.csail.mit.edu:1357'
popupSentenceDisplay = $('''<div id="popupSentenceDisplay">dialog content is here</div>''')
popupSentenceDisplay.dialog({
'autoOpen': false,
'modal': false,
'title': '',
#'show': 'clip',
#'hide': 'clip',
'position': ['left', 'top'],
'zIndex': 99,
'width': 'auto',
'height': 'auto',
#'float': 'left',
#'width': '100%',
'maxHeight': '100px',
'max-height': '100px',
'create': () ->
$(this).css("maxHeight", '100px').css('max-height', '100px')
'close': () ->
audioTag = $('audio')[0]
if audioTag?
audioTag.pause()
}).css('max-height', '100px').css('maxHeight', '100px')
callOnceElementAvailable('.location-shortbody-text', () ->
for lang in ['zh', 'ja', 'fr', 'de']
console.log lang
console.log $('.location-shortbody-text').text()
if $('.location-shortbody-text').text().indexOf('lang=' + lang) != -1
root.selectedLanguage = lang
break
)
)
root.selectedLanguage = 'zh'
chrome.extension.onMessage.addListener((request, sender, sendResponse) ->
if request['selectedLanguage']?
root.selectedLanguage = request['selectedLanguage']
if request['screenshotResult']?
console.log 'screenshot result'
console.log request['screenshotResult']
if request['executeHere']? and request['executeHere'].function? and request['executeHere'].arglist?
root[request['executeHere'].function].apply(request['executeHere'].arglist)
if request['log']?
console.log request['log']
console.log request
)
synthesizeSpeech = root.synthesizeSpeech = (sentence, lang, isloop) ->
audioTag = $('audio')[0]
if not audioTag
$('body').append($('<audio>').attr('autoplay', true).attr('loop', isloop))
audioTag = $('audio')[0]
audioTag.src = 'http://geza.csail.mit.edu:1357/synthesize?sentence=' + sentence + '&lang=' + lang
if isloop or not isloop?
$('audio').attr('loop', false)
else
$('audio').attr('loop', true)
audioTag.play()
haveNewText = () ->
console.log root.currentText
$('#popupSentenceDisplay').dialog('open')
$('#popupSentenceDisplay').text('')
$('#popupSentenceDisplay').css('width', 'auto')
$('#popupSentenceDisplay').css('height', 'auto')
$('.selection.selected').unbind('click', haveNewText)
$('.selection.selected').bind('click', haveNewText)
$('.location-lens.selected').unbind('click', haveNewText)
$('.location-lens.selected').bind('click', haveNewText)
$('.ui-dialog').css('z-index', 99)
$('.ui-dialog').css('width', 'auto')
$('.ui-dialog').css('height', 'auto')
#selectedOffset = $('.selection.selected').offset()
#if selectedOffset?
# $('.ui-dialog').offset({'left': selectedOffset.left, 'top': selectedOffset.top})
$('#popupSentenceDisplay').css('max-height', '500px')
root.addSentence(root.currentText, root.selectedLanguage, $('#popupSentenceDisplay'), true, () ->
positionPopup()
)
synthesizeSpeech(root.currentText, root.selectedLanguage, false)
positionPopup()
getOCR = (imagedata, callback) ->
dataPrefix = 'data:image/png;base64,'
if imagedata.indexOf(dataPrefix) == 0
imagedata = imagedata[dataPrefix.length..]
$.get(root.serverLocation + '/getOCR?data=' + imagedata, callback)
cropBase64Image = (imgData, screenshotCoordinates, callback) ->
canvas = document.createElement('canvas')
canvas.width = screenshotCoordinates.width
canvas.height = screenshotCoordinates.height
context = canvas.getContext('2d')
imageObj = new Image()
imageObj.width = screenshotCoordinates.width
imageObj.heigth = screenshotCoordinates.height
imageObj.onload = () ->
context.drawImage(
imageObj,
screenshotCoordinates.left,
screenshotCoordinates.top,
screenshotCoordinates.width,
screenshotCoordinates.height,
0,
0,
screenshotCoordinates.width,
screenshotCoordinates.height
)
callback(context.canvas.toDataURL("image/png"))
imageObj.src = imgData
prevScreenshotCoordinates = null
setInterval(() ->
if root.selectedLanguage not in ['zh', 'ja']
return
if not $('.ui-drawable-selection')? or not $('.ui-drawable-selection').offset or not $('.ui-drawable-selection').offset()?
return
#console.log 'something selected'
screenshotCoordinates = {
'left': $('.ui-drawable-selection').offset().left,
'top': $('.ui-drawable-selection').offset().top,
'width': $('.ui-drawable-selection').width(),
'height': $('.ui-drawable-selection').height(),
}
if _.isEqual(screenshotCoordinates, prevScreenshotCoordinates)
return
prevScreenshotCoordinates = screenshotCoordinates
console.log 'taking screenshot'
console.log screenshotCoordinates
origOpacity = $('.ui-drawable-selection').css('opacity')
$('.ui-drawable-selection').css('opacity', 0.0).promise().done(() ->
chrome.extension.sendMessage({'takeScreenshot': true}, (screenshotData) ->
$('.ui-drawable-selection').css('opacity', origOpacity)
console.log 'screenshot data!'
cropBase64Image(screenshotData, screenshotCoordinates, (croppedImage) ->
console.log "printing result of crop"
console.log croppedImage
getOCR(croppedImage, (ocrText) ->
console.log ocrText
noteBodyNum = 0
noteBodySelector = $('.note-body')
if noteBodySelector? and noteBodySelector.length > 0
noteBodyText = noteBodySelector.text()
noteBodyNum = getLineNumFromText(noteBodyText)
ocrPrefix = ''
if not isNaN(noteBodyNum)
ocrPrefix = (parseInt(noteBodyNum) + 1) + ') '
if $('textarea').text() == ''
$('textarea').text(ocrPrefix + ocrText)
)
)
)
)
#chrome.tabs.captureVisibleTab(null, {'format': 'png'}, (screenshotData) ->
# console.log 'screenshot captured'
# cropBase64Image(screenshotData, screenshotCoordinates, (croppedImage) ->
# console.log "printing result of crop"
# console.log croppedImage
# #sendMessage({'screenshotResult': croppedImage})
# )
#)
#chrome.extension.sendMessage({'takeScreenshot': true})
, 3000)
#setInterval(() ->
# positionPopup()
#, 300)
setInterval(() ->
selectedText = $('.note-body').html()
if not selectedText?
return
selectedText = selectedText.split('<br>')
selectedText = (line for line in selectedText when line.indexOf('lang=') != 0 and line.indexOf('showenglish') != 0).join(' ')
selectedText = $('<span>').html(selectedText).text()
selectedText = trimSelectedText(selectedText)
if selectedText != root.currentText
root.currentText = selectedText
haveNewText()
, 300)