-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
55 lines (48 loc) · 1.92 KB
/
content.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
function mouseDownHandler(event) {
if (!window.confirm("Do you really want to sign out?")) {
event.preventDefault();
event.stopPropagation();
return;
} else {
location.replace("/logout");
}
}
function onLoad(event) {
if (!signoutEl) {
signoutEl = document.querySelector('[href="/logout"]');
addClickListener(signoutEl, event.type);
}
}
document.addEventListener("DOMContentLoaded", onLoad);
document.addEventListener("load", onLoad);
// Function to add the event listener to the sign out element
function addClickListener(signoutEl, eventType) {
if (signoutEl) {
signoutEl.addEventListener('mousedown', mouseDownHandler, true);
console.info("Sign out click event listener added successfully.");
} else {
console.warn('Could not find the "Sign out" button element on', eventType);
}
}
// Create a MutationObserver to watch for creation of the sign out element
const observer = new MutationObserver((mutations) => {
for (let mutation of mutations) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
for (let node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE && node.hasAttribute('href') && node.getAttribute('href') === '/logout') {
addClickListener(node, "mutation observe match");
observer.disconnect(); // Stop observing once the element is found
return;
}
}
}
}
});
// Start observing the document for changes
observer.observe(document.body, { childList: true, subtree: true });
// Try to add the event listener immediately in case the element is already in the DOM
var signoutEl = document.querySelector('[href="/logout"]');
if (signoutEl) {
addClickListener(signoutEl, "script run");
observer.disconnect(); // Stop observing if the element is already found
}