-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
83 lines (73 loc) · 2.58 KB
/
script.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
setInterval(function() {
let chat = document.getElementsByClassName('_1mf _1mj');
console.log(chat)
// make sure chat window exists
if (chat.length>0){
for(i = 0; i<chat.length; i++){
emojify(chat[i.toString()]);
}
} else {
// console.log("No chat windows open")
}
}, 1000);
function emojify(chat_box) {
let text = chat_box.textContent;
// make sure user has typed something
if(text.length > 1){
let start = text.indexOf(":")+1;
// check for starting ':'
if(start > 0) {
let end = text.indexOf(":", start);
// check for ending ':'
if(end > 0) {
let emoji_alias = text.slice(start, end);
// Make sure emoji doesnt have whitespace
if(/^\S*$/.test(emoji_alias)){
console.log("emoji requested: "+emoji_alias+"\nstarts: "+start+"\nends:"+end);
// console.log(getMeAnEmoji(emoji_alias))
let converted = text.substring(0, start-1)+getMeAnEmoji(emoji_alias)+text.substring(end+1)
// console.log("final: "+converted)
chat_box.textContent = converted;
}
}
}
} else {
// console.log("Found an empty chat window")
}
}
/** SOURCE CODE FROM: https://github.com/notwaldorf/emoji-translate **/
var allEmojis;
var SYMBOLS = '!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~';
(function() {
var request = new XMLHttpRequest();
request.open('GET', chrome.extension.getURL('/emojis.json'), true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
allEmojis = JSON.parse(request.response);
}
};
request.send();
})();
function getMeAnEmoji(keyword) {
keyword = keyword.trim().toLowerCase();
if (!keyword || keyword === '' || keyword === 'it')
return '';
// Maybe this is a plural word but the keyword is the singular?
var maybeSingular = '';
if (keyword[keyword.length - 1] == 's')
maybeSingular = keyword.slice(0, keyword.length - 1);
// Maybe this is a singular word but the keyword is the plural?
// Don't do this for single letter since that will pluralize crazy things.
var maybePlural = (keyword.length == 1) ? '' : keyword + 's';
// Go through all the things and find the first one that matches.
for (var emoji in allEmojis) {
var keywords = allEmojis[emoji].keywords;
if (emoji == keyword || emoji == maybeSingular || emoji == maybePlural ||
(keywords && keywords.indexOf(keyword) >= 0) ||
(keywords && keywords.indexOf(maybeSingular) >= 0) ||
(keywords && keywords.indexOf(maybePlural) >= 0))
return allEmojis[emoji].char;
}
return '';
};
/** END OF SOURCE CODE FROM: https://github.com/notwaldorf/emoji-translate **/