Replace "Available" and Make Attack-Links Clickable

Replaces "available" with attack links and makes them clickable, skipping anonymous sellers.

  1. // ==UserScript==
  2. // @name Replace "Available" and Make Attack-Links Clickable
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Replaces "available" with attack links and makes them clickable, skipping anonymous sellers.
  6. // @author Grance [3487987]
  7. // @match *://www.torn.com/page.php?sid=ItemMarket*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // Function to replace "available" with attack links based on extracted IDs
  16. function replaceAvailableWithAttackLinks() {
  17. const sellerList = document.querySelector('ul.sellerList___kgAh_'); // Get the <ul> with class 'sellerList___kgAh_'
  18.  
  19. if (sellerList) {
  20. const sellers = sellerList.querySelectorAll('li.rowWrapper___me3Ox'); // Select all listing rows
  21.  
  22. sellers.forEach(row => {
  23. const anonymousDiv = row.querySelector('div.anonymous___P3S5S');
  24. if (anonymousDiv) {
  25. // Skip if the seller is anonymous
  26. return;
  27. }
  28.  
  29. const profileLink = row.querySelector('a[href^="/profiles.php?XID="]');
  30. if (profileLink) {
  31. const url = new URL(profileLink.href, 'https://www.torn.com'); // Handle relative URLs
  32. const profileID = url.searchParams.get('XID'); // Extract the XID parameter from the href
  33.  
  34. const availableDiv = row.querySelector('div.available___xegv_');
  35. if (availableDiv && availableDiv.textContent.includes('available')) {
  36. // Extract the quantity from the text (e.g., "16 available")
  37. const quantity = availableDiv.textContent.split(' ')[0]; // Get the first part (e.g., "16")
  38. availableDiv.textContent = `https://www.torn.com/loader.php?sid=attack&user2ID=${profileID} (${quantity})`;
  39. }
  40. }
  41. });
  42. }
  43. }
  44.  
  45. // Function to convert div into a clickable link
  46. function convertDivToLink() {
  47. const divs = document.querySelectorAll('div.available___xegv_');
  48.  
  49. divs.forEach((div) => {
  50. if (div.textContent.includes('https://') && !div.dataset.linkConverted) {
  51. const urlMatch = div.textContent.match(/https:\/\/www\.torn\.com\/loader\.php\?sid=attack&user2ID=\d+/);
  52. if (urlMatch) {
  53. const url = urlMatch[0];
  54. const text = div.textContent.replace(url, '').trim();
  55.  
  56. const link = document.createElement('a');
  57. link.href = url;
  58. link.textContent = `${text} Attack`;
  59. link.style.color = 'inherit';
  60. link.style.textDecoration = 'none';
  61. link.target = '_blank';
  62.  
  63. div.innerHTML = '';
  64. div.appendChild(link);
  65. div.dataset.linkConverted = 'true';
  66. }
  67. }
  68. });
  69. }
  70.  
  71. // Run the replacement function after the page has fully loaded
  72. window.addEventListener('load', () => {
  73. replaceAvailableWithAttackLinks();
  74. convertDivToLink();
  75. });
  76.  
  77. // Observe for dynamic updates and replace again
  78. const observer = new MutationObserver(() => {
  79. replaceAvailableWithAttackLinks();
  80. convertDivToLink();
  81. });
  82.  
  83. observer.observe(document.body, { childList: true, subtree: true });
  84. })();