Artex / FA Search Similiar

// ==UserScript==
// @name        FA Search Similiar
// @namespace   Artex
// @match       https://www.furaffinity.net/view/*
// @version     1.0
// @author      Artex
// @description Generate search from submission tags to find similar images.
// @license MIT
// ==/UserScript==

function addTagToggle() {
  let tags = document.querySelectorAll(".tags-row .tags a");
  for (let tag of tags) {
    tag.href = "#";
    tag.setAttribute("similar", true);
    tag.addEventListener("click", function () {
      if (this.getAttribute("similar")) {
        this.setAttribute("similar", "");
        this.style = "background-color:#999;";
      }
      else {
        this.style = "background-color:#efb221";
        this.setAttribute("similar", true);
      }
    });
  }
}

function searchSimilar() {
  let tags = document.querySelectorAll(".tags-row .tags a");
  let tagList = [];
  for (let tag of tags) {
    if (tag.getAttribute("similar")) {
      tagList.push(tag.innerText)
    }
  }
  let query = tagList.join(" | ")
  window.open("https://www.furaffinity.net/search?q=" + query)
}

function addSearchButton() {
  let searchButton = document.createElement("BUTTON")
  searchButton.innerText = "Search Similar";
  searchButton.style = "display: block;";
  searchButton.addEventListener("click", function () {
    searchSimilar();
  });

  let tagsRow = document.getElementsByClassName("tags-row")[0];
  tagsRow.appendChild(searchButton);
  addTagToggle();
}

addSearchButton();