Auto Popup Killer InuHK

「アカウント登録のお願い」ポップアップの「閉じる」ボタンを自動でクリックします。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Popup Killer InuHK
// @version      0.2
// @description  「アカウント登録のお願い」ポップアップの「閉じる」ボタンを自動でクリックします。
// @match        *://news.web.nhk/*
// @match        *://*.nhk.or.jp/*
// @match        *://*.nhk.jp/*
// @run-at       document-idle
// @grant        none
// @namespace    shino-tools
// @license CC0-1.0
// ==/UserScript==

(function () {
    'use strict';

    let alreadyClosed = false;
    let observer = null;
    let initialized = false;

    function findAndClickCloseButton() {
        if (alreadyClosed) return;

        const containers = Array.from(
            document.querySelectorAll('div, section, article, dialog')
        );

        for (const el of containers) {
            const text = (el.textContent || '').replace(/\s+/g, ' ');

            // 「NHK ONE アカウント登録のお願い」を含む要素に絞る
            if (!/NHK\s*ONE.*アカウント登録のお願い/.test(text)) {
                continue;
            }

            // その中の「閉じる」ボタンらしき要素を探す
            const buttons = el.querySelectorAll('button, [role="button"], a');

            for (const btn of buttons) {
                const labelText =
                    (btn.textContent ||
                        btn.getAttribute('aria-label') ||
                        '').trim();

                if (
                    labelText.includes('閉じる') ||
                    labelText.includes('とじる') ||
                    labelText.toLowerCase().includes('close')
                ) {
                    // 実際のクリック
                    btn.click();
                    alreadyClosed = true;

                    if (observer) {
                        observer.disconnect();
                    }
                    return;
                }
            }
        }
    }

    function init() {
        if (initialized) return;
        initialized = true;

        // ページロード完了後に一度チェック
        findAndClickCloseButton();

        // 動的にポップアップが出るケースに備えて監視
        observer = new MutationObserver(() => {
            findAndClickCloseButton();
        });

        observer.observe(document.documentElement, {
            childList: true,
            subtree: true,
        });

        // 念押しで少し遅らせて再チェック
        setTimeout(findAndClickCloseButton, 1000);
        setTimeout(findAndClickCloseButton, 3000);
        setTimeout(findAndClickCloseButton, 5000);
    }

    // @run-at document-idle にしているので、基本的には DOM 構築も完了後に呼ばれますが、
    // 念のため readyState を見てロード完了後に初期化しています。
    if (document.readyState === 'complete') {
        init();
    } else {
        window.addEventListener('load', init);
    }
})();