Lair Checker Adjusted 2.0 -- HTTPS+

Now with colours added by Alana!

  1. // ==UserScript==
  2. // @name Lair Checker Adjusted 2.0 -- HTTPS+
  3. // @description Now with colours added by Alana!
  4. // @author Tiff Zhang (derivative from bookmarklet at http://aywaslairchecker.mygamesonline.org/ ) -- with small modifications by Alana, #70812 on Aywas
  5. // @match https://www.aywas.com/*
  6. // @license MIT
  7. // @version 0.0.1.20240405182420
  8. // @namespace https://greasyfork.org/users/1284383
  9. // ==/UserScript==
  10. /* jshint -W097 */
  11. 'use strict';
  12. var alwaysHighlight = false;
  13.  
  14. // Lair Checker script taken from http://aywaslairchecker.mygamesonline.org/
  15. function lair_check() {
  16.  
  17. // <Species Name> <Genus Name> : <image url>
  18. var process = function() {
  19. var page = 1,
  20. petsTable = [],
  21. petImages = {},
  22. resultsTable = {},
  23. userID, version = "0.10",
  24. phoneHome = true,
  25. remoteVersion, versionCheckComplete = false,
  26. versionAnswer;
  27.  
  28. function versionCheck() {
  29. $.ajax("http://aywaslairchecker.net/bookmarklets/lite/version.js", {
  30. dataType: "text",
  31. cache: false,
  32. error: function() {
  33. versionCheckComplete = true;
  34. },
  35. success: function(text) {
  36. var data = $.parseJSON(text);
  37. remoteVersion = data.currentVersionNew;
  38. if (remoteVersion !== version) {
  39. var ref = "http://aywaslairchecker.net/bookmarklets/lite/lite-";
  40. if (phoneHome) ref = ref + "checkin-";
  41. ref = ref + "min.bm.js";
  42. $.ajax(ref, {
  43. dataType: "text",
  44. cache: false,
  45. error: function() {
  46. versionCheckComplete = true;
  47. },
  48. success: function(text) {
  49. versionAnswer = $('<div></div>');
  50. versionAnswer.append($('<p class="keep">This version is out of date. The current version is V' + remoteVersion + '</p>'));
  51. versionAnswer.append($('<p class="keep">' + data.updateMessage + '</p>'));
  52. var a = $('<a href="' + text + '">Aywas Lair Checker V' + remoteVersion + '</a>');
  53. versionAnswer.append(a);
  54. versionCheckComplete = true;
  55. }
  56. });
  57. } else {
  58. versionCheckComplete = true;
  59. }
  60. }
  61. });
  62. }
  63.  
  64. function generateOutput() {
  65. if (!versionCheckComplete) {
  66. window.setTimeout(generateOutput, 10000);
  67. return;
  68. }
  69. if (phoneHome) {
  70. $.get("http://aywaslairchecker.net/checkin.php", {
  71. id: userID,
  72. version: 0.6,
  73. lite: true
  74. });
  75. }
  76. var output = document.createElement('div'),
  77. k, ul, li, a, i;
  78. for (k in resultsTable) {
  79. if (resultsTable.hasOwnProperty(k)) {
  80. if (resultsTable[k].length > 1) {
  81. i = 0;
  82. ul = document.createElement('ul');
  83. $(ul).text(k);
  84. for (i = 0; i < resultsTable[k].length; i += 1) {
  85. li = document.createElement('li');
  86. a = document.createElement('a');
  87. $(a).text(resultsTable[k][i].name);
  88. $(a).attr("href", "https://www.aywas.com/pp/view/" + resultsTable[k][i].id + "/");
  89. $(li).append(a);
  90. $(ul).append(li);
  91. }
  92. }
  93. $(output).append(ul);
  94. }
  95. }
  96. $('title').text("Aywas Lair Checker - Results");
  97. if (versionAnswer) $('body').append(versionAnswer);
  98. $('body').append($(output));
  99. $('p:not(".keep")').hide().queue(function(next) {
  100. $(this).remove();
  101. next();
  102. });
  103. }
  104.  
  105. function addTable(element) {
  106. var breed, id, name;
  107. breed = $(element).find(".gen-small > a > strong").text().split(' the ');
  108. breed = breed[breed.length - 1];
  109. breed = breed.trim().split('(')[0].trim().replace(/^\s*\S*(Male|Female|Androgynous|Hermaphrodite|Undecided|Robot|Genderless|Agender|Bigender|Genderqueer|Neutrois|Pangender|Genderfluid|Non-Binary|Intersex|Other)/i, "").trim();
  110. id = $(element).find(".gen-small > a > strong").text().split('(')[1].match(/\d+/ig);
  111. if (id) {
  112. id = Number(id[0]);
  113. }
  114. name = $(element).find(".gen-small > a > strong").text();
  115. if (!breed.match(/Custom/ig)) {
  116. if (!resultsTable[breed]) {
  117. resultsTable[breed] = [];
  118. }
  119. resultsTable[breed].push({
  120. id: id,
  121. name: name
  122. });
  123. }
  124. }
  125.  
  126. function addTables() {
  127. var i;
  128. for (i = 0; i < petsTable.length; i += 1) {
  129. addTable(petsTable[i]);
  130. }
  131.  
  132. localStorage.setItem("petImages", JSON.stringify(petImages));
  133.  
  134. localStorage.setItem("petsList", JSON.stringify(resultsTable));
  135. generateOutput();
  136. }
  137.  
  138. function fetchPage() {
  139. $('body').append($("<p>Loading page " + page + ". Pets loaded so far: " + petsTable.length + "</p>"));
  140. $('title').text("Aywas Lair Checker: Loading page " + page + ". Pets loaded so far: " + petsTable.length);
  141. var listUrl = "https://www.aywas.com/lair/group/" + userID + "/all/?p=" + page + "&l=240",
  142. pageResult = [],
  143. i;
  144. $.ajax(listUrl, {
  145. dataType: "text",
  146. success: function(xml) {
  147. //xml = xml.replace(/<[^\/]*img[^>]*>([^<]*<[^\/\w]*\/img[^>]*>)*/gi, "");
  148. pageResult = $(xml).find('div#lair-sort-pets > div');
  149. for (i = 0; i < pageResult.length; i += 1) {
  150.  
  151. pageResult[i].innerHTML = pageResult[i].innerHTML.replace(/src\s*=\s*"https:\/\/www\.aywas\.com\/+images(\/+images)?\/pets\/([^"]+)"/gi, function (match, $i, identifier) {
  152. if (petImages[identifier]) {
  153. petImages[identifier]++;
  154. } else {
  155. petImages[identifier] = 1;
  156. }
  157. return "";
  158. });
  159. petsTable.push(pageResult[i]);
  160. }
  161. page = page + 1;
  162. if (pageResult.length) {
  163. //If your lair checker gets stuck and won't continue, increase the number below by 1000 and try again until it makes it all the way through!
  164. setTimeout(fetchPage,1000);
  165. } else {
  166. $('body').append($("<p>Load complete. Loaded " + petsTable.length + " pets.</p>"));
  167. addTables();
  168. }
  169. }
  170. });
  171. }
  172.  
  173. function getUserID() {
  174. userID = $("div#side > h3 > a");
  175. if (userID && userID.length) {
  176. userID = Number(userID.attr('href').match(/\d+/i)[0]);
  177. }
  178. }
  179. var scripts = document.getElementsByTagName('script');
  180. while (scripts.length) {
  181. scripts[0].parentElement.removeChild(scripts[0]);
  182. }
  183. scripts = document.getElementsByTagName('link');
  184. while (scripts.length) {
  185. scripts[0].parentElement.removeChild(scripts[0]);
  186. }
  187. var script = document.createElement('script');
  188. script.src = "//code.jquery.com/jquery-1.10.2.min.js";
  189. script.type = "text/javascript";
  190. script.onload = function() {
  191. getUserID();
  192. $('body').remove();
  193. $('html').append($('<body></body>'));
  194. $('body').append($('<h1>Aywas Lair Checker V' + version + '</h1>'));
  195. window.setTimeout(versionCheck, 1);
  196. fetchPage();
  197.  
  198. };
  199. document.getElementsByTagName('head')[0].appendChild(script);
  200. };
  201.  
  202. if (!(location.host.match(/.*aywas.*/ig) && location.host.match(/.*aywas.*/ig).length) && !(location.hostname.match(/.*aywas.*/ig) && location.hostname.match(/.*aywas.*/ig).length)) {
  203. alert("Please re-run this bookmarklet from Aywas.com!");
  204. location.href = "https://www.aywas.com/";
  205. } else {
  206. process();
  207. }
  208. };
  209.  
  210. $(document).ready(function() {
  211.  
  212. var map = [];
  213.  
  214. $("#side-mail").prepend('<p><a href="javascript:;" id="owned-pets-button" class="ctabtn normal"><span>Owned Pets</span></a></p>');
  215. $("#side-mail").prepend('<p><a href="#" id="lair-check-button" class="ctabtn normal"><span>Check Lair</span></a></p>');
  216. $("#lair-check-button").click(function() {
  217. lair_check();
  218. });
  219.  
  220. function highlightPets() {
  221. var petsList = JSON.parse(localStorage.getItem("petsList"));
  222. var petImages = JSON.parse(localStorage.getItem("petImages"));
  223.  
  224. var imageRegex = /https?:\/\/www\.aywas\.com\/+images(\/+images)?\/pets\/([^"]+)/i;
  225. $("#content img").each(function () {
  226. var res = $(this).attr('src').match(imageRegex);
  227. if (res) {
  228. var ident = res[2];
  229. if (petImages[ident]) {
  230. if (petImages[ident] > 1) {
  231. $(this).css("background-color", "blue");
  232. } else {
  233. $(this).css("background-color", "green");
  234. }
  235. } else {
  236. $(this).css("background-color", "red");
  237. }
  238. }
  239. });
  240.  
  241. $("#lair-pets .item-thumb").each(function () {
  242. const nameElement = $(this).find(".gen-small > a > strong");
  243. const name = nameElement.text();
  244. let breed = name.split(' the ');
  245. breed = breed[breed.length - 1];
  246. breed = breed.trim().split('(')[0].trim().replace(/^\s*\S*(Male|Female|Androgynous|Hermaphrodite|Undecided|Robot|Genderless|Agender|Bigender|Genderqueer|Neutrois|Pangender|Genderfluid|Non-Binary|Intersex|Other)/i, "").trim();
  247. let id = name.split('(')[1].match(/\d+/ig);
  248. if (id) {
  249. id = Number(id[0]);
  250. }
  251. let list = petsList[breed];
  252. if (list) {
  253. list.forEach(function (pet, i) {
  254. if (pet.id === id && i >= 1) {
  255. nameElement.css("background-color", "purple");
  256. }
  257. });
  258. }
  259. });
  260.  
  261. $("select").each(function () {
  262. if ($(this).attr("name") == "species_id") {
  263. $(this).find("option").each(function () {
  264. var pet = $(this).html();
  265. if (resultsTable[pet]) {
  266. $(this).html(pet + " (OWNED)");
  267. }
  268. });
  269. }
  270. });
  271. }
  272.  
  273. $("#owned-pets-button").click(highlightPets);
  274.  
  275. // From http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once
  276. onkeydown = onkeyup = function(e){
  277. e = e || event; // to deal with IE
  278. map[e.keyCode] = e.type == 'keydown';
  279.  
  280. if (map[17] && map[81]) {
  281. highlightPets();
  282. }
  283. }
  284.  
  285. if (alwaysHighlight) {
  286. highlightPets();
  287. }
  288.  
  289. });