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).

2022/03/08のページです。最新版はこちら

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Arrow Keys navigate Marketplace
// @namespace    http://tampermonkey.net/
// @version      0.6
// @license      MIT
// @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).
// @author       Aida Beorn
// @match        https://marketplace.secondlife.com/*
// @icon         https://www.google.com/s2/favicons?domain=secondlife.com
// @grant              None
// ==/UserScript==

function NextPage() {
	let next = document.getElementsByClassName('next_page')[0];
    if(next) {
        next.click();
    }
}

function PreviousPage() {
	let prev = document.getElementsByClassName('previous_page')[0];
    if(prev) {
        prev.click();
    }
}

function HandleKeyEvent(e) {
    switch(e.key) {
        case "ArrowRight":
            NextPage();
            break;
        case "ArrowLeft":
            PreviousPage();
            break;
    }
}

(function() {
    'use strict';

    document.addEventListener('keydown', HandleKeyEvent);
    RemoveDuplicates();
})();



function RemoveDuplicates() {
    let items1 = Array.from(document.getElementsByClassName('gallery-item'));
    let items2 = Array.from(document.getElementsByClassName('gallery-item'));
    let count = 0;
    const maxDistance = 10;
    const filterGatcha = false;
    const filterDuplicates = true;

    const gachaTerms = [
        "gacha",
        "rare",
        "common"
    ];

    for(let i = 0; i < items1.length; i++) {
        for(let k = 0; k < items2.length; k++) {
            const a = items1[i];
            const b = items2[k];
            const levDistance = levenshteinDistance(a.children[1].innerText, b.children[1].innerText);
            if(levDistance < maxDistance && i !== k) {
                //console.log(`${a.children[1].innerText}::${b.children[1].innerText}::${levDistance}`);
                b.remove();
                count++;
            }
        }
    }

    if(filterGatcha) {
        let items = Array.from(document.getElementsByClassName('gallery-item'));
        for(let j = 0; j < items.length; j++) {
            const item = items[j];
            const itemLower = item.children[1].innerText.toLowerCase();
            if (gachaTerms.some(v => itemLower.includes(v))) {
                console.log(itemLower);
                item.remove();
                count++;
            }
        }
    }

    console.log(`Removed ${count} items`);
}

function levenshteinDistance(a, b){
  if(a.length == 0) return b.length;
  if(b.length == 0) return a.length;

  var matrix = [];

  // increment along the first column of each row
  var i;
  for(i = 0; i <= b.length; i++){
    matrix[i] = [i];
  }

  // increment each column in the first row
  var j;
  for(j = 0; j <= a.length; j++){
    matrix[0][j] = j;
  }

  // Fill in the rest of the matrix
  for(i = 1; i <= b.length; i++){
    for(j = 1; j <= a.length; j++){
      if(b.charAt(i-1) == a.charAt(j-1)){
        matrix[i][j] = matrix[i-1][j-1];
      } else {
        matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, // substitution
                                Math.min(matrix[i][j-1] + 1, // insertion
                                         matrix[i-1][j] + 1)); // deletion
      }
    }
  }

  return matrix[b.length][a.length];
};