Advertisement
Takezo

SP2 Wiki Bot from Kongregate chat

Dec 19th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        SP2 Wiki Bot from Kongregate chat
  3. // @description Adds a small input box in the chat to request items definition from the Swords & Potions 2 wiki
  4. // @namespace   *
  5. // @include     http://www.kongregate.com/games/EdgebeeStudios/swords-and-potions-2*
  6. // @version     1
  7. // @grant       GM_xmlhttpRequest
  8. // @grant       GM_log
  9. // @grant       GM_addStyle
  10. // ==/UserScript==
  11.  
  12. function waitForChat() {
  13.     canary = document.querySelectorAll("div.chat_tabpane.users_in_room.clear")
  14.     if (canary.length > 0) {
  15.         above = document.querySelector("div#chat_window")
  16.         GM_log("going in")
  17.         initInput()
  18.         GM_addStyle("div.chat_message_window {height:458px !important}")
  19.        
  20.     } else {
  21.         GM_log("wait")
  22.         setTimeout(waitForChat, 1000)
  23.     }
  24. }
  25.  
  26. waitForChat()
  27.  
  28. function addMessage(who, msg) {
  29.     spanUsername = document.createElement("span")
  30.     spanUsername.setAttribute("class", "username chat_message_window_username")
  31.    
  32.     spanUsername.setAttribute("username", who)
  33.     spanUsername.textContent = who
  34.    
  35.     spanSeparator = document.createElement("span")
  36.     spanSeparator.setAttribute("class", "separator")
  37.     spanSeparator.textContent = ": "
  38.  
  39.     spanMessage = document.createElement("span")
  40.     spanMessage.setAttribute("class", "message hyphenate")
  41.     spanMessage.setAttribute("id", "sp2wikibot_message")
  42.     GM_addStyle("#sp2wikibot_message {font-style:italic !important}")
  43.     spanMessage.textContent = msg
  44.  
  45.     spanClear = document.createElement("span")
  46.     spanClear.setAttribute("class", "clear")
  47.  
  48.     p = document.createElement("p")
  49.     p.appendChild(spanUsername)
  50.     p.appendChild(spanSeparator)
  51.     p.appendChild(spanMessage)
  52.     p.appendChild(spanClear)
  53.  
  54.     divChat = document.createElement("div")
  55.     divChat.setAttribute("id", "sp2wikibot_divmessage")
  56.     GM_addStyle("#sp2wikibot_divmessage {background-color:#FAEBD7}")  // antiquewhite (maybe try blanchedalmond)
  57.     divChat.appendChild(p)
  58.    
  59.     messageWindow = document.querySelector("div#chat_rooms_container > div.chat_room_template > div.chat_message_window")
  60.     if (messageWindow.lastChild != null) {
  61.         messageWindow = messageWindow.lastChild
  62.     }
  63.    
  64.     messageWindow.appendChild(divChat)
  65. }
  66.  
  67. function initInput() {
  68.     textArea = document.createElement("textarea")
  69.     textArea.setAttribute("class", "chat_input prompt_text")
  70.     textArea.setAttribute("id", "sp2wikibot_textarea")
  71.     GM_addStyle("#sp2wikibot_textarea {height:17px !important; margin-top:10px !important}")
  72.     textArea.textContent = "Enter item:<asked item>"
  73.  
  74.     divInput = document.createElement("div")
  75.     divInput.setAttribute("class", "chat_controls")
  76.     divInput.setAttribute("id", "sp2wikibot_divinput")
  77.     GM_addStyle("#sp2wikibot_divinput {height:22px !important}")
  78.     divInput.appendChild(textArea)
  79.    
  80.     divMain = document.createElement("div")
  81.     divMain.setAttribute("id", "sp2wikibot")
  82.     divMain.appendChild(divInput)
  83.    
  84.     above.appendChild(divMain)
  85.    
  86.     textArea.addEventListener("keyup", function(event) {parseRequest(event);}, false)
  87.     textArea.addEventListener("click", clickTextareaTheFirstTime, false)
  88. }
  89.  
  90. function clickTextareaTheFirstTime() {
  91.     textArea.value=""
  92.     textArea.setAttribute("class", "chat_input")
  93.     textArea.removeEventListener("click", clickTextareaTheFirstTime, false)
  94. }
  95.  
  96. function parseRequest(event) {
  97.     if (event.keyCode == 38 && typeof(lastRequest) !== "undefined") { // up arrow
  98.         textArea.value = lastRequest
  99.         return
  100.     }
  101.  
  102.     if (event.keyCode != 13) { // enter
  103.         return
  104.     }
  105.    
  106.     msg = textArea.value.replace(/\n/, "")  // remove line breaks
  107.     addMessage("you", msg)
  108.     textArea.value = ""
  109.     lastRequest = msg
  110.    
  111.     msgParts = msg.split(":")
  112.    
  113.     if (msgParts[0].trim() == "item") {
  114.         searchWiki(msgParts[1])
  115.     } else {
  116.         addMessage("bot", "cannot understand the request")
  117.     }
  118. }
  119.  
  120. // each word gets one and only one upper case letter: the first one
  121. function toTitleCase(str)
  122. {
  123.     return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  124. }
  125.  
  126. //http://www.edgebee.com/wiki/index.php?title=Fire_dagger_Recipe
  127. // =if(trim(G9)="","",hyperlink(CONCATENATE("http://www.edgebee.com/wiki/index.php?title=",substitute(G9," ","_"),"_Recipe"),"W"))
  128. function searchWiki(item) {
  129.     cleanItemName = toTitleCase(item.trim().replace(/ +/g, " "))
  130.     url = "http://www.edgebee.com/wiki/index.php?title="+cleanItemName.replace(/ /, "_")+"_Recipe"
  131.     //GM_log(url)
  132.     GM_xmlhttpRequest({
  133.         method: "GET",
  134.         url: url,
  135.         onload: function( response ) {
  136.             if ( response.status == 200 ) {
  137.                 var parser = new DOMParser();
  138.                 var responseDoc = parser.parseFromString(response.responseText, "text/html");
  139.                 rows = responseDoc.querySelectorAll("table[cellspacing='1'] > tbody > tr")
  140.  
  141.                 nameLevel = rows[0].textContent.trim()
  142.                 worker = rows[1].querySelector("td+td").textContent.trim()
  143.                 workstation = rows[2].querySelector("td+td").textContent.trim()
  144.                 price = rows[3].querySelector("td+td").textContent.trim()
  145.                 resources = "Needs: "+rows[6].querySelector("td+td").textContent.trim()
  146.                 speed = "Speed: "+rows[7].querySelector("td+td").textContent.trim()
  147.                 unlocks = "Unlocks: "+rows[9].querySelector("td+td").textContent.trim()
  148.                 unlockedBy = "Unlocked by "+rows[10].querySelector("td+td").textContent.trim()
  149.                 recipes = "Needed for Recipes: "+rows[11].querySelector("td+td").textContent.trim()
  150.                 quests = "Needed for Quests: "+rows[12].querySelector("td+td").textContent.trim()
  151.                 improvements = "Needed for Improvements: "+rows[13].querySelector("td+td").textContent.trim()
  152.                
  153.                 addMessage("sp2wikiBot",
  154.                            nameLevel+" • "+
  155.                            worker+" • "+
  156.                            workstation+" • "+
  157.                            price+" • "+
  158.                            resources+" • "+
  159.                            speed+" • "+
  160.                            unlocks + " • "+
  161.                            unlockedBy + " • "+
  162.                            recipes + " • "+
  163.                            quests + " • "+
  164.                            improvements)
  165.             } else {
  166.                 addMessage("sp2wikiBot", "item not found")
  167.             }
  168.         }
  169.     });
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement