-
Notifications
You must be signed in to change notification settings - Fork 0
/
email2inter.js
114 lines (100 loc) · 3.69 KB
/
email2inter.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
// Improved email regex
const emailRegex = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b/g;
// Set to keep track of processed nodes
const processedNodes = new WeakSet();
// Function to create Intercom search URL
function createIntercomSearchUrl(email) {
return new Promise((resolve) => {
try {
chrome.storage.local.get(['intercomAppCode'], function(result) {
if (chrome.runtime.lastError) {
console.error('Chrome runtime error:', chrome.runtime.lastError);
resolve(null);
return;
}
if (result.intercomAppCode) {
const appCode = result.intercomAppCode;
const encodedEmail = encodeURIComponent(email);
resolve(`https://app.intercom.io/a/inbox/${appCode}/inbox/search?query=${encodedEmail}`);
} else {
console.error('Intercom App Code not set. Please set it in the extension popup.');
resolve(null);
}
});
} catch (error) {
console.error('Error accessing chrome API:', error);
resolve(null);
}
});
}
// Function to make emails clickable
function makeEmailsClickable() {
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
const nodesToReplace = [];
while (walker.nextNode()) {
const node = walker.currentNode;
if (node.nodeType === Node.TEXT_NODE && !processedNodes.has(node) && node.textContent.match(emailRegex)) {
nodesToReplace.push(node);
}
}
for (const node of nodesToReplace) {
if (!node.parentNode) continue;
const fragment = document.createDocumentFragment();
let lastIndex = 0;
const matches = node.textContent.matchAll(emailRegex);
for (const match of matches) {
fragment.appendChild(document.createTextNode(node.textContent.slice(lastIndex, match.index)));
const link = document.createElement('a');
link.href = '#';
link.textContent = match[0];
link.style.cssText = 'color: blue; text-decoration: underline; cursor: pointer;';
link.title = "Click to search in Intercom";
link.addEventListener('click', (e) => handleEmailClick(e, match[0]));
fragment.appendChild(link);
lastIndex = match.index + match[0].length;
}
fragment.appendChild(document.createTextNode(node.textContent.slice(lastIndex)));
if (node.parentNode) {
node.parentNode.replaceChild(fragment, node);
processedNodes.add(fragment);
}
}
}
// Function to handle email click
function handleEmailClick(e, email) {
e.preventDefault();
e.stopPropagation(); // Prevent event bubbling
createIntercomSearchUrl(email).then(url => {
if (url) {
chrome.runtime.sendMessage({action: "openIntercomTab", url: url});
} else {
chrome.runtime.sendMessage({action: "openPopup"});
}
}).catch(err => {
console.error('Error:', err);
});
}
// Debounce function
function debounce(func, wait) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
// Debounced version of makeEmailsClickable
const debouncedMakeEmailsClickable = debounce(makeEmailsClickable, 300);
// Initialize extension after window load and a delay
window.addEventListener('load', function() {
setTimeout(() => {
console.log('Initializing Email to Intercom Linker');
makeEmailsClickable();
// Set up MutationObserver
const observer = new MutationObserver(mutations => {
if (mutations.some(mutation => mutation.type === 'childList' && mutation.addedNodes.length > 0)) {
debouncedMakeEmailsClickable();
}
});
observer.observe(document.body, { childList: true, subtree: true });
}, 2000); // 2 second delay after window load
});