PTT Comment Auto Update & Reload

Automatically enables comment auto-update on PTT webpages and reloads the page upon errors.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name               PTT Comment Auto Update & Reload
// @name:zh-TW         PTT 留言自動更新載入
// @namespace          wellstsai.com
// @version            1.0.1
// @license            MIT
// @description        Automatically enables comment auto-update on PTT webpages and reloads the page upon errors.
// @description:zh-TW  自動啟用 PTT 網頁的留言自動更新功能,並在發生錯誤時重新載入網頁。
// @author             WellsTsai
// @match              https://www.ptt.cc/*
// @grant              none
// @run-at             document-idle
// ==/UserScript==

(() => {
    'use strict'

    const enableAutoUpdate = () => {
        const pollerDiv = document.querySelector('#article-polling')

        if (!pollerDiv) {
            console.warn('PTT AutoReload: #article-polling not found.')
            return
        }

        pollerDiv.click()
        console.log('PTT AutoReload: Auto-update enabled.')

        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                if (mutation.target.textContent.includes('無法更新推文')) {
                    console.warn('PTT AutoReload: Detected update error, reloading page...')
                }
            })
        })

        observer.observe(pollerDiv, { childList: true, subtree: true })
    }

    enableAutoUpdate()
})()