Subeta Searching

This script searches for shops.

目前為 2017-06-28 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/30529/203146/Subeta%20Searching.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Subeta Searching
// @namespace    http://artees.pw/
// @description  This script searches for shops.
// @version      1.3
// @author       Artees
// @match        *://subeta.net/user_shops.php/mine/*
// @match        *://subeta.net/shop.php?shop*
// @exclude      *quick_stock*
// @exclude      *profits*
// @exclude      *history*
// @exclude      *edit_shop*
// @exclude      *delete_shop*
// @grant        none
// @icon         https://subeta.net/favicon.ico
// @icon64       https://img.subeta.net/items/identifier_04_crystal.gif
// ==/UserScript==

const CY = " sP",
    SPLITTER = "    | ",
    XHR = new XMLHttpRequest(),
    KEY_ITEM_DELAY = "itemDelay";

var itemDelay = getFromStorage(localStorage, KEY_ITEM_DELAY, 5),
    itemCountdown = itemDelay;

function parseItems(itemDivs, getPriceField) {
    var prevItem = null,
        firstItem;
    for (var i = 0; i < itemDivs.length; i++) {
        var priceField = getPriceField(itemDivs[i]);
        if (priceField === null) {
            continue;
        }
        var curItem = new Item(itemDivs[i], priceField);
        if (curItem.priceField === null) {
            continue;
        }
        if (prevItem === null) {
            firstItem = curItem;
        } else {
            prevItem.next = curItem;
        }
        prevItem = curItem;
    }
    prevItem.next = firstItem;
    return firstItem;
}

function getFromStorage(storage, key, defaultValue) {
    if (storage[key] === undefined) {
        return defaultValue;
    } else {
        return typeof defaultValue === "boolean" ? storage[key] === "true" : storage[key];
    }
}

function forEachItem(action) {
    var f = curItem,
        i = f;
    do {
        if (!action(i)) {
            return;
        }
        i = i.next;
    } while (i !== f);
}

function createSpace() {
    var space = document.createElement("SPAN");
    space.innerText = "|";
    space.style = "display: inline-block; width: 50px;";
    return space;
}

function search() {
    XHR.open("GET", "https://subeta.net/user_shops.php/search/shops/" + curItem.name, true);
    XHR.send();
    curItem.priceField.value += SPLITTER;
    loadIntervalId = setInterval(updateSearchProgress, 100);
}

function updateSearchProgress() {
    if (curItem.priceField.value.length > 20) {
        curItem.priceField.value = "";
        XHR.abort();
        clearInterval(loadIntervalId);
        search();
        return;
    }
    curItem.priceField.value += ".";
}

XHR.onreadystatechange = function setLowestPrice() {
    if (XHR.readyState !== XMLHttpRequest.DONE || XHR.status !== 200) return;
    clearInterval(loadIntervalId);
    var pricesText = XHR.responseText.split(CY),
        prices = [];
    for (var i = 0; i < pricesText.length; i++) {
        var priceWithCommas = pricesText[i].substring(pricesText[i].lastIndexOf(">") + 1),
            priceText = replaceAll(priceWithCommas, ",", ""),
            price = parseInt(priceText);
        if (isNaN(price)) continue;
        var isNPC = pricesText[i].indexOf("(NPC Shop)") !== -1,
            priceObject = new Price(price, isNPC);
        prices.push(priceObject);
    }
    if (prices.length > 0) {
        prices.sort(compareNumbers);
    }
    filterPrices(prices);
    curItem = curItem.next;
    if (curItem.onSelect()) {
        itemIntervalId = setInterval(updateItemCountdown, 1000);
    }
};

function Price(value, isNPC) {
    this.isNPC = function () {
        return isNPC;
    };
    this.valueOf = function () {
        return value;
    };
    this.toString = function () {
        return value.toString();
    };
}

function replaceAll(target, searchValue, replaceValue) {
    return target.replace(new RegExp(searchValue, 'g'), replaceValue);
}

function compareNumbers(a, b)
{
    return a - b;
}

function updateItemCountdown() {
    curItem.priceField.value = curItem.resetPrice() + SPLITTER + itemCountdown;
    itemCountdown--;
    if (itemCountdown >= 0) return;
    curItem.priceField.value = curItem.resetPrice();
    itemCountdown = itemDelay;
    clearInterval(itemIntervalId);
    search();
}