-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript.js
More file actions
69 lines (61 loc) · 2.57 KB
/
userscript.js
File metadata and controls
69 lines (61 loc) · 2.57 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
// ==UserScript==
// @name YouTube Sign Out Confirm
// @namespace http://tampermonkey.net/
// @version 2024-05-29
// @description Asks for confirmation before signing out of YouTube (when you click the "Sign out" button)
// @author Wis (wis.am)
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function () {
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
}
})();