Greasy Fork

Greasy Fork is available in English.

Price Converter with Tax for Specific Websites

Convert price tags on websites

当前为 2023-05-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Price Converter with Tax for Specific Websites
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Convert price tags on websites
// @author       Nears
// @match        *://*.newegg.ca/*
// @match        *://*.canadacomputers.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Define the tax rate
    const TAX_RATE = 0.14975;

    // Add CSS block
    const css = `
        .tax-included {
            color: red;
            font-size: 0.8em;
        }
        .base-price {
            color: #666;
            font-size: 0.8em;
            text-decoration: line-through;
        }
    `;
    const styleElement = document.createElement('style');
    styleElement.textContent = css;
    document.head.appendChild(styleElement);

    // Define the websites and their price tag selectors
    const websites = [
        {
            domain: 'newegg.ca',
            selectors: ['li.price-current', '.goods-price-current'],
            updatePrice: (element) => {
                const strongElement = element.querySelector('strong');
                const supElement = element.querySelector('sup');
                if (strongElement && supElement) {
                    const price = parseFloat(`${strongElement.textContent.replace(',', '')}${supElement.textContent}`);
                    const convertedPrice = convertPrice(price);
                    element.innerHTML = `<div class="tax-included">$${convertedPrice}</div><div class="base-price">Base price: $${price.toFixed(2)}</div>`;
                }
            }
        },
        {
            domain: 'canadacomputers.com',
            selectors: [
                '.h2-big > strong:nth-child(1)',
                '.text-red d-block mb-0 pq-hdr-product_price line-height',
                '.d-block.mb-0.pq-hdr-product_price.line-height',
                '.text-danger h2 price',
            ],
            updatePrice: (element) => {
                const price = parseFloat(element.textContent.replace('$', '').replace(',', ''));
                const convertedPrice = convertPrice(price);
                element.outerHTML = `<div class="tax-included">$${convertedPrice}</div><div class="base-price">Base price: $${price.toFixed(2)}</div>`;
            }
        }
    ];

    // Function to convert the price with tax
    function convertPrice(price) {
        const priceWithTax = price * (1 + TAX_RATE);
        return priceWithTax.toFixed(2);
    }

    // Function to update price tags on the website
    function updatePriceTags(website) {
        website.selectors.forEach(selector => {
            const priceElements = document.querySelectorAll(selector);
            priceElements.forEach(element => {
                website.updatePrice(element);
            });
        });
    }

    // Get the current hostname
    const hostname = window.location.hostname;

    // Update price tags for the matching website
    websites.forEach(website => {
        if (hostname.includes(website.domain)) {
            updatePriceTags(website);
        }
    });
})();