Open Elements in Background Tab

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

目前為 2024-11-01 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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