Beanfun QR Code Click-to-Copy

在 beanfun! QR Code 登入頁面,點擊 QR Code 圖片時,自動複製其連結到剪貼簿。

目前為 2025-05-01 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Beanfun QR Code Click-to-Copy
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  在 beanfun! QR Code 登入頁面,點擊 QR Code 圖片時,自動複製其連結到剪貼簿。
// @author       eunalice (Generated by Gemini 2.5 Pro)
// @match        *://*.beanfun.com/loginform.aspx*
// @match        *://tw.newlogin.beanfun.com/loginform.aspx*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const IFRAME_ID = 'ifmForm8';
    const QR_CODE_IMAGE_SELECTOR = '#theQrCodeImg';
    const PREFIX_TO_REMOVE = 'https://tw.newlogin.beanfun.com/qrhandler.ashx?u=';
    const NOTIFICATION_ID = 'gm-qr-copy-notify';

    GM_addStyle(`
        #${NOTIFICATION_ID} {
            position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
            background-color: rgba(0, 0, 0, 0.8); color: white; padding: 10px 20px;
            border-radius: 5px; z-index: 10000; font-size: 14px;
            opacity: 0; transition: opacity 0.5s ease-in-out; pointer-events: none;
        }
        #${NOTIFICATION_ID}.visible { opacity: 1; }
    `);

    function showNotification(message, duration = 3000) {
        let notify = document.getElementById(NOTIFICATION_ID);
        if (!notify) {
            notify = document.createElement('div');
            notify.id = NOTIFICATION_ID;
            document.body.appendChild(notify);
        }
        notify.textContent = message;
        notify.offsetHeight;
        notify.classList.add('visible');
        if (notify.timer) clearTimeout(notify.timer);
        notify.timer = setTimeout(() => {
            notify.classList.remove('visible');
        }, duration);
    }

    const checkInterval = setInterval(() => {
        const targetIframe = document.getElementById(IFRAME_ID);
        if (targetIframe) {
            clearInterval(checkInterval);

            targetIframe.addEventListener('load', () => {
                 try {
                    setTimeout(() => {
                        const iframeDoc = targetIframe.contentDocument || targetIframe.contentWindow?.document;
                        if (!iframeDoc) {
                            return;
                        }

                        const qrImageElement = iframeDoc.querySelector(QR_CODE_IMAGE_SELECTOR);

                        if (qrImageElement) {
                            qrImageElement.style.cursor = 'pointer';
                            qrImageElement.title = '點擊以複製鏈結';

                            qrImageElement.replaceWith(qrImageElement.cloneNode(true));
                            const newQrImageElement = iframeDoc.querySelector(QR_CODE_IMAGE_SELECTOR);
                            if (!newQrImageElement) return;

                            newQrImageElement.addEventListener('click', (event) => {
                                event.preventDefault();

                                let qrUrl = newQrImageElement.src || newQrImageElement.dataset.url;

                                if (qrUrl && String(qrUrl).trim()) {
                                    let originalUrl = String(qrUrl).trim();
                                    let finalValueToCopy = originalUrl;

                                    if (originalUrl.startsWith(PREFIX_TO_REMOVE)) {
                                        finalValueToCopy = originalUrl.substring(PREFIX_TO_REMOVE.length);
                                    }

                                    GM_setClipboard(finalValueToCopy);
                                    showNotification('已複製鏈結!');

                                } else {
                                    showNotification('錯誤:無法從圖片獲取有效資料');
                                }
                            });

                        } else {
                            // Element not found, do nothing silently in cleaned version
                        }
                    }, 100);

                 } catch (e) {
                     if (e.name === 'SecurityError') {
                         showNotification('錯誤:無法設定點擊事件 (安全性限制)');
                     } else {
                         showNotification('設定點擊事件時發生錯誤');
                     }
                 }
             });
        }
    }, 500);

    setTimeout(() => {
        clearInterval(checkInterval);
    }, 15000);

})();