-
Notifications
You must be signed in to change notification settings - Fork 0
/
emojifier.js
205 lines (181 loc) · 6.37 KB
/
emojifier.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
let apiEmojiKey;
let apiTextKey;
chrome.storage.local.get('apiEmojiKey', (data) => {
apiEmojiKey = data.apiEmojiKey;
console.log('Emoji API key: ', apiEmojiKey);
});
chrome.storage.local.get('apiTextKey', (data) => {
apiTextKey = data.apiTextKey;
console.log('Text API key: ', apiTextKey);
});
let selectedText;
const popupNode = document.createElement('a');
popupNode.textContent = 'Emojify';
popupNode.style.position = 'absolute';
popupNode.style.background = '#d1d800';
popupNode.style.padding = '4px 6px';
popupNode.style.cursor = 'pointer';
popupNode.style.textDecoration = 'underline';
popupNode.style.zIndex = 999999;
popupNode.hidden = true;
document.body.appendChild(popupNode);
const popupTextNode = document.createElement('a');
popupTextNode.textContent = 'Translate';
popupTextNode.style.position = 'absolute';
popupTextNode.style.background = '#58d68d ';
popupTextNode.style.padding = '2px 3px';
popupTextNode.style.cursor = 'pointer';
popupTextNode.style.textDecoration = 'underline';
popupTextNode.hidden = true;
document.body.appendChild(popupTextNode);
const emojiParent = document.createElement('div');
document.body.appendChild(emojiParent);
function findAndAddEmojis(imgNode) {
popupNode.hidden = true;
imgNode.classList.add('emojifierConverted');
fetch('https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize', {
method: 'POST',
body: JSON.stringify({url: imgNode.src}),
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiEmojiKey
}
}).then((resp) => {
if (resp.ok) {
return resp.json();
} else {
console.error('Response error', resp);
}
})
.then((resp) => {
addEmojis(imgNode, resp);
})
.catch((error) => {
console.log(error);
});
}
function addEmojis(imgNode, faces) {
for (const {faceRectangle, scores} of faces) {
const image = new Image();
image.src = chrome.extension.getURL(`emojis/${getClosestEmoji(scores)}`);
const rect = imgNode.getBoundingClientRect();
const ratioX = imgNode.scrollWidth / imgNode.dataset.naturalWidth;
const ratioY = imgNode.scrollHeight / imgNode.dataset.naturalHeight;
const left = rect.left + window.scrollX + faceRectangle.left * ratioX;
const top = rect.top + window.scrollY + faceRectangle.top * ratioY;
const width = faceRectangle.width * ratioX;
const height = faceRectangle.height * ratioY;
image.style.position = 'absolute';
image.style.top = `${top}px`;
image.style.left = `${left}px`;
image.style.zIndex = 999999;
image.style.width = `${width}px`;
image.style.height = `${height}px`;
image.classList.add('emojifierEmoji');
emojiParent.appendChild(image);
}
}
function showPopup(imgNode) {
popupNode.hidden = false;
const rect = imgNode.getBoundingClientRect();
const top = rect.top + window.scrollY + imgNode.offsetHeight - popupNode.offsetHeight;
const left = rect.left + window.scrollX + imgNode.offsetWidth - popupNode.offsetWidth;
popupNode.style.top = `${top}px`;
popupNode.style.left = `${left}px`;
}
function showTextPopup(event) {
let textArea = event.target;
console.log('TextArea: ', textArea);
let left = textArea.offsetLeft + textArea.offsetWidth - popupTextNode.offsetWidth;
let top = textArea.offsetTop + textArea.offsetHeight - popupTextNode.offsetHeight;
popupTextNode.style.left = `${left}px`;
popupTextNode.style.top = `${top}px`;
popupTextNode.hidden = false;
}
function translateText(event) {
popupTextNode.hidden = true;
fetch('https://api.microsofttranslator.com/V2/Http.svc/Translate', {method: 'POST',
body: JSON.stringify({text: selectedText, from: 'en', to: 'lt'}),
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiTextKey
}
})
.then((resp) => {
console.log(resp);
})
.catch((error) => {
console.log(error);
});
alert("Got selected text: " + selectedText);
}
popupNode.addEventListener('click', () => findAndAddEmojis(currentImage));
popupTextNode.addEventListener('click', translateText);
document.addEventListener('mouseover', ({target}) => {
if (isValidImage(target)) {
currentImage = target;
showPopup(target);
}
});
function isValidImage(image) {
if (image.tagName !== 'IMG' && !image.style.backgroundImage) {
return false;
}
if (image.classList.contains('emojifierConverted') ||
image.classList.contains('emojifierEmoji')) {
return false;
}
let url;
if (image.tagName === 'IMG') {
url = image.src;
} else {
url = image.style.backgroundImage;
}
if (url.startsWith('data:')) {
return false;
}
let naturalWidth, naturalHeight;
if (image.tagName === 'IMG') {
image.dataset.naturalWidth = image.naturalWidth;
image.dataset.naturalHeight = image.naturalHeight;
} else {
const testImage = new Image();
testImage.src = image.style.backgroundImage;
image.dataset.naturalWidth = testImage.width;
image.dataset.naturalHeight = testImage.height;
}
return image.dataset.naturalWidth >= 36 && image.dataset.naturalHeight >= 36;
}
chrome.runtime.onMessage.addListener((message) => {
if (message.command === "emojify") {
let i = 0;
for (const image of document.images) {
if (isValidImage(image)) {
setTimeout(() => findAndAddEmojis(image), 100 * i);
++i;
}
}
} else if (message.command === "reset") {
emojiParent.innerHTML = '';
for (const image of document.getElementsByClassName('emojifierConverted')) {
image.classList.remove('emojifierConverted');
}
}
});
document.onmouseup = checkSelectedText;
function checkSelectedText() {
let text = getSelectedText();
if (text) {
selectedText = text;
showTextPopup(event);
}
}
function getSelectedText() {
let text = "";
if (typeof window.getSelection != "undefined") {
text = window.getSelection().toString();
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}