satiel7314

Untitled

Oct 2nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Webhook Notifications - PEC
  3. // @namespace PEC
  4. // @version 2.8.3
  5. // @author Hypercube
  6. // @description Sends a message to the Discord clan server when you find a hero+ mob.
  7. // @icon https://assets.xeyven.pl/logos/pec.png
  8. // @updateURL https://margonem.xeyven.pl/addons/pec/webhook-notifications.user.js
  9. // @downloadURL https://margonem.xeyven.pl/addons/pec/webhook-notifications.user.js
  10. // @match http://tempest.margonem.pl
  11. // @run-at document-end
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (() => {
  16. const NI = getCookie('interface') === 'ni';
  17. const URI =
  18. 'https://discordapp.com/api/webhooks/615243675802533888/sEFK6sC5G-C-n--TanKma6uP23DBHJJ5Lk9NK9A5nPGxk4mOJ62X8P39VJ6BW9-VW63X';
  19. let loaded = false;
  20. let started = false;
  21.  
  22. function getCookie(name) {
  23. const value = '; ' + document.cookie;
  24. const parts = value.split(`; ${name}=`);
  25.  
  26. if (parts.length == 2) {
  27. return parts.pop().split(';')[0];
  28. }
  29. }
  30.  
  31. function myNick() {
  32. return NI ? Engine.hero.d.nick : hero.nick;
  33. }
  34.  
  35. function getMapProp(prop) {
  36. return NI ? Engine.map.d[prop] : map[prop];
  37. }
  38.  
  39. function checkNpc(npc) {
  40. if (npc.wt > 79 && npc.lvl > 90) {
  41. const type =
  42. npc.wt < 100 ? 'heros' : getMapProp('mode') !== 5 ? 'tytan' : 'kolos';
  43. const color =
  44. type === 'heros' ? 3448267 : type === 'tytan' ? 12718623 : 8037675;
  45.  
  46. sendMessage({
  47. ...npc,
  48. color,
  49. imageURI: window.location.origin + (NI ? CFG.npath : '') + npc.icon,
  50. stringifiedType: type
  51. });
  52. }
  53. }
  54.  
  55. function sendMessage(mob) {
  56. const message = {
  57. username: mob.nick,
  58. avatar_url: mob.imageURI,
  59. content: '@everyone',
  60. embeds: [
  61. {
  62. color: mob.color,
  63. title: `${myNick()} znalazł ${mob.stringifiedType}a`,
  64. thumbnail: {
  65. url: mob.imageURI
  66. },
  67. fields: [
  68. {
  69. inline: true,
  70. name: new Date().toLocaleString(),
  71. value: `\`${mob.nick} ${mob.lvl} lvl\n${getMapProp('name')} (${
  72. mob.x
  73. }, ${mob.y})\``
  74. }
  75. ]
  76. }
  77. ]
  78. };
  79.  
  80. fetch(URI, {
  81. method: 'POST',
  82. cache: 'no-cache',
  83. headers: { 'Content-Type': 'application/json' },
  84. body: JSON.stringify(message)
  85. }).catch(error => {
  86. console.error(error);
  87.  
  88. log('Błąd przy wysyłaniu danych do webhooka PEC', 2);
  89. log(error, 2);
  90. });
  91. }
  92.  
  93. function start() {
  94. if (NI) {
  95. API.addCallbackToEvent('newNpc', data => {
  96. if (data && data.d) {
  97. checkNpc(data.d);
  98. }
  99. });
  100. } else {
  101. const oldNewNpc = newNpc;
  102.  
  103. newNpc = npcs => {
  104. oldNewNpc(npcs);
  105.  
  106. if (npcs) {
  107. for (const npc of Object.values(npcs)) {
  108. checkNpc(npc);
  109. }
  110. }
  111. };
  112. }
  113.  
  114. const npcs = NI ? Engine.npcs.check() : g.npc;
  115.  
  116. for (const npc of Object.values(npcs)) {
  117. checkNpc(npc);
  118. }
  119. }
  120.  
  121. const oldSend = XMLHttpRequest.prototype.send;
  122.  
  123. XMLHttpRequest.prototype.send = function() {
  124. const callback = this.onreadystatechange;
  125.  
  126. this.onreadystatechange = function() {
  127. if (4 === this.readyState) {
  128. if (this.responseURL.includes('margonem.pl/engine?t=init&initlvl=4')) {
  129. loaded = true;
  130. }
  131. }
  132.  
  133. if (callback !== null) {
  134. callback.apply(this, arguments);
  135. }
  136. };
  137.  
  138. oldSend.apply(this, arguments);
  139.  
  140. if (loaded && !started) {
  141. started = true;
  142.  
  143. start();
  144. }
  145. };
  146. })();
Add Comment
Please, Sign In to add comment