Advertisement
satiel7314

Untitled

May 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. (() => {
  2. const NI = getCookie('interface') === 'ni';
  3. // TODO: move this url to .env file or create whole account system
  4. const URI = 'webhook link';
  5.  
  6. function getCookie(name) {
  7. const value = '; ' + document.cookie;
  8. const parts = value.split(`; ${name}=`);
  9.  
  10. if (parts.length == 2) {
  11. return parts.pop().split(";").shift();
  12. }
  13. }
  14.  
  15. function myNick() {
  16. return NI ? Engine.hero.d.nick : hero.nick;
  17. }
  18.  
  19. function getMapProp(prop) {
  20. return NI ? Engine.map.d[prop] : map[prop];
  21. }
  22.  
  23. function checkNpc(npc) {
  24. if (npc.wt > 79) {
  25. const type = npc.wt < 100 ? 'heros' : getMapProp('mode') !== 5 ? 'tytan' : 'kolos';
  26. const color = type === 'heros' ? 3448267 : type === 'tytan' ? 12718623 : 8037675;
  27.  
  28. sendMessage({
  29. ...npc,
  30. color,
  31. imageURI: window.location.origin + (NI ? CFG.npath : '') + npc.icon,
  32. stringifiedType: type,
  33. });
  34. }
  35. }
  36.  
  37. function sendMessage(mob) {
  38. const message = {
  39. username: mob.nick,
  40. avatar_url: mob.imageURI,
  41. content: '@everyone',
  42. embeds: [{
  43. color: mob.color,
  44. title: `${myNick()} znalazł ${mob.stringifiedType}a`,
  45. thumbnail: {
  46. url: mob.imageURI,
  47. },
  48. fields: [{
  49. inline: true,
  50. name: new Date().toLocaleString(),
  51. value: `\`${mob.nick} ${mob.lvl} lvl\n${getMapProp('name')} (${mob.x}, ${mob.y})\``,
  52. }],
  53. }],
  54. };
  55.  
  56. fetch(URI, {
  57. method: 'POST',
  58. cache: 'no-cache',
  59. headers: { 'Content-Type': 'application/json' },
  60. body: JSON.stringify(message),
  61. })
  62. .catch((error) => {
  63. console.error(error);
  64.  
  65. log('Błąd przy wysyłaniu danych do webhooka PEC', 2);
  66. log(error, 2);
  67. });
  68. }
  69.  
  70. function start() {
  71. if (NI) {
  72. API.addCallbackToEvent('newNpc', (data) => {
  73. if (data && data.d) {
  74. checkNpc(data.d);
  75. }
  76. });
  77. } else {
  78. const oldNewNpc = newNpc;
  79.  
  80. newNpc = (npcs) => {
  81. oldNewNpc(npcs);
  82.  
  83. if (npcs) {
  84. for (const npc of Object.values(npcs)) {
  85. checkNpc(npc);
  86. }
  87. }
  88. };
  89. }
  90. }
  91.  
  92. const oldSend = XMLHttpRequest.prototype.send;
  93. let loaded = false;
  94. let started = false;
  95.  
  96. XMLHttpRequest.prototype.send = function () {
  97. const callback = this.onreadystatechange;
  98.  
  99. this.onreadystatechange = function () {
  100. if (4 === this.readyState) {
  101. if (this.responseURL.includes('margonem.pl/engine?t=init&initlvl=4')) {
  102. loaded = true;
  103. }
  104. }
  105.  
  106. if (callback !== null) {
  107. callback.apply(this, arguments);
  108. }
  109. };
  110.  
  111. oldSend.apply(this, arguments);
  112.  
  113. if (loaded && !started) {
  114. started = true;
  115.  
  116. start();
  117. }
  118. };
  119. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement