Open Elements in Background Tab

將選定的元素在背景分頁或新分頁開啟

Versione datata 01/11/2024. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Open Elements in Background Tab
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  將選定的元素在背景分頁或新分頁開啟
// @author       You
// @match        https://tixcraft.com/ticket/area/*
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 設置預設值:true 為背景開啟,false 為新分頁開啟
    const OPEN_IN_BACKGROUND = GM_getValue('openInBackground', true);

    // 在頁面頂部添加切換按鈕
    function addToggleButton() {
        const button = document.createElement('button');
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.right = '10px';
        button.style.zIndex = '9999';
        button.style.padding = '5px 10px';
        button.style.backgroundColor = '#f0f0f0';
        button.style.border = '1px solid #ccc';
        button.style.borderRadius = '4px';
        button.style.cursor = 'pointer';

        function updateButtonText() {
            const currentValue = GM_getValue('openInBackground', true);
            button.textContent = `當前模式: ${currentValue ? '背景開啟' : '新分頁開啟'}`;
        }

        updateButtonText();

        button.onclick = function() {
            const currentValue = GM_getValue('openInBackground', true);
            GM_setValue('openInBackground', !currentValue);
            updateButtonText();
        };

        document.body.appendChild(button);
    }

    // 從頁面腳本中提取 areaUrlList
    function extractAreaUrlList() {
        const scripts = document.getElementsByTagName('script');
        let urlList = {};
        for (const script of scripts) {
            if (script.textContent.includes('var areaUrlList =')) {
                try {
                    const match = script.textContent.match(/var areaUrlList = (\{[^;]+\});/);
                    if (match && match[1]) {
                        urlList = JSON.parse(match[1]);
                        console.log('Successfully extracted areaUrlList:', urlList);
                    }
                } catch (e) {
                    console.error('Error parsing areaUrlList:', e);
                }
            }
        }
        return urlList;
    }

    function init() {
        addToggleButton();
        const urlList = extractAreaUrlList();

        // 使用 capture 階段來確保我們的處理器最先執行
        document.addEventListener('click', function(event) {
            const clickedElement = event.target.closest('a[id]');
            if (clickedElement && event.ctrlKey) {
                // 阻止事件繼續傳播和預設行為
                event.preventDefault();
                event.stopPropagation();
                event.stopImmediatePropagation();

                const elementId = clickedElement.id;
                const url = urlList[elementId];
                if (url) {
                    const openInBackground = GM_getValue('openInBackground', true);
                    console.log('Opening URL:', url, 'for ID:', elementId, 'in background:', openInBackground);
                    GM_openInTab(url, { active: !openInBackground });
                } else {
                    console.log('No URL found for ID:', elementId);
                }
                return false;
            }
        }, true);

        // 額外添加一個事件攔截器到具體的連結上
        document.querySelectorAll('a[id]').forEach(link => {
            link.addEventListener('click', function(event) {
                if (event.ctrlKey) {
                    event.preventDefault();
                    event.stopPropagation();
                    event.stopImmediatePropagation();
                    return false;
                }
            }, true);
        });
    }

    // 如果 DOM 已經加載完成,直接執行
    if (document.readyState === 'complete' || document.readyState === 'interactive') {
        init();
    } else {
        // 否則等待 DOM 加載完成
        document.addEventListener('DOMContentLoaded', init);
    }
})();