Beanfun QR Code Click-to-Copy

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

Från och med 2025-05-01. Se den senaste versionen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);

})();