forked from locomojis/Chromoji
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemoji.js
160 lines (140 loc) · 4.9 KB
/
emoji.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
(function emojiInsertion(window, undefined) {
'use strict';
var items = charDictionary.items, allChars = fillChars(items),
regexp = new RegExp(allChars.join('|')), nodes;
function walkTheDOM(node, func) {
if (func(node)) {
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
}
if (typeof MutationObserver !== 'function') window.MutationObserver = window.WebKitMutationObserver;
jQuery.fn.just_text = function just_text() {
return $(this).children().end().text();
};
function filter_nodes(nodes, regexp) {
// var nonEditable = nodes.querySelectorAll('[contenteditable!="true"]').querySelectorAll('[contenteditable!="plaintext-only"]'),
// txt = function txt(index) {
// };
return $(nodes).find('[contenteditable!="true"][contenteditable!="plaintext-only"]').addBack().filter(function txt(index) {
var result = false, html;
if ($(this).just_text().search(regexp) !== -1) {
html = $(this).html();
if (html) {
index = html.indexOf('document.write');
result = (index === -1);
} else result = true;
}
return result;
});
}
function on_mutation(mutations) {
var l = mutations.length, i, mutation, added, al, target;
for (i = 0; i < l; i++) {
mutation = mutations[i];
added = mutation.addedNodes;
al = added.length;
target = mutation.target;
if (al > 0) run(filter_nodes(added, regexp));
}
}
function run(nodes) {
nodes.forEach(function runnode(element, index, array) {
var node = element, html, replacement;
if (!$(node).html()) node = $(node).parent();
if ($(node).html()) {
html = $(node).html();
replacement = html.replace(regexp, function replacer(c) {
return '<span class="emojifont" id="emoji-font">' + c + '</span>';
});
$(node).html(replacement);
}
});
}
function isPrivate(ch) {
var code;
if (ch.length === 1) code = ch.charCodeAt(0);
else code = (ch.charCodeAt(0) - 0xD800) * 0x400 + ch.charCodeAt(1) - 0xDC00 + 0x10000;
return code >= 0xE000 && code <= 0xF8FF || code >= 0xF0000 && code <= 0xFFFFD ||
code >= 0x100000 && code <= 0x10FFFD;
}
function fillChars(items) {
var charArr = [];
items.forEach(function charArray(element, index, array) {
var chars = element.chars;
chars.forEach(function ch(element, index, array) {
charArr.push(element);
});
});
chrome.extension.sendMessage({setting: 'hidePrivate'}, function filterHidden(response) {
if (response.result) charArr = charArr.filter(function isNotPrivate(ch) {return !isPrivate(ch);});
});
return charArr;
}
function isInput(el) {
return (el.nodeName.toLowerCase() === 'input' && el.type === 'text') ||
(el.nodeName.toLowerCase() === 'textarea') || el.isContentEditable;
}
function isEdit(el) {
var n = el.nodeName.toLowerCase();
return ((n === 'input' && el.type === 'text') || (n === 'textarea') ||
el.isContentEditable);
}
function fontExtend(el) {
var font = window.getComputedStyle(el)['font-family'] || 'monospace';
el.dataset.emoji_font = true;
el.style.removeProperty('font-family');
el.style.cssText += ['; font-family: ', font,
', "Segoe UI Emoji", "Segoe UI Symbol", Symbola, EmojiSymbols !important;'].join('');
}
function fontExtendEdit(e) {
var el = e.target;
if (isEdit(el) && !el.dataset.emoji_font) fontExtend(el);
}
document.addEventListener('focus', fontExtendEdit, true);
function fontExtendLoad(el) {
var n = el.nodeName.toLowerCase();
if (n !== 'script' && n !== 'stylesheet' && n !== 'link' && !isEdit(el) &&
!el.dataset.emoji_font) fontExtend(el);
}
function fontExtender() {
walkTheDOM(document.body, fontExtendLoad);
}
if (isReady()) fontExtender();
else document.addEventListener('DOMContentLoaded', fontExtender, false);
function start_observer() {
var target = document.body, observer = new MutationObserver(on_mutation),
config = {childList: true, characterData: true, subtree: true};
observer.observe(target, config);
}
function init() {
return; //no init for now
nodes = filter_nodes($('body'), regexp);
run(nodes);
start_observer();
}
function isReady() {
return /complete|loaded|interactive/.test(document.readyState);
}
chrome.extension.sendMessage({setting: 'blacklist'}, function checkBlacklist(response) {
var blacklist = response.result;
if (!blacklist) {
if (isReady()) return init();
return document.addEventListener('DOMContentLoaded', init, false);
}
blacklist = blacklist.split(',');
var blacklisted = false;
blacklist.forEach(function blist(element, index, array) {
var bdomArr = element.split('.'), bl = bdomArr.length, domArr = document.domain.split('.');
if (bl <= domArr.length) for (var i = bl; i--;) if (bdomArr.pop() !== domArr.pop()) break;
blacklisted = blacklisted || !bdomArr.length;
});
if (!blacklisted) {
if (isReady()) return init();
return document.addEventListener('DOMContentLoaded', init, false);
}
});
}(this));