Arrow Keys navigate Marketplace

This script makes it so you can navigate marketplace with arrow keys between next and previous pages. It also attemps to remove duplicates from results(highly imperfect).

  1. // ==UserScript==
  2. // @name Arrow Keys navigate Marketplace
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.10
  5. // @license MIT
  6. // @description This script makes it so you can navigate marketplace with arrow keys between next and previous pages. It also attemps to remove duplicates from results(highly imperfect).
  7. // @author Aida Beorn
  8. // @match https://marketplace.secondlife.com/*
  9. // @icon https://www.google.com/s2/favicons?domain=secondlife.com
  10. // ==/UserScript==
  11.  
  12. function NextPage() {
  13. let next = document.getElementsByClassName('next_page')[0];
  14. if(next) {
  15. next.click();
  16. }
  17. }
  18.  
  19. function PreviousPage() {
  20. let prev = document.getElementsByClassName('previous_page')[0];
  21. if(prev) {
  22. prev.click();
  23. }
  24. }
  25.  
  26. function HandleKeyEvent(e) {
  27. switch(e.key) {
  28. case "ArrowRight":
  29. NextPage();
  30. break;
  31. case "ArrowLeft":
  32. PreviousPage();
  33. break;
  34. }
  35. }
  36.  
  37.  
  38. (function() {
  39. 'use strict';
  40.  
  41. let captureSupported = false;
  42. let passiveSupported = false;
  43.  
  44. try {
  45. const options = {
  46. get capture() { // This function will be called when the browser
  47. // attempts to access the capture property.
  48. captureSupported = true;
  49. return false;
  50. },
  51. get passive() { // This function will be called when the browser
  52. // attempts to access the passive property.
  53. passiveSupported = true;
  54. return false;
  55. }
  56. };
  57.  
  58. window.addEventListener("test", null, options);
  59. window.removeEventListener("test", null, options);
  60. } catch(err) {
  61. captureSupported = false;
  62. let passiveSupported = false;
  63. }
  64.  
  65. const options = {
  66. capture: captureSupported,
  67. passive: passiveSupported
  68. };
  69.  
  70. document.addEventListener('keyup', HandleKeyEvent, options);
  71. RemoveDuplicates();
  72. })();
  73.  
  74.  
  75.  
  76. function RemoveDuplicates() {
  77. if(window.location.pathname !== "/products/search") {
  78. return;
  79. }
  80.  
  81.  
  82. let count = 0;
  83. let loopcount = 0;
  84. const maxDistance = 10;
  85. const filterGatcha = true;
  86. const filterDuplicates = true;
  87. const debuging = false;
  88.  
  89. const gachaTerms = [
  90. "gacha",
  91. "rare",
  92. "common"
  93. ];
  94.  
  95. const fragment = new DocumentFragment();
  96. const realDocument = document.getElementsByClassName('product-listing gallery')[0];
  97.  
  98. const realList = Array.from(realDocument.childNodes);
  99. realList.forEach(v => {
  100. fragment.appendChild(v);
  101. });
  102.  
  103. let items1 = Array.from(fragment.querySelectorAll('.gallery-item'));
  104. let items2;
  105.  
  106. let start;
  107. let endItemFilter;
  108. let endGachaFilter;
  109.  
  110. if(true) {
  111. start = new Date();
  112. }
  113.  
  114. items1.forEach((item1, idx1) => {
  115. items2 = Array.from(fragment.querySelectorAll('.gallery-item'));
  116. items2.forEach((item2, idx2) => {
  117. if(idx1 !== idx2) {
  118. const levDistance = levenshteinDistance(item1.children[1].innerText, item2.children[1].innerText);
  119. if(levDistance < maxDistance && levDistance !== 0) {
  120. if(debuging) {
  121. console.log(`${item1.children[1].innerText}::${item2.children[1].innerText}::${levDistance}`);
  122. //console.log(items1[idx1].children[1].innerText,items2[idx2].children[1].innerText);
  123. }
  124. item2.remove();
  125. count++;
  126. }
  127. loopcount++;
  128. }
  129. })
  130. })
  131.  
  132. if(true) {
  133. endItemFilter = new Date();
  134. }
  135.  
  136. if(filterGatcha) {
  137. let items = Array.from(fragment.querySelectorAll('.gallery-item'));
  138. for(let j = 0; j < items.length; j++) {
  139. const item = items[j];
  140. const itemLower = item.children[1].innerText.toLowerCase();
  141. if (gachaTerms.some(v => itemLower.includes(v))) {
  142. if(debuging) {
  143. console.log(itemLower);
  144. }
  145. item.remove();
  146. count++;
  147. }
  148. loopcount++;
  149. }
  150. }
  151.  
  152. if(true) {
  153. endGachaFilter = new Date();
  154.  
  155. console.log(`Items: ${endItemFilter - start}; Gacha: ${endGachaFilter - endItemFilter}`);
  156. }
  157.  
  158. realDocument.appendChild(fragment);
  159.  
  160. console.log(`Removed ${count} items in ${loopcount} iterations`);
  161. }
  162.  
  163.  
  164. // BEGIN js-levenstein
  165. /* MIT License
  166.  
  167. Copyright (c) 2017 Gustaf Andersson
  168.  
  169. Permission is hereby granted, free of charge, to any person obtaining a copy
  170. of this software and associated documentation files (the "Software"), to deal
  171. in the Software without restriction, including without limitation the rights
  172. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  173. copies of the Software, and to permit persons to whom the Software is
  174. furnished to do so, subject to the following conditions:
  175.  
  176. The above copyright notice and this permission notice shall be included in all
  177. copies or substantial portions of the Software.
  178.  
  179. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  180. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  181. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  182. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  183. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  184. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  185. SOFTWARE. */
  186. function levenshteinDistance(a, b){
  187. if (a === b) {
  188. return 0;
  189. }
  190.  
  191. if (a.length > b.length) {
  192. var tmp = a;
  193. a = b;
  194. b = tmp;
  195. }
  196.  
  197. var la = a.length;
  198. var lb = b.length;
  199.  
  200. while (la > 0 && (a.charCodeAt(la - 1) === b.charCodeAt(lb - 1))) {
  201. la--;
  202. lb--;
  203. }
  204.  
  205. var offset = 0;
  206.  
  207. while (offset < la && (a.charCodeAt(offset) === b.charCodeAt(offset))) {
  208. offset++;
  209. }
  210.  
  211. la -= offset;
  212. lb -= offset;
  213.  
  214. if (la === 0 || lb < 3) {
  215. return lb;
  216. }
  217.  
  218. var x = 0;
  219. var y;
  220. var d0;
  221. var d1;
  222. var d2;
  223. var d3;
  224. var dd;
  225. var dy;
  226. var ay;
  227. var bx0;
  228. var bx1;
  229. var bx2;
  230. var bx3;
  231.  
  232. var vector = [];
  233.  
  234. for (y = 0; y < la; y++) {
  235. vector.push(y + 1);
  236. vector.push(a.charCodeAt(offset + y));
  237. }
  238.  
  239. var len = vector.length - 1;
  240.  
  241. for (; x < lb - 3;) {
  242. bx0 = b.charCodeAt(offset + (d0 = x));
  243. bx1 = b.charCodeAt(offset + (d1 = x + 1));
  244. bx2 = b.charCodeAt(offset + (d2 = x + 2));
  245. bx3 = b.charCodeAt(offset + (d3 = x + 3));
  246. dd = (x += 4);
  247. for (y = 0; y < len; y += 2) {
  248. dy = vector[y];
  249. ay = vector[y + 1];
  250. d0 = _min(dy, d0, d1, bx0, ay);
  251. d1 = _min(d0, d1, d2, bx1, ay);
  252. d2 = _min(d1, d2, d3, bx2, ay);
  253. dd = _min(d2, d3, dd, bx3, ay);
  254. vector[y] = dd;
  255. d3 = d2;
  256. d2 = d1;
  257. d1 = d0;
  258. d0 = dy;
  259. }
  260. }
  261.  
  262. for (; x < lb;) {
  263. bx0 = b.charCodeAt(offset + (d0 = x));
  264. dd = ++x;
  265. for (y = 0; y < len; y += 2) {
  266. dy = vector[y];
  267. vector[y] = dd = _min(dy, d0, dd, bx0, vector[y + 1]);
  268. d0 = dy;
  269. }
  270. }
  271.  
  272. return dd;
  273. };
  274.  
  275. function _min(d0, d1, d2, bx, ay)
  276. {
  277. return d0 < d1 || d2 < d1
  278. ? d0 > d2
  279. ? d2 + 1
  280. : d0 + 1
  281. : bx === ay
  282. ? d1
  283. : d1 + 1;
  284. }