Price Converter with Tax for Specific Websites

Convert price tags website

La data de 17-05-2023. Vezi ultima versiune.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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

// ==/UserScript==

(function() {
    'use strict';

    const TAX_RATE = 0.14975;

    const websites = [
        {
            domain: 'newegg.ca',
            selectors: ['li.price-current', '.goods-info'],
            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);
                    strongElement.textContent = convertedPrice.split('.')[0];
                    supElement.textContent = `.${convertedPrice.split('.')[1]}`;
                }
            }
        },
        {
            domain: 'canadacomputers.com',
            selectors: [
                '.col-auto.col-md-12.order-2.order-md-1.text-red .h2-big strong',
                '.h2-big > strong:nth-child(1)',
                '.d-block.mb-0.pq-hdr-product_price.line-height',
            ],
            updatePrice: (element) => {
                const price = parseFloat(element.textContent.replace('$', '').replace(',', ''));
                const convertedPrice = convertPrice(price);
                element.textContent = `$${convertedPrice}`;
            }
        }
    ];

    function convertPrice(price) {
        const priceWithTax = price * (1 + TAX_RATE);
        return priceWithTax.toFixed(2);
    }

    function updatePriceTags(website) {
        website.selectors.forEach(selector => {
            const priceElements = document.querySelectorAll(selector);
            priceElements.forEach(element => {
                website.updatePrice(element);
            });
        });
    }

    const hostname = window.location.hostname;

    websites.forEach(website => {
        if (hostname.includes(website.domain)) {
            updatePriceTags(website);
        }
    });
})();