Amazon Wish List Valuer

Displays the total value of an Amazon wish list

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Amazon Wish List Valuer
// @namespace    https://greasyfork.org/en/users/1262395-grinnch
// @version      1.1
// @description  Displays the total value of an Amazon wish list
// @author       grinnch
// @license      MIT
// @match        https://www.amazon.com/hz/wishlist/*
// @match        https://www.amazon.ca/hz/wishlist/*
// @match        https://www.amazon.co.uk/hz/wishlist/*
// @icon         https://www.wildbirdfund.org/wp-content/uploads/2016/04/UWL_SWF_shims._CB368675346_.png
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const STORAGE_KEY = 'wishlistToggleStates';

    const loadStates = () => {
        const storedStates = localStorage.getItem(STORAGE_KEY);
        return storedStates ? JSON.parse(storedStates) : {};
    };

    const saveState = (itemId, isChecked) => {
        const states = loadStates();
        states[itemId] = isChecked;
        localStorage.setItem(STORAGE_KEY, JSON.stringify(states));
    };

    const titleElement = document.getElementById('profile-list-name');
    if (!titleElement) {
        return;
    }

    const displayNode = document.createElement("span");
    displayNode.id = 'ws-total-display';
    displayNode.style.marginLeft = '10px';
    titleElement.appendChild(displayNode);

    const recalculateTotal = () => {
        let sum = 0;
        const allItemRows = document.querySelectorAll('#g-items > li.ws-item-row');

        allItemRows.forEach(row => {
            const checkbox = row.querySelector('.ws-toggle-include');
            if (checkbox && checkbox.checked) {
                sum += parseFloat(row.dataset.price);
            }
        });

        const finalSum = sum.toFixed(2);
        const sumWithTax = (sum * 1.13).toFixed(2);
        const firstSymbolEl = document.querySelector('.a-price-symbol');
        const symbol = firstSymbolEl ? firstSymbolEl.textContent : '$';

        displayNode.textContent = `(Total: ${symbol}${finalSum} / ${symbol}${sumWithTax} with ~13% tax)`;
    };

    const allListItems = document.querySelectorAll('#g-items > li');
    const savedStates = loadStates();

    allListItems.forEach(listItem => {
        const priceEl = listItem.querySelector('[id^=itemPrice_]');
        const actionContainer = listItem.querySelector('[id^=itemAction_]');

        if (priceEl && actionContainer) {
            const itemId = actionContainer.id.replace('itemAction_', '');
            const wholePart = priceEl.querySelector('.a-price-whole');
            const fractionPart = priceEl.querySelector('.a-price-fraction');

            if (wholePart && fractionPart) {
                const whole = wholePart.textContent.replace(/[,.]/g, '').trim();
                const fraction = fractionPart.textContent.trim();
                const cost = parseFloat(`${whole}.${fraction}`);

                if (!isNaN(cost)) {
                    listItem.classList.add('ws-item-row');
                    listItem.dataset.price = cost;

                    const toggleContainer = document.createElement('div');
                    toggleContainer.style.cssText = 'margin-top: 8px; color: #333; font-size: 13px;';

                    const checkbox = document.createElement('input');
                    checkbox.type = 'checkbox';
                    checkbox.className = 'ws-toggle-include';
                    checkbox.checked = savedStates[itemId] !== false;
                    checkbox.style.cssText = 'margin-right: 5px; vertical-align: middle; position: relative; top: -1px;';

                    checkbox.addEventListener('change', () => {
                        saveState(itemId, checkbox.checked);
                        recalculateTotal();
                    });

                    const label = document.createElement('label');
                    label.style.cssText = 'cursor: pointer; font-weight: normal;';
                    label.appendChild(checkbox);
                    label.appendChild(document.createTextNode('Include in Total'));

                    toggleContainer.appendChild(label);
                    actionContainer.appendChild(toggleContainer);
                }
            }
        }
    });

    recalculateTotal();
})();