Google Logout Confirm

Prevent logout all your account accidentally by misclick at Google search page.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google Logout Confirm
// @name:zh      防止在 Google 誤觸登出
// @namespace    http://tampermonkey.net/
// @author       Microdust
// @version      1.0
// @description  Prevent logout all your account accidentally by misclick at Google search page.
// @description:zh  為 Google 登出鈕增加確認對話框,避免誤點導致帳號全部登出
// @include      *://*.google.*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const MSG = (() => {
        const userLanguage = navigator.language || navigator.userLanguage;
        switch (userLanguage) {
            case 'zh-TW':
                return '確定要登出嗎?';
            case 'zh-HK':
                return '確定要登出嗎?';
            case 'zh-CN':
                return '确定要登出吗?';
            default:
                return 'Are you sure you want to log out?';
        }
    })();

    window.addEventListener('load', function() {
        confirmLogout();
    });

    function confirmLogout() {
        const logoutElements = Array.from(document.getElementsByTagName('a')).filter(el => el.href.includes('Logout'));

        logoutElements.forEach(element => {
            let parent = element;

            while (parent && parent.tagName !== 'A') {
                parent = parent.parentElement;
            }

            if (parent && parent.tagName === 'A') {
                const clonedLink = parent.cloneNode(true);
                clonedLink.removeAttribute('href');

                clonedLink.addEventListener('click', (event) => {
                    event.preventDefault();
                    if (confirm(MSG)) {
                        parent.click();
                    }
                });
                parent.parentElement.insertBefore(clonedLink, parent);
                parent.style.display = 'none';

            }
        });
    }
})();