-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMentionsModule.js
More file actions
92 lines (80 loc) · 3.49 KB
/
MentionsModule.js
File metadata and controls
92 lines (80 loc) · 3.49 KB
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
window.initMentionsModule = function() {
'use strict';
const injectStyles = () => {
if (document.getElementById('itd-link-styles')) return;
const s = document.createElement('style');
s.id = 'itd-link-styles';
s.innerHTML = `
.itd-clickable {
color: #0095ff !important;
text-decoration: none !important;
font-weight: 500 !important;
cursor: pointer !important;
transition: opacity 0.1s ease;
}
.itd-mention:hover { opacity: 0.8 !important; }
.itd-url:hover {
text-decoration: underline !important;
opacity: 0.8 !important;
}
`;
document.head.appendChild(s);
};
const smartReplace = (node) => {
const mentionRegex = /(?<![/\w])@([\w\d_]+)/;
const urlRegex = /((?:https?:\/\/|www\.)[^\s/$.?#].[^\s]*|(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+(?:com|ru|net|org|io|gov|edu|it|me|info|biz|site)(?:\/[^\s]*)?)/;
const combinedRegex = new RegExp(`${mentionRegex.source}|${urlRegex.source}`, 'g');
const text = node.nodeValue;
if (!combinedRegex.test(text)) return;
const fragment = document.createDocumentFragment();
let lastIndex = 0;
let match;
combinedRegex.lastIndex = 0;
while ((match = combinedRegex.exec(text)) !== null) {
fragment.appendChild(document.createTextNode(text.substring(lastIndex, match.index)));
const matchedText = match[0];
const isMention = matchedText.startsWith('@');
const link = document.createElement('a');
if (isMention) {
const username = match[1];
link.href = `/${username}`;
link.className = 'itd-clickable itd-mention';
link.textContent = matchedText;
link.onclick = (e) => {
e.preventDefault();
window.location.href = link.href;
};
} else {
let url = matchedText;
const fullUrl = /^(https?:\/\/|\/\/)/i.test(url) ? url : `https://${url}`;
link.href = fullUrl;
link.className = 'itd-clickable itd-url';
link.textContent = url;
link.target = '_blank';
link.rel = 'noopener noreferrer';
}
fragment.appendChild(link);
lastIndex = combinedRegex.lastIndex;
}
fragment.appendChild(document.createTextNode(text.substring(lastIndex)));
node.parentNode.replaceChild(fragment, node);
};
const processElement = (el) => {
if (el.hasAttribute('data-itd-parsed')) return;
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);
let node;
const nodesToProcess = [];
while (node = walker.nextNode()) nodesToProcess.push(node);
nodesToProcess.forEach(smartReplace);
el.setAttribute('data-itd-parsed', 'true');
};
const startObserver = () => {
const observer = new MutationObserver(() => {
const selectors = '.post-content, .profile-bio__text, .user-bio, .item-text, .bio-text';
document.querySelectorAll(selectors).forEach(processElement);
});
observer.observe(document.body, { childList: true, subtree: true });
};
injectStyles();
startObserver();
};