TORN: Prefill Item Send

Allows Prefill values to be set for amounts and players

当前为 2023-12-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TORN: Prefill Item Send
  3. // @namespace http://torn.city.com.dot.com.com
  4. // @version 1.1.1
  5. // @description Allows Prefill values to be set for amounts and players
  6. // @author IronHydeDragon[2428902]
  7. // @match https://www.torn.com/item.php*
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. //////// PREFILL VALUES ////////
  12. const prefillAmountVal = ['2', '5', '999999', '']; // 999999 is just some rediculous number that should be equivalent to 'max'
  13. const prefillPlayerVal = ['Kv0the [2153277]', ''];
  14. const prefillMessageVal = ['Edit script message prefills', ''];
  15.  
  16. //////// INDEXES USED BY FUNCTIONS
  17. // DO NOT CHANGE
  18. let amountIndex = 0;
  19. let playerIndex = 0;
  20. let messageIndex = 0;
  21.  
  22. //////// FUNCIONS ////////
  23. function prefillAmount(inputEl) {
  24. const maxAmount = inputEl.dataset.max;
  25.  
  26. inputEl.value = +prefillAmountVal[amountIndex] > +maxAmount ? +maxAmount : +prefillAmountVal[amountIndex];
  27. console.log('input', inputEl.value); // TEST
  28. inputEl.dispatchEvent(new Event('input', { bubbles: true }));
  29. }
  30. function prefillPlayer(inputEl) {
  31. inputEl.value = prefillPlayerVal[playerIndex];
  32. inputEl.dispatchEvent(new Event('input', { bubbles: true }));
  33. }
  34. function prefillMessage(inputEl, message) {
  35. console.log('message', message); // TEST
  36. if (message === undefined) {
  37. inputEl.value = prefillMessageVal[messageIndex];
  38. }
  39. if (message !== undefined) {
  40. inputEl.value = '';
  41. }
  42. inputEl.dispatchEvent(new Event('input', { bubbles: true }));
  43. }
  44.  
  45. function amountClickHandler(e) {
  46. const max = prefillAmountVal.length - 1;
  47. if (++amountIndex > max) amountIndex = 0;
  48.  
  49. prefillAmount(e.target);
  50. e.target.focus();
  51. e.target.select();
  52. }
  53. function playerClickHandler(e) {
  54. const max = prefillPlayerVal.length - 1;
  55. if (++playerIndex > max) playerIndex = 0;
  56.  
  57. prefillPlayer(e.target);
  58. e.target.focus();
  59. e.target.select();
  60. }
  61. function messageClickHandler(e) {
  62. const max = prefillMessageVal.length - 1;
  63. if (++messageIndex > max) messageIndex = 0;
  64.  
  65. prefillMessage(e.target);
  66. e.target.focus();
  67. e.target.select();
  68. }
  69. function removeMessageClickHandler(e, inputEl) {
  70. console.log('removeMessage'); // TEST
  71. prefillMessage(inputEl, '');
  72. }
  73.  
  74. async function sendItemObserverCallback(mutationList, observer) {
  75. for (const mutation of mutationList) {
  76. if (mutation.target.classList.contains('cont-wrap') && mutation.type !== 'attributes') {
  77. if (mutation.addedNodes.length > 0 && mutation.addedNodes[0].classList.length > 0 && mutation.addedNodes[0].classList.contains('send-act')) {
  78. const sendActionEl = mutation.addedNodes[0];
  79. const amountInputEl = sendActionEl.querySelector('input.amount[type="text"]');
  80. const playerInputEl = sendActionEl.querySelector('input.user-id');
  81.  
  82. prefillAmount(amountInputEl);
  83. prefillPlayer(playerInputEl);
  84.  
  85. amountInputEl.addEventListener('click', amountClickHandler);
  86. playerInputEl.addEventListener('click', playerClickHandler);
  87. }
  88. }
  89. if (mutation.target.classList.contains('msg-active')) {
  90. console.log(mutation); // TEST
  91. const messageContainerEl = mutation.target;
  92. const messageInput = messageContainerEl.querySelector('input.message');
  93. const removeMessageBtn = messageContainerEl.querySelector('.action-remove');
  94.  
  95. prefillMessage(messageInput);
  96. messageInput.addEventListener('click', messageClickHandler);
  97. removeMessageBtn.addEventListener('click', (e, messageInput) => removeMessageClickHandler(e, messageInput));
  98. }
  99. }
  100. }
  101.  
  102. function createSenditemMutationObserver() {
  103. const observer = new MutationObserver(sendItemObserverCallback);
  104. observer.observe(document, {
  105. attributes: true,
  106. childList: true,
  107. subtree: true,
  108. });
  109. }
  110.  
  111. // document.addEventListener('input', (e) => console.log(e)); // TEST)
  112.  
  113. (async () => {
  114. console.log('🎁 Prefill item send script is ON!'); // TEST
  115.  
  116. createSenditemMutationObserver();
  117. })();