// ==UserScript== // @name VDex Project Ajax capture // @namespace Http://www.enbewe.de/vdex // @description Tries to capture Ajax events // @include http://vdexproject.net/dungeon.php?mode=navigate* // @version 0.1 // ==/UserScript== // Control flags var maincontrol = false; var travel = false; var take_skills = false; var autobattle = false; // Status variables var mode = 'unknown'; var room = ''; var lastDirection = 'east'; // Create control window // TODO: create option to minimize var controlWindow = document.createElement('div'); controlWindow.setAttribute('style','width:180px; height:180px; position: fixed; top: 10px; left: 10px; background: #232323; border: 3px solid grey; padding: 10px;'); document.getElementById('login').parentNode.appendChild(controlWindow); refreshControlWindow(); function handleSend(send) { var rege; rege = /mode=changeroom&direction=(\w+)/; if (rege.test(send)) { rege.exec(send); mode = 'roomchange'; // alert('Sending roomchange to '+RegExp.$1); return 1; } rege = /mode=take&what=(\w+)/; if (rege.test(send)) { // alert('Taking skill disk'); return 1; } rege = /mode=enterbattle/; if (rege.test(send)) { mode = 'battle'; // alert('Entering battle'); return 1; } rege = /mode=battle/; if (rege.test(send)) { mode = 'battle'; // alert('Fighting'); return 1; } mode = 'unknown' return -1; } function handleResponse(response) { if (maincontrol) { var rege; room = document.getElementById("thisroom"); var roomname = document.evaluate('//div[@id="thisroom"]/b[1]',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML; // alert(roomname); if (take_skills) { rege = /There\sis\sa\sSKILL\sDISC/; if (rege.test(room.innerHTML)) { unsafeWindow.take('skill'); return 1; } } if (autobattle) { if (mode == 'battle') { var btlog = document.evaluate('//td[@class="btlog"]',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML; rege = /defeated/ if (rege.test(btlog.toLowerCase())) { mode = 'battledone'; unsafeWindow.endbattle(); setTimeout(function() { handleResponse(response); }, 500+(Math.floor(Math.random()*800))); return 1; } else { mode = 'battle'; unsafeWindow.battle('ATTACK'); return 1; } alert(btlog); } else { var playdata = document.getElementById('yourdata').innerHTML; var playskills = document.getElementById('yourskills').innerHTML; rege = /HP\s(\d+)\/(\d+)/ rege.exec(playdata); var needHeal = (RegExp.$1 <= (RegExp.$2 / 3)) rege = /prayer/; var hasPrayer = rege.test(playskills.toLowerCase()); rege = /rest/; var hasRest = rege.test(playskills.toLowerCase()); if (needHeal && hasPrayer) { unsafeWindow.rest(true); } else if (needHeal && hasRest) { unsafeWindow.rest(false); } else { rege = /There\sare\sENEMIES\sroaming\sthe\sroom/; if (rege.test(room.innerHTML)) { var enemy = document.evaluate('//span[starts-with(@id,"enemyicon")]',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; var enemyid = enemy.getAttribute('id'); rege = /enemyicon\d+(\w)/; rege.exec(enemyid); mode = 'battle'; unsafeWindow.enterbattle(RegExp.$1); return 1; } } } } if (travel && roomname != 'FINAL ROOM') { if (document.getElementById('north') != null) { unsafeWindow.go('north'); return 1; } if (document.getElementById(lastDirection) != null) { unsafeWindow.go(lastDirection); return 1; } if (lastDirection == 'east') { lastDirection = 'west'; } else { lastDirection = 'east'; } if (document.getElementById(lastDirection) != null) { unsafeWindow.go(lastDirection); return 1; } } } else { return -1; } return 1; } //Setting up ajax capture var OldXHR = unsafeWindow.XMLHttpRequest; var NewXHR = function() { var self = this; var actualXHR = new OldXHR(); this.onopen = function(a,b,c,d,e) { // alert('Open:\n'+a+'\n'+b+'\n'+c+'\n'+d+'\n'+e); } this.onsend = function(a) { if (handleSend(a) != 1) { // alert('Send:\n'+a); } } this.ononreadystatechange = function() { if (self.readyState == 4) { setTimeout(function() { if (handleResponse(self.responseText) != 1) { // alert('onreadystatechange:\n'+self.responseText+'\n'+self.statusText+'\n'+self.responseXML); } }, 500+(Math.floor(Math.random()*800))); } } this.requestHeaders = ""; this.requestBody = ""; // emulate methods from regular XMLHttpRequest object this.open = function(a, b, c, d, e) { self.openMethod = a.toUpperCase(); self.openURL = b; if (self.onopen != null && typeof(self.onopen) == "function") { self.onopen(a,b,c,d,e); } return actualXHR.open(a,b,c,d,e); } this.send = function(a) { if (self.onsend != null && typeof(this.onsend) == "function") { self.onsend(a); } self.requestBody += a; return actualXHR.send(a); } this.setRequestHeader = function(a, b) { if (self.onsetrequestheader != null && typeof(self.onsetrequestheader) == "function") { self.onsetrequestheader(a, b); } self.requestHeaders += a + ":" + b + "\r\n"; return actualXHR.setRequestHeader(a, b); } this.getRequestHeader = function() { return actualXHR.getRequestHeader(); } this.getResponseHeader = function(a) { return actualXHR.getResponseHeader(a); } this.getAllResponseHeaders = function() { return actualXHR.getAllResponseHeaders(); } this.abort = function() { return actualXHR.abort(); } this.addEventListener = function(a, b, c) { alert("not implemented yet"); } this.dispatchEvent = function(e) { alert("not implemented yet"); } this.openRequest = function(a, b, c, d, e) { alert("not implemented yet"); } this.overrideMimeType = function(e) { alert("not implemented yet"); } this.removeEventListener = function(a, b, c) { alert("not implemented yet"); } // emulate callbacks from regular XMLHttpRequest object actualXHR.onreadystatechange = function() { // copy properties back from the actual XHR to the wrapper self.readyState = actualXHR.readyState; try { self.status = actualXHR.status; } catch (e) {} try { self.responseText = actualXHR.responseText; } catch (e) {} try { self.statusText = actualXHR.statusText; } catch (e) {} try { self.responseXML = actualXHR.responseXML; } catch (e) {} try { if (self.ononreadystatechange != null && typeof(self.ononreadystatechange) == "function") { self.ononreadystatechange(); } } catch (e) {} // onreadystatechange callback if (self.onreadystatechange != null && typeof(self.onreadystatechange) == "function") { return self.onreadystatechange(); } } actualXHR.onerror = function(e) { if (self.onerror != null) { return self.onerror(e); } } actualXHR.onload = function(e) { if (self.onload != null) { return self.onload(e); } } actualXHR.onprogress = function(e) { if (self.onprogress != null) { return self.onprogress(e); } } } unsafeWindow.XMLHttpRequest = NewXHR; // Control window contents function refreshControlWindow() { controlWindow.innerHTML = '
Dungeon Crawler

'; // Loot skills var controlWindow_skills = document.createElement('div'); controlWindow_skills.setAttribute('id','controlWindow_skills'); controlWindow_skills.setAttribute('style','text-align: center;'); controlWindow_skills.innerHTML = 'Auto take skills: '; if (take_skills) { controlWindow_skills.innerHTML += 'active'; } else { controlWindow_skills.innerHTML += 'inactive'; } controlWindow_skills.innerHTML += ''; controlWindow_skills.addEventListener("click", function() { take_skills = !take_skills; refreshControlWindow();}, true); controlWindow.appendChild(controlWindow_skills); // Fight var controlWindow_battle = document.createElement('div'); controlWindow_battle.setAttribute('id','controlWindow_battle'); controlWindow_battle.setAttribute('style','text-align: center;'); controlWindow_battle.innerHTML = 'Auto battle: '; if (autobattle) { controlWindow_battle.innerHTML += 'active'; } else { controlWindow_battle.innerHTML += 'inactive'; } controlWindow_battle.innerHTML += ''; controlWindow_battle.addEventListener("click", function() { autobattle = !autobattle; refreshControlWindow();}, true); controlWindow.appendChild(controlWindow_battle); // Travel var controlWindow_travel = document.createElement('div'); controlWindow_travel.setAttribute('id','controlWindow_travel'); controlWindow_travel.setAttribute('style','text-align: center;'); controlWindow_travel.innerHTML = 'Auto travel: '; if (travel) { controlWindow_travel.innerHTML += 'active'; } else { controlWindow_travel.innerHTML += 'inactive'; } controlWindow_travel.innerHTML += ''; controlWindow_travel.addEventListener("click", function() { travel = !travel; refreshControlWindow();}, true); controlWindow.appendChild(controlWindow_travel); // Activate and deactivate var controlWindow_main = document.createElement('div'); controlWindow_main.setAttribute('id','controlWindow_main'); controlWindow_main.setAttribute('style','text-align: center;'); controlWindow_main.innerHTML = '

Main control:
'; if (maincontrol) { controlWindow_main.innerHTML += 'active'; } else { controlWindow_main.innerHTML += 'inactive'; } controlWindow_main.innerHTML += '
'; controlWindow_main.addEventListener("click", function() { maincontrol = !maincontrol; refreshControlWindow();}, true); controlWindow.appendChild(controlWindow_main); }