Rtings Open Shopping Links in New Tab Only

Opens shopping links in new tabs on rtings.com without affecting the current tab

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Rtings Open Shopping Links in New Tab Only
// @namespace   https://greasyfork.org/en/users/594496-divided-by
// @author      dividedby
// @description Opens shopping links in new tabs on rtings.com without affecting the current tab
// @version     1.1
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @contributionURL     https://www.paypal.com/cgi-bin/webscr?cmd=_donations&[email protected]&item_name=Rtings+Tab+Donation
// @contributionAmount  $1
// @match       https://www.rtings.com/*
// run-at       document-idle

// ==/UserScript==

(function() {
    'use strict';

    // Array of domain patterns to match
    const domainPatterns = [
        'amazon.com',
        'ebay.com',
        'walmart.com',
        'target.com',
        'bestbuy.com',
        'bhphotovideo.com',
        'shop-links.co'
    ];

    function handleClick(event) {
        const link = event.currentTarget;
        if (domainPatterns.some(pattern => link.href.includes(pattern))) {
            event.preventDefault();
            event.stopPropagation();
            window.open(link.href, '_blank', 'noopener,noreferrer');
        }
    }

    function enhanceLinks() {
        const selector = domainPatterns.map(pattern => `a[href*="${pattern}"]`).join(',');
        const links = document.querySelectorAll(`${selector}:not([data-enhanced])`);
        links.forEach(link => {
            link.setAttribute('data-enhanced', 'true');
            link.addEventListener('click', handleClick, true);
        });
    }

    enhanceLinks();

    const observer = new MutationObserver(mutations => {
        if (mutations.some(mutation => mutation.addedNodes.length > 0)) {
            enhanceLinks();
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });
})();