Trade Offer Helper

Auto-fill the trade form from URL parameters, allow purchasing by total price, and get rid of the trade confirmation pop-up

目前為 2022-04-15 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Trade Offer Helper
// @namespace    http://knightsradiant.pw/
// @version      0.5
// @description  Auto-fill the trade form from URL parameters, allow purchasing by total price, and get rid of the trade confirmation pop-up
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @author       Talus
// @match        https://politicsandwar.com/nation/trade/create*
// @grant        none
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    var AMOUNT_TEXT_SELECTOR = '#createTrade > div.row > div > table > tbody > tr:nth-child(3) > td:nth-child(1)';
    var UNITS_SELECTOR = '#createTrade > div.row > div > table > tbody > tr:nth-child(3)';
    var AMOUNT_SELECTOR = '#amount';
    var PRICE_PER_SELECTOR = '#priceper';
    var OFFER_TOTAL_PRICE_SELECTOR = '#offer_total_price';
    var BUY_BUTTON_SELECTOR = '#createTrade > div.row > div > table > tbody > tr:nth-child(11) > td > div > button.btn.btn-primary.btn-lg';
    var SELL_BUTTON_SELECTOR = '#createTrade > div.row > div > table > tbody > tr:nth-child(11) > td > div > button.btn.btn-success.btn-lg';

    var $ = window.jQuery;
    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)', 'i').exec(window.location.href);
        if (results) {
            return results[1] || 0;
        }
    }

    $(AMOUNT_TEXT_SELECTOR).text('Desired Units:');
    var total_price = $(AMOUNT_SELECTOR).val() * $(PRICE_PER_SELECTOR).val();
    $(UNITS_SELECTOR).after('<tr><td>Desired Total Price:</td><td><input type="number" name="offer_total_price" minimum = "1" id="'+OFFER_TOTAL_PRICE_SELECTOR.substr(1)+'" style="width:100%;" value="'+total_price+'" class="right" /></td></tr>');
    $(AMOUNT_SELECTOR).on("keyup change", function() {
        $(OFFER_TOTAL_PRICE_SELECTOR).val(Math.floor($(AMOUNT_SELECTOR).val() * $(PRICE_PER_SELECTOR).val()));
        $(AMOUNT_SELECTOR).trigger("change");
    })
    $(OFFER_TOTAL_PRICE_SELECTOR).on("change", function() {
        $(AMOUNT_SELECTOR).val(Math.floor($(OFFER_TOTAL_PRICE_SELECTOR).val() / $(PRICE_PER_SELECTOR).val()));
        $(AMOUNT_SELECTOR).trigger("change");
    })
    $(PRICE_PER_SELECTOR).on("keyup change", function() {
        $(OFFER_TOTAL_PRICE_SELECTOR).val(Math.floor($(AMOUNT_SELECTOR).val() * $(PRICE_PER_SELECTOR).val()));
        $(PRICE_PER_SELECTOR).trigger("change");
    })

    $(BUY_BUTTON_SELECTOR).attr('type', 'submit');
    $(BUY_BUTTON_SELECTOR).attr('name', 'submit');
    $(BUY_BUTTON_SELECTOR).attr('form', 'createTrade');
    $(BUY_BUTTON_SELECTOR).attr('value', 'Buy');
    $(BUY_BUTTON_SELECTOR).attr('data-toggle', null);
    $(BUY_BUTTON_SELECTOR).attr('data-target', null);

    $(SELL_BUTTON_SELECTOR).attr('type', 'submit');
    $(SELL_BUTTON_SELECTOR).attr('name', 'submit');
    $(SELL_BUTTON_SELECTOR).attr('form', 'createTrade');
    $(SELL_BUTTON_SELECTOR).attr('value', 'Sell');
    $(SELL_BUTTON_SELECTOR).attr('data-toggle', null);
    $(SELL_BUTTON_SELECTOR).attr('data-target', null);

    if ($.urlParam('action')) {
        var action = $.urlParam('action').toLowerCase();
        switch (action) {
            case 'buy':
                $(BUY_BUTTON_SELECTOR).css('border-radius', '6px')
                $(SELL_BUTTON_SELECTOR).hide()
                break;
            case 'sell':
                $(SELL_BUTTON_SELECTOR).css('border-radius', '6px')
                $(BUY_BUTTON_SELECTOR).hide()
                break;
        }
    }
})();