Flight Club Send to Mia

One stop button to send items to MIA [131289]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Flight Club Send to Mia
// @namespace    https://www.torn.com/profiles.php?XID=3890054
// @version      1.0.3
// @description  One stop button to send items to MIA [131289]
// @author       Njoric
// @match        https://www.torn.com/item.php*
// @match        https://www.torn.com/items.php*
// @run-at       document-idle
// @grant        none
// @connect      torn.com
// @connect      *.torn.com
// @license      MIT 
// ==/UserScript==

//////// PREFILL VALUES ////////
const prefillAmountVal = ['999999']; // Prefill amount value
const prefillPlayerVal = ['MIA [131289]']; // Recipient for all items
const prefillMessageVal = ['Edit script message prefills', ''];

//////// INDEXES USED BY FUNCTIONS
// DO NOT CHANGE
let amountIndex = 0;
let playerIndex = 0;
let messageIndex = 0;

//////// FUNCTIONS ////////
function prefillAmount(inputEl) {
  const maxAmount = inputEl.dataset.max;

  inputEl.value = +prefillAmountVal[amountIndex] > +maxAmount ? +maxAmount : +prefillAmountVal[amountIndex];
  console.log('Amount input:', inputEl.value); // TEST
  inputEl.dispatchEvent(new Event('input', { bubbles: true }));
}

function prefillPlayer(inputEl) {
  inputEl.value = prefillPlayerVal[playerIndex];
  console.log('Player input:', inputEl.value); // TEST
  inputEl.dispatchEvent(new Event('input', { bubbles: true }));
}

function prefillMessage(inputEl, message) {
  console.log('Message:', message); // TEST
  if (message === undefined) {
    inputEl.value = prefillMessageVal[messageIndex];
  }
  if (message !== undefined) {
    inputEl.value = '';
  }
  inputEl.dispatchEvent(new Event('input', { bubbles: true }));
}

function amountClickHandler(e) {
  const max = prefillAmountVal.length - 1;
  if (++amountIndex > max) amountIndex = 0;

  prefillAmount(e.target);
  e.target.focus();
  e.target.select();
}

function playerClickHandler(e) {
  const max = prefillPlayerVal.length - 1;
  if (++playerIndex > max) playerIndex = 0;

  prefillPlayer(e.target);
  e.target.focus();
  e.target.select();
}

function messageClickHandler(e) {
  const max = prefillMessageVal.length - 1;
  if (++messageIndex > max) messageIndex = 0;

  prefillMessage(e.target);
  e.target.focus();
  e.target.select();
}

function removeMessageClickHandler(e, inputEl) {
  console.log('Remove message'); // TEST
  prefillMessage(inputEl, '');
}

async function sendItemObserverCallback(mutationList, observer) {
  for (const mutation of mutationList) {
    if (mutation.target.classList.contains('cont-wrap') && mutation.type !== 'attributes') {
      if (mutation.addedNodes.length > 0 && mutation.addedNodes[0].classList.length > 0 && mutation.addedNodes[0].classList.contains('send-act')) {
        const sendActionEl = mutation.addedNodes[0];
        const amountInputEl = sendActionEl.querySelector('input.amount[type="text"]');
        const playerInputEl = sendActionEl.querySelector('input.user-id');

        prefillAmount(amountInputEl);
        prefillPlayer(playerInputEl);

        amountInputEl.addEventListener('click', amountClickHandler);
        playerInputEl.addEventListener('click', playerClickHandler);
      }
    }
    if (mutation.target.classList.contains('msg-active')) {
      console.log(mutation); // TEST
      const messageContainerEl = mutation.target;
      const messageInput = messageContainerEl.querySelector('input.message');
      const removeMessageBtn = messageContainerEl.querySelector('.action-remove');

      prefillMessage(messageInput);
      messageInput.addEventListener('click', messageClickHandler);
      removeMessageBtn.addEventListener('click', (e) => removeMessageClickHandler(e, messageInput));
    }
  }
}

function createSenditemMutationObserver() {
  const observer = new MutationObserver(sendItemObserverCallback);
  observer.observe(document, {
    attributes: true,
    childList: true,
    subtree: true,
  });
}

// document.addEventListener('input', (e) => console.log(e)); // TEST

(async () => {
  console.log('🎁 Prefill item send script is ON!'); // TEST

  createSenditemMutationObserver();
})();