forked from gkovacs/foreign-manga-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammarvis-ext.js
150 lines (137 loc) · 4.93 KB
/
grammarvis-ext.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
// Generated by IcedCoffeeScript 1.3.3f
(function() {
var autoResize, captureScreenshot, cropBase64Image, currentLanguage, doOncePageLoads, finishedLoading, root, sendMessage;
root = typeof exports !== "undefined" && exports !== null ? exports : this;
autoResize = root.autoResize = function(id) {
var newheight, newwidth;
if (document.getElementById) {
newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
}
document.getElementById(id).height = newheight + "px";
document.getElementById(id).width = newwidth + "px";
return $('#' + id).iframeAutoHeight();
};
finishedLoading = false;
doOncePageLoads = function() {
return console.log('loaded');
};
currentLanguage = 'zh';
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
var lineNum, parenIndex, parenIndexes, selectedHTML, selectedText, x;
if ((request.selectedText != null) && request.selectedText.length > 0) {
$('#sentenceDisplay').html('');
return addSentence(request.selectedText, currentLanguage);
} else if ((request.selectedHTML != null) && request.selectedHTML.length > 0) {
selectedHTML = request.selectedHTML;
if (selectedHTML.indexOf('<a href="http://geza') !== -1) {
selectedHTML = selectedHTML.slice(0, selectedHTML.indexOf('<a href="http://geza'));
}
selectedText = $('<span>').html(selectedHTML).text();
parenIndexes = (function() {
var _i, _len, _ref, _results;
_ref = '))';
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
if (selectedText.indexOf(x) !== -1) {
_results.push(selectedText.indexOf(x));
}
}
return _results;
})();
if (parenIndexes.length > 0) {
parenIndex = Math.min.apply(Math, parenIndexes);
lineNum = selectedText.slice(0, parenIndex).trim();
if (!isNaN(lineNum)) selectedText = selectedText.slice(parenIndex + 1);
}
$('#sentenceDisplay').html('');
return addSentence(selectedText, currentLanguage);
}
});
captureScreenshot = function(screenshotCoordinates) {
console.log("capturing screenshot");
return chrome.tabs.captureVisibleTab(null, {
'format': 'png'
}, function(screenshotData) {
console.log("cropping screenshot");
cropBase64Image(screenshotData, screenshotCoordinates, function(croppedImage) {
console.log("printing result of crop");
console.log(croppedImage);
return sendMessage({
'screenshotResult': croppedImage
});
});
return chrome.tabs.executeScript(null, {
'file': 'setNoteBodyBackToNormal.js'
});
});
};
cropBase64Image = function(imgData, screenshotCoordinates, callback) {
var canvas, context, imageObj;
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 = function() {
context.drawImage(imageObj, screenshotCoordinates.left, screenshotCoordinates.top, screenshotCoordinates.width, screenshotCoordinates.height, 0, 0, screenshotCoordinates.width, screenshotCoordinates.height);
return callback(context.canvas.toDataURL("image/png"));
};
return imageObj.src = imgData;
};
sendMessage = function(message) {
console.log('sending message:');
console.log(message);
return chrome.tabs.getSelected(null, function(tab) {
return chrome.tabs.sendMessage(tab.id, message);
});
};
document.addEventListener('DOMContentLoaded', function() {
console.log('content loaded!');
$('#lang_zh').click(function() {
return sendMessage({
'selectedLanguage': 'zh'
});
});
$('#lang_ja').click(function() {
return sendMessage({
'selectedLanguage': 'ja'
});
});
$('#lang_de').click(function() {
return sendMessage({
'selectedLanguage': 'de'
});
});
$('#lang_fr').click(function() {
return sendMessage({
'selectedLanguage': 'fr'
});
});
$('#disable_popup').click(function() {
return sendMessage({
'closePopup': 'true'
});
});
return setInterval(function() {
return sendMessage({
'log': 'are we still running?'
});
}, 1000);
});
/*
document.addEventListener('DOMContentLoaded', () ->
setTimeout(() ->
addSentence('是我们银河系的近邻。', 'zh')
, 5000)
#newIframe = $('')
#$('body').append(newIframe)
#$.get('http://geza.csail.mit.edu:1357/?lang=zh&sentence=是我们银河系的近邻。', (data) ->
# $('body').html(data)
#)
)
*/
}).call(this);