Advertisement
iradap

Herosy na disco

Sep 27th, 2019
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.21 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Heroes on Discord
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.0
  5. // @description Quickly send discord messages for heroes and titans
  6. // @author Kris Aphalon
  7. // @match http://*.margonem.pl/
  8. // @grant none
  9. // @run-at document-idle
  10. // ==/UserScript==
  11.  
  12. (function ()
  13. {
  14. let settings = {
  15. webhookUrl: "",
  16. name: ""
  17. }
  18.  
  19. function loadSettings()
  20. {
  21. const savedSettings = localStorage.getItem("heroes-discord")
  22. if (savedSettings)
  23. {
  24. settings = JSON.parse(savedSettings)
  25. }
  26. }
  27.  
  28. const css = '.heroes-discord__popup{border-image:url("http://tempest.margonem.pl/img/gui/tmp/window-frame.png") 32 20 repeat;border-style:solid;border-width:10px 5px;color:black;left:calc(50% - 150px);position:fixed;top:20px;width:300px;z-index:1000}.heroes-discord__background{background-image:url("http://tempest.margonem.pl/img/gui/content-redleather.jpg");height:auto}.heroes-discord__img{display:flex;justify-content:center;padding-top:10px}.heroes-discord__text{margin-bottom:5px;margin-top:10px;text-align:center}.heroes-discord__buttons-container{align-content:center;border-top:2px solid #3d291c;display:flex;height:37px;justify-content:space-evenly;margin:0 -2px;padding-top:3px}.heroes-discord__button{background-color:#000;background-image:linear-gradient(to top, #12210d, #396b29);border:solid 1px #0c0d0d;border-radius:6px;box-shadow:inset 0 0 1px 1px #cecece, inset 0 0 0 3px #0c0d0d;box-sizing:border-box;color:#e6d6bf;cursor:pointer;display:inline-block;height:32px;line-height:normal;padding:0;position:relative;width:100px}.heroes-discord__button:hover{background-image:linear-gradient(to top, #101010, #434343)}.heroes-discord__button:active{background-image:linear-gradient(to top, #58310b, #b26417)}.heroes-discord__button-positive{font-size:13px}.heroes-discord__button-negative{background-image:linear-gradient(to top, #310b0b, #831f1f);box-shadow:inset 0 0 1px 1px #cecece, inset 0 0 0 3px #0c0d0d;font-size:10px}.heroes-discord__setup{left:calc(50% - 150px);top:calc(50% - 150px)}.heroes-discord__setup .heroes-discord__background{box-sizing:border-box;height:253px;padding:14px}.heroes-discord__button-close{background:url("http://aldous.margonem.pl/img/gui/buttony.png?v=5") -263px -79px;border:1px solid gray;cursor:pointer;height:22px;position:absolute;right:-3px;top:-7px;width:22px}.heroes-discord__button-close:hover{background-position:-286px -79px}.heroes-discord__discord-icon{cursor:pointer;padding-right:4px;padding-top:5px;position:absolute;right:0;top:0;width:27px}.heroes-discord__label{display:block;height:60px;margin-top:16px;text-align:center}.heroes-discord__input{border:1px solid black;height:18px;margin-top:5px;width:80%}.heroes-discord__setup .heroes-discord__buttons-container{border-top:none;margin-top:25px}.heroes-discord__setup .heroes-discord__button-negative{font-size:13px}.bottom-panel-pointer-bg .heroes-discord__discord-icon{background-color:#2c2f33;cursor:url(../img/gui/cursor/5.png), url(../img/gui/cursor/5.cur), auto;font-size:13px;left:40px;padding:0;pointer-events:auto;top:25px}'
  29. const style = document.createElement("style")
  30. style.appendChild(document.createTextNode(css))
  31. document.head.appendChild(style)
  32.  
  33. function sendDiscordAlert(webhookUrl, playerNick, npc, mapName, serverName)
  34. {
  35. let color = 8388608 //red
  36. let type1 = "Tytan"
  37. let type2 = "tytana"
  38. if (isHeros(npc))
  39. {
  40. type1 = "Heros"
  41. type2 = "heroesa"
  42. color = 8388736 //purple
  43. }
  44. let addToThumbnail = ""
  45. if (getCookie("interface") === "ni")
  46. {
  47. addToThumbnail = "/obrazki/npc"
  48. }
  49. const request = new XMLHttpRequest()
  50. request.open("POST", webhookUrl, true)
  51. request.setRequestHeader("Content-Type", "application/json")
  52. request.send(JSON.stringify({
  53. content: `**@here ${type1}:** ${npc.nick} - ${mapName} (${npc.x}, ${npc.y})`,
  54. username: "Wysłannik zakonu",
  55. avatar_url: "http://margonem.pl/obrazki/npc/bur/zr_ithan.gif",
  56. embeds: [{
  57. color: color,
  58. title: `${playerNick} znalazł ${type2}!`,
  59. description: `${npc.nick} - ${mapName} (${npc.x}, ${npc.y}) - ${serverName}`,
  60. thumbnail: {
  61. url: `http://margonem.pl${addToThumbnail}${npc.icon}`
  62. },
  63. timestamp: new Date().toISOString()
  64. }]
  65. }))
  66. }
  67.  
  68. function isHeros(npc)
  69. {
  70. return npc.wt > 79 && npc.wt <= 99
  71. }
  72.  
  73. function closePopup()
  74. {
  75. document.body.removeChild(document.getElementById("heroes-popup"))
  76. }
  77.  
  78. function getServerName()
  79. {
  80. if (getCookie("interface") === "ni")
  81. {
  82. return Engine.worldName[0].toUpperCase() + Engine.worldName.slice(1)
  83. }
  84. else
  85. {
  86. return g.worldname[0].toUpperCase() + g.worldname.slice(1)
  87. }
  88.  
  89. }
  90.  
  91.  
  92. function displayPopup(nick, npc, map)
  93. {
  94. let type = "Tytan"
  95. if (isHeros(npc))
  96. {
  97. type = "Heros"
  98. }
  99.  
  100. const popup = document.createElement("div")
  101. popup.id = "heroes-popup"
  102. popup.className = "heroes-discord__popup"
  103.  
  104. const popupBackground = document.createElement("div")
  105. popupBackground.className = "heroes-discord__background"
  106.  
  107. const imgContainer = document.createElement("div")
  108. imgContainer.className = "heroes-discord__img"
  109.  
  110. const img = document.createElement("img")
  111. img.src = "https://margonem.pl/obrazki/npc/" + npc.icon
  112.  
  113. imgContainer.appendChild(img)
  114.  
  115. popupBackground.appendChild(imgContainer)
  116.  
  117. const textContainer = document.createElement("div")
  118. textContainer.className = "heroes-discord__text"
  119.  
  120. const info = document.createElement("span")
  121. info.innerText = `${type} ${npc.nick} na mapie ${map.name} i kordach (${npc.x},${npc.y})`
  122. textContainer.appendChild(info)
  123. textContainer.appendChild(document.createElement("br"))
  124. const question = document.createElement("span")
  125. question.innerText = "Powiadomić discorda?"
  126. textContainer.appendChild(question)
  127.  
  128. popupBackground.appendChild(textContainer)
  129.  
  130. const buttonContainer = document.createElement("div")
  131. buttonContainer.className = "heroes-discord__buttons-container"
  132.  
  133. const yesButton = document.createElement("button")
  134. yesButton.className = "heroes-discord__button heroes-discord__button-positive"
  135. yesButton.innerText = "Powiadom"
  136. yesButton.addEventListener("click", function ()
  137. {
  138. if (!settings.webhookUrl)
  139. {
  140. message("Nie skonfigurowałeś dodatku! Ustawienia są w prawym górnym rogu konfiguracji.")
  141. }
  142. else
  143. {
  144. let name = nick
  145. if (settings.name)
  146. {
  147. name = nick
  148. }
  149. sendDiscordAlert(settings.webhookUrl, name, npc, map.name, getServerName())
  150. closePopup()
  151. }
  152.  
  153. })
  154. buttonContainer.appendChild(yesButton)
  155.  
  156. const noButton = document.createElement("button")
  157. noButton.className = "heroes-discord__button heroes-discord__button-negative"
  158. noButton.innerText = "Nie powiadamiaj"
  159. noButton.addEventListener("click", closePopup)
  160. buttonContainer.appendChild(noButton)
  161.  
  162. popupBackground.appendChild(buttonContainer)
  163.  
  164. popup.appendChild(popupBackground)
  165. document.body.appendChild(popup)
  166. }
  167.  
  168. const webhookInput = document.createElement("input")
  169. const nameInput = document.createElement("input")
  170.  
  171. function saveSettings()
  172. {
  173. settings.webhookUrl = webhookInput.value
  174. settings.name = nameInput.value
  175.  
  176. localStorage.setItem("heroes-discord", JSON.stringify(settings))
  177. }
  178.  
  179. function closeSetup()
  180. {
  181. const setup = document.getElementsByClassName("heroes-discord__setup")
  182. if (setup.length > 0)
  183. {
  184. document.body.removeChild(setup[0])
  185. }
  186. }
  187.  
  188. function showSetup()
  189. {
  190. if (document.getElementsByClassName("heroes-discord__setup").length === 0)
  191. {
  192. const popup = document.createElement("div")
  193. popup.className = "heroes-discord__popup heroes-discord__setup"
  194.  
  195. const close = document.createElement("div")
  196. close.className = "heroes-discord__button-close"
  197. close.addEventListener("click", closeSetup)
  198. popup.appendChild(close)
  199.  
  200. const popupBackground = document.createElement("div")
  201. popupBackground.className = "heroes-discord__background "
  202.  
  203. const webhookLabel = document.createElement("label")
  204. webhookLabel.className = "heroes-discord__label"
  205. webhookLabel.innerText = "Link do webhooka (poproś o niego zarządcę serwera discord)"
  206. popupBackground.appendChild(webhookLabel)
  207.  
  208. webhookInput.type = "text"
  209. webhookInput.className = "heroes-discord__input"
  210. webhookInput.value = settings.webhookUrl
  211. webhookLabel.appendChild(webhookInput)
  212.  
  213. const nameLabel = document.createElement("label")
  214. nameLabel.className = "heroes-discord__label"
  215. nameLabel.innerText = "Nick jaki chcesz by się wyświetlał (zostaw puste jeżeli ma to być nick aktualnej postaci)"
  216. popupBackground.appendChild(nameLabel)
  217.  
  218. nameInput.type = "text"
  219. nameInput.className = "heroes-discord__input"
  220. nameInput.value = settings.name
  221. if (getCookie("interface") === "ni")
  222. {
  223. nameInput.placeholder = Engine.hero.nick
  224. }
  225. else
  226. {
  227. nameInput.placeholder = hero.nick
  228. }
  229. nameLabel.appendChild(nameInput)
  230.  
  231.  
  232. const buttonContainer = document.createElement("div")
  233. buttonContainer.className = "heroes-discord__buttons-container"
  234.  
  235. const yesButton = document.createElement("button")
  236. yesButton.className = "heroes-discord__button heroes-discord__button-positive"
  237. yesButton.innerText = "Zapisz"
  238. yesButton.addEventListener("click", saveSettings)
  239. yesButton.addEventListener("click", closeSetup)
  240. yesButton.addEventListener("click", function ()
  241. {
  242. message("Zapisano! Nie musisz nic odświeżać.")
  243. })
  244. buttonContainer.appendChild(yesButton)
  245.  
  246. const noButton = document.createElement("button")
  247. noButton.className = "heroes-discord__button heroes-discord__button-negative"
  248. noButton.innerText = "Anuluj"
  249. noButton.addEventListener("click", closeSetup)
  250. buttonContainer.appendChild(noButton)
  251.  
  252. popupBackground.appendChild(buttonContainer)
  253.  
  254. popup.appendChild(popupBackground)
  255. document.body.appendChild(popup)
  256. }
  257. }
  258.  
  259. function getCookie(cookieName)
  260. {
  261. let c_value = " " + document.cookie
  262. let c_start = c_value.indexOf(" " + cookieName + "=")
  263. if (c_start === -1)
  264. {
  265. c_value = null
  266. }
  267. else
  268. {
  269. c_start = c_value.indexOf("=", c_start) + 1
  270. let c_end = c_value.indexOf(";", c_start)
  271. if (c_end === -1)
  272. {
  273. c_end = c_value.length
  274. }
  275. c_value = unescape(c_value.substring(c_start, c_end))
  276. }
  277. return c_value
  278.  
  279. }
  280.  
  281. loadSettings()
  282. if (getCookie("interface") === "ni")
  283. {
  284.  
  285. window.API.addCallbackToEvent("newNpc", function (npc)
  286. {
  287. if (npc.d.wt > 79)
  288. {
  289. displayPopup(window.Engine.hero.nick, npc.d, window.Engine.map.d)
  290. }
  291. })
  292.  
  293. const discordIcon = document.createElement("img")
  294. discordIcon.src = "https://i.imgur.com/i2QLF9l.png"
  295. discordIcon.alt = "Discord Icon"
  296. discordIcon.className = "heroes-discord__discord-icon"
  297. discordIcon.addEventListener("click", showSetup)
  298. document.getElementsByClassName("bottom-panel-pointer-bg")[0].appendChild(discordIcon)
  299. }
  300. else
  301. {
  302. const oldNewNpc = window.newNpc
  303. window.newNpc = function (dane)
  304. {
  305. for (const id in dane)
  306. {
  307. if (dane.hasOwnProperty(id) && dane[id].wt > 79)
  308. {
  309. displayPopup(window.hero.nick, dane[id], window.map)
  310. }
  311. }
  312. oldNewNpc(dane)
  313. }
  314.  
  315.  
  316. const discordIcon = document.createElement("img")
  317. discordIcon.src = "https://i.imgur.com/i2QLF9l.png"
  318. discordIcon.alt = "Discord Icon"
  319. discordIcon.className = "heroes-discord__discord-icon"
  320. discordIcon.addEventListener("click", showSetup)
  321. document.getElementById("config").appendChild(discordIcon)
  322. }
  323. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement