Youtube Logout Confirm

為 Youtube 登出鈕增加確認對話框,避免誤點導致帳號全部登出

目前为 2024-11-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Logout Confirm
  3. // @namespace http://tampermonkey.net/
  4. // @author Microdust
  5. // @version 1.0
  6. // @description 為 Youtube 登出鈕增加確認對話框,避免誤點導致帳號全部登出
  7. // @include *://*.youtube.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const MSG = (() => {
  16. const userLanguage = navigator.language || navigator.userLanguage;
  17. switch (userLanguage) {
  18. case 'zh-TW':
  19. return '確定要登出嗎?';
  20. case 'zh-HK':
  21. return '確定要登出嗎?';
  22. case 'zh-CN':
  23. return '确定要登出吗?';
  24. default:
  25. return 'Are you sure you want to log out?';
  26. }
  27. })();
  28.  
  29. function confirmLogout(element) {
  30. const parent = element.closest('a');
  31. if (parent && !parent.dataset.processed) {
  32. parent.dataset.processed = 'true';
  33. const clonedLink = parent.cloneNode(false);
  34. clonedLink.removeAttribute('href');
  35. clonedLink.appendChild(parent.children[0]);
  36. clonedLink.addEventListener('click', (event) => {
  37. event.preventDefault();
  38. if (confirm(MSG)) {
  39. parent.click();
  40. }
  41. });
  42.  
  43. parent.parentElement.insertBefore(clonedLink, parent);
  44. parent.style.display = 'none';
  45. }
  46. }
  47.  
  48. const observer = new MutationObserver((mutationsList) => {
  49. for (const mutation of mutationsList) {
  50. if (mutation.type === 'childList') {
  51. const logoutElements = Array.from(document.querySelectorAll('[id=endpoint]')).filter(el => el.href.includes('logout') && !el.closest('a').dataset.processed);
  52. if (logoutElements.length) {
  53. confirmLogout(logoutElements[0]);
  54. }
  55. }
  56. }
  57. });
  58.  
  59. observer.observe(document.body, { childList: true, subtree: true });
  60. })();