Tokyo Otaku Mode Stock Level

Shows the stock level on the product pages of items on TOM

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Tokyo Otaku Mode Stock Level
// @namespace    https://tharglet.me.uk/
// @version      1.0
// @description  Shows the stock level on the product pages of items on TOM
// @author       tharglet
// @include      /^https:\/\/otakumode.com\/shop\/[a-f0-9]{24}\//
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    let re = /^https:\/\/otakumode.com\/shop\/([a-f0-9]{24})\//;
    let matches = window.location.href.match(re);
    let productId = matches[1];
    console.log(productId);
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            let productJson = JSON.parse(this.response);
            //Filter to the valid options
            const validSkus = productJson.skus.filter((sku) => {
                return !sku.is_denied_country;
            });
            if(validSkus.length === 1) {
                let stockLevel = validSkus[0].stock_count;
                if(stockLevel === -1) {
                    stockLevel = 'Unlimited!';
                }
                let stockLevelP = document.createElement('p');
                const t = document.createTextNode(`Stock available: ${stockLevel}`);
                stockLevelP.appendChild(t);
                const heading = document.getElementsByTagName("h1")[0];
                heading.parentNode.insertBefore(stockLevelP, heading.nextSibling);
            } else {
                let stockBlock = document.createElement('div');
                stockBlock.className = "u-mbs";
                validSkus.forEach((sku) => {
                    let stockLevel = sku.stock_count;
                    if(stockLevel === -1) {
                        stockLevel = 'Unlimited!';
                    }
                    let stockLevelP = document.createElement('p');
                    const t = document.createTextNode(`${sku.short_titles.en_us} stock available: ${stockLevel}`);
                    stockLevelP.appendChild(t);
                    stockBlock.appendChild(stockLevelP);
                });
                const heading = document.getElementsByTagName("h1")[0];
                heading.parentNode.insertBefore(stockBlock, heading.nextSibling);
            }
        }
    };
    xhttp.open('GET', `/shop/getProduct?_id=${productId}`, true);
    xhttp.send();
})();