Attack links on enemy list

Adds attack links on enemy list

  1. // ==UserScript==
  2. // @name Attack links on enemy list
  3. // @namespace https://gitgud.com/stephenlynx
  4. // @version 1.2.2
  5. // @description Adds attack links on enemy list
  6. // @author Stephen Lynx
  7. // @license MIT
  8. // @match https://www.torn.com/page.php?sid=list&type=enemies
  9. // @match https://www.torn.com/page.php?sid=UserList*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=torn.com
  11. // @run-at document-body
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. var toBlock = [ '72', '77', '71', '15', '16', '70', '82' ];
  19.  
  20. function checkIcons(base) {
  21.  
  22. var iconArray = Array.from(base.getElementsByClassName('iconShow'));
  23.  
  24. for (var i = 0; i < iconArray.length; i++) {
  25.  
  26. var icon = iconArray[i];
  27.  
  28. if (toBlock.indexOf(icon.id.substr(4, 2)) > -1) {
  29. return false;
  30. }
  31.  
  32. }
  33.  
  34. return true;
  35.  
  36. }
  37.  
  38. function processPlayerElement(link){
  39.  
  40. var destination = link.href;
  41.  
  42. console.log(destination);
  43. var id = destination
  44. .match(/\/profiles\.php\?XID=(\d*)/)[1];
  45.  
  46. var attackLink = document.createElement('a');
  47. var oldOnClick = attackLink.onclick;
  48. attackLink.onclick = function(event) {
  49. event.stopPropagation();
  50. oldOnClick();
  51. }
  52. attackLink.innerHTML = 'Attack';
  53. attackLink.style['font-size'] = '12px';
  54. attackLink.target = '_blank';
  55. attackLink.href = '/loader.php?sid=attack&user2ID='
  56. + id;
  57. link.after(attackLink);
  58.  
  59. }
  60.  
  61. var observer = new MutationObserver(
  62. function(mutationList, observer) {
  63. mutationList
  64. .forEach(function(event) {
  65.  
  66.  
  67. for (var added of event.addedNodes) {
  68.  
  69. if (!added.querySelectorAll) {
  70. continue;
  71. }
  72.  
  73. //enemy list
  74. for (var element of added.querySelectorAll('[class^=\'honorName___\']')) {
  75. processPlayerElement(element.parentElement);
  76. }
  77.  
  78. //player search
  79. for (element of added.getElementsByClassName('user name')) {
  80.  
  81. var parent = element.parentElement.parentElement;
  82.  
  83. if(!checkIcons(parent)){
  84. parent.remove();
  85. }else {
  86. processPlayerElement(element);
  87. }
  88.  
  89.  
  90. }
  91.  
  92.  
  93. }
  94.  
  95. });
  96.  
  97. });
  98.  
  99. observer.observe(document.getElementsByTagName('body')[0], {
  100. childList : true,
  101. subtree : true
  102. });
  103.  
  104. })();