Beanfun QR Code Click-to-Copy

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Beanfun QR Code Click-to-Copy
// @namespace    http://tampermonkey.net/
// @version      0.7.1
// @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';
    const LISTENER_ADDED_FLAG = 'gm_qr_listener_added';

    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);
    }

     function setupClickListener(targetIframe) {
         try {
             setTimeout(() => {
                 const iframeDoc = targetIframe.contentDocument || targetIframe.contentWindow?.document;
                 if (!iframeDoc) {
                     return;
                 }

                 const qrImageElement = iframeDoc.querySelector(QR_CODE_IMAGE_SELECTOR);

                 if (qrImageElement) {
                     if (qrImageElement.dataset[LISTENER_ADDED_FLAG]) {
                         return;
                     }

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

                     qrImageElement.addEventListener('click', (event) => {
                         event.preventDefault();
                         event.stopPropagation();
                         event.stopImmediatePropagation();

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

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

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

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

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

                     qrImageElement.dataset[LISTENER_ADDED_FLAG] = 'true';

                 }
             }, 250);

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

    const checkInterval = setInterval(() => {
        const targetIframe = document.getElementById(IFRAME_ID);
        if (targetIframe) {
            clearInterval(checkInterval);
            targetIframe.addEventListener('load', () => setupClickListener(targetIframe));
             setupClickListener(targetIframe);
        }
    }, 500);

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

})();