Greasy Fork

Subeta Autobuy

This script automatically buys items at the lowest price on the Shop Search page.

目前为 2017-06-28 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/30965/202967/Subeta%20Autobuy.js

// ==UserScript==
// @name         Subeta Autobuy
// @namespace    http://artees.pw/
// @description  This script automatically buys items at the lowest price on the Shop Search page.
// @version      0.1
// @author       Artees
// @match        *://subeta.net/user_shops.php/search/shops/*
// @match        *://subeta.net/shop.php?shopid=*
// @grant        none
// @icon         https://subeta.net/favicon.ico
// @icon64       https://img.subeta.net/items/accessory_hustlermoneyclip.gif
// ==/UserScript==

const KEY_AUTOBUY_SOURCE = "autobuySource",
    KEY_AUTOBUY_NPC = "autobuyNpc";

buy();

function buy() {
    var source = sessionStorage[KEY_AUTOBUY_SOURCE];
    if (source === undefined) return;
    sessionStorage.removeItem(KEY_AUTOBUY_SOURCE);
    var rows = document.getElementsByClassName("row"),
        items = [];
    if (rows.length === 0) return;
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i],
            buttons = getButtons(row);
        if (buttons.length === 0) continue;
        items.push(row);
    }
    function getButtons(e) {
        return e.getElementsByClassName("btn btn-primary btn-sm");
    }
    items.sort(comparePrices);
    var item = items[0],
        isNPC = item.innerText.indexOf("NPC Shop") !== -1;
    if (isNPC) {
        sessionStorage[KEY_AUTOBUY_NPC] = source;
    }
    getButtons(item)[0].click();
    if (!isNPC) {
        open(source, "_self");
    }
}

function comparePrices(a, b) {
    var aPrice = getPrice(a),
        bPrice = getPrice(b);
    function getPrice(item) {
        var itemText = item.innerHTML,
            end = itemText.indexOf(" sP"),
            start = itemText.lastIndexOf(">", end) + 1,
            priceText = replaceAll(itemText.substring(start, end), ",", "");
        return parseInt(priceText);
    }
    return aPrice - bPrice;
}

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

back();

function back() {
    if (document.body.textContent.indexOf("been put in your items") === -1) return;
    var source = sessionStorage[KEY_AUTOBUY_NPC];
    if (source === undefined) return;
    sessionStorage.removeItem(KEY_AUTOBUY_NPC);
    open(source, "_self");
}