Greasemonkey/VDex_Project_Pokemon_Tracker.user.js

213 lines
7.3 KiB
JavaScript
Raw Normal View History

2014-01-05 19:14:05 +01:00
// ==UserScript==
// @name VDex Project Pokemon Tracker
// @namespace http://www.enbewe.de/vdex/
// @description Lets you track pokemon
// @include http://vdexproject.net/poke.php*
// @include http://vdexproject.net/pokegear.php*
// @copyright eNBeWe
// @version 0.3
// ==/UserScript==
(function (){
var page = window.location.pathname
if (page == "/pokegear.php" || page == "/pokegear.php#info") {
// Fetch anchor
var anchor = document.evaluate('//table[@class="content"]/tbody',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// Create and place row for tracking plugin
var elmContentRow = document.createElement('tr');
anchor.appendChild(elmContentRow);
// Create and place cell with heading
var elmContentCell = document.createElement('td');
elmContentRow.appendChild(elmContentCell);
elmContentCell.setAttribute("id","trackingHeader");
elmContentCell.setAttribute("style","vertical-align: top;");
elmContentCell.setAttribute("colspan","2");
elmContentCell.innerHTML = '<b>Tracked Pokemon:</b><br>';
// Place link to load the tracked pokemon
var loadTrackedLink = document.createElement('small');
loadTrackedLink.setAttribute("id","trackerLinkSmall");
loadTrackedLink.innerHTML = '[ <span class="hlclickable">Load tracked Pokemon</span> ]';
loadTrackedLink.addEventListener("click", function(){loadTracked();}, true);
elmContentCell.appendChild(loadTrackedLink);
} else if (page == "/poke.php") {
// Get PokeID of active pokemon
var pokeID = getUrlParam('id');
// Fetch anchor
var anchor = document.getElementById('infopoke');
// Create and place link to toggle tracking
var elmTrackerLink = document.createElement('small');
elmTrackerLink.innerHTML = '[ <span class="hlclickable">Toggle pokemon tracking</span> ]';
elmTrackerLink.addEventListener("click", function(){toggleTracking(pokeID);}, true);
anchor.parentNode.insertBefore(elmTrackerLink, anchor.nextSibling);
}
// Load trcked pokemon
function loadTracked() {
// Fetch anchor
var trackedLink = document.getElementById('trackerLinkSmall');
var anchor = trackedLink.parentNode;
// Remove Link
anchor.removeChild(trackedLink);
// Get list of tracked pokemon
var pokelist = GM_getValue('track','');
if (pokelist == '' || pokelist == ' '){
// List is empty -> display message
var trackerMsg = document.createElement('small');
trackerMsg.innerHTML = 'No pokemon are being tracked';
anchor.appendChild(trackerMsg);
} else {
// Convert List to Array
var Pokemon = pokelist.split(' ');
// Prepare table
var trackedTable = document.createElement('table');
trackedTable.setAttribute('id','trackingTable');
anchor.appendChild(trackedTable);
// Load data for tracked pokemon
for (var i = 0; i < Pokemon.length; i++) {
GM_xmlhttpRequest({
method: "GET",
url: "http://vdexproject.net/poke.php?&id="+Pokemon[i],
onload: function(response){
addPokemonToView(response);
}
});
}
}
}
// Helper function to add a loaded Pokemon to the view
function addPokemonToView(response) {
// Fetch anchor
var anchor = document.getElementById('trackingTable');
var textToInsert;
var activeRow;
// Fetch pokemon id
var regex = /id=(\d*)/;
regex.exec(response.finalUrl);
var pokeid = RegExp.$1;
if (response.responseText.search(/does\snot\shave\san\sowner\scurrently/) == -1) {
// Pokemon has owner
// Parse response
var parser = new DOMParser();
var data = parser.parseFromString(response.responseText, "text/html");
// Store data for cell entry
var celldata = data.evaluate('//table[@id="infopoke"]/tbody/tr[1]',data, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
textToInsert = '<table><tr>'+celldata.innerHTML+'</tr></table><div style="text-align: center;"><small>( <a href="http://vdexproject.net/poke.php?&id='+pokeid+'">more info</a> )</small></div>';
} else {
textToInsert = '<div style="text-align: center;">Pokemon has currently no owner!</div>';
}
// Get number of existing cells
var existingCellsCount = document.evaluate('//table[@id="trackingTable"]/tr/td', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength;
// Determine side where to add the new cell
if (existingCellsCount % 2 == 1) {
activeRow = document.evaluate('//table[@id="trackingTable"]/tr[last()]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
} else {
activeRow = document.createElement('tr');
anchor.appendChild(activeRow);
}
// Create new cell
var elmNewCell = document.createElement('td');
elmNewCell.setAttribute('style','padding: 20px; border: 2px solid grey;');
elmNewCell.innerHTML = textToInsert;
// Add removal link
var elmRemovalLink = document.createElement('small');
elmRemovalLink.innerHTML = '[ <span class="hlclickable">Toggle pokemon tracking</span> ]';
elmRemovalLink.addEventListener("click", function(){toggleTracking(pokeid);}, true);
var elmRemovalDiv = document.createElement('div');
elmRemovalDiv.setAttribute('style','text-align: center;');
elmRemovalDiv.appendChild(elmRemovalLink);
elmNewCell.appendChild(elmRemovalDiv);
activeRow.appendChild(elmNewCell);
}
// Helper function to toggle tracking of pokemon
function toggleTracking(pokemonID) {
// Get list of tracked pokemon
var getter = GM_getValue('track','');
// If the list is empty set active pokemon as tracked
if (getter == '' || getter == ' ') {
GM_setValue('track', pokemonID);
alert('Tracking this pokemon');
return
}
// Parse list
var Pokemon = getter.split(' ');
// Check if pokemon is already tracked
if (contains(Pokemon, pokemonID)) {
// Remove pokemon from array
var Pokemon_neu = new Array();
for (var i = 0; i < Pokemon.length; i++) {
if (Pokemon[i] == pokemonID) Pokemon[i] = '';
}
// Strip double spaces
var trackString = Pokemon.join(' ');
trackString = trackString.replace(/\s\s/g,' ');
trackString = trackString.replace(/^\s/,'');
trackString = trackString.replace(/\s$/,'');
// Save new list
GM_setValue('track', trackString);
alert('Removed pokemon from tracking');
} else {
// Add pokemon to list
Pokemon.push(pokemonID);
var trackString = Pokemon.join(' ');
trackString = trackString.replace(/\s\s/g,' ');
trackString = trackString.replace(/^\s/,'');
trackString = trackString.replace(/\s$/,'');
// Save new list
GM_setValue('track', trackString);
alert('Tracking this pokemon');
}
}
// Helper function to check if an array contains a given element
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
// Helper function to parse url parameters
function getUrlParam(name) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if ( results == null ) {
return "";
} else {
return results[1];
}
}
})();