clemente / YggTorrent amélioré

// ==UserScript==
// @name           YggTorrent amélioré
// @namespace      https://openuserjs.org/users/clemente
// @match          https://ygg.re/*
// @match          https://*.ygg.re/*
// @version        1.23
// @author         clemente
// @license        MIT
// @description    Ajoute la connexion automatique, un bouton télécharger à la recherche, et plus encore
// @inject-into    content
// @noframes
// @icon           data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAJcElEQVRoge2Xe1hUZR7H3+HOjCAgYni/kJeM1Lywi4tlKRiGmoigpHPOmcuZYVAw9VnStkjaNFtrVSijx6cy0xSYGRABEwUHEFAEAc+cc+bGpU3Zp9RuuLWp3/0jHERoq83+2OeZz/N8/5jz/t7zfr/n/N5zzhDiwoULFy5cuHDh4mcBINlz9ar/K5cvj3nhs88n/R7K6uoal3n9egAAt/tm/PWuLtmWjs8Wbm7/dM8GR3ttmr29bb2trSvV1vbP+6n1trauNHtbR7qj49zmts59Wzs+i8u8etX/N5nf2nl51iZHR77OYv9GzVvBmEXQnADqdxLNCWDMItS8BalWx42Nbe0lWzs75wGQ/CrjACQZ7e3xaXaHgxVEKAULFIIFCkEEbeYhv8Q5RXFmMILoHGd4AQpBhMpihdpihUKwgDbzzhpGEEFxZtBmHkrxxxqVxQqGF0CbeSju1PEiVLyANLv98vOdnfRRwP0XB/hzR0fcOrv1ilIwg2q6cGulQX8rUZ+PlQX5oOpqwfBmyLlW0GYOTFMjEg16JOnzkagvAFNlQpLRgOjXdmDh9leRcPQI2KZGrCouQmJBPhKNBrCXWqA4V4cl7+biyaxtiN2zG/KK01DUnv2xRl9w+9mK0z8wZg4KnsM6u/X68x2ONb/I/PP/sE9Ms1talXwraL4Fa2tMN0fMnnXTSyaFl0yGGfK1YPlWUOZmsFYe0Tt3wGfwYHj7+cEnIAAjZkyHdEgQiEQCQgi8/f0R9sR8BIwcCS+ZDAGjRyMmaxtGzp4Ndy8vEEIgcXND8IQJGBcVBW8/P3j6+t4OT0i4qeZbIeeaoeBbkW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PoGe+pYx1GKKz5UFrywdqM0JkLv0ouyH5vyuLohwkhv+5PDSGE+A0fHjxvA7V6jWGPUdOU96lONH6ntRXe1tgMuP8yQms1Qicav9dezL9Mlew7Pn+Lhhk6dugD/5P5u/CQBgcPf2TZwicXZKakLnv7pdfi92dlL9+/Led+Kn5/VvYz+zJ3Lti2Ln3aykUxQcODRhFCPH+L8XuR9JxQSgjx+50k61njN11xFy5cuHDhwoULF/8n/AeQIqepPaGRrgAAAABJRU5ErkJggg==
// @grant          GM_getValue
// @grant          GM_setValue
// @homepageURL    https://openuserjs.org/scripts/clemente/YggTorrent_am%C3%A9lior%C3%A9
// @supportURL     https://openuserjs.org/scripts/clemente/YggTorrent_am%C3%A9lior%C3%A9/issues
// @updateURL      https://openuserjs.org/meta/clemente/YggTorrent_amélioré.meta.js
// ==/UserScript==

/* jshint esversion: 6 */

// login();
addDownloadButtonToTorrents();
hideSidebar();
displayLargerNfo();
searchByLatestFirst();
fixFavicon();
// displayNewSearchButton();
keepSearchWhenClickingOnSubcategoryIcon();
replaceIPTVWithRss();

function login(alreadyCalled) {
  const loginButton = document.getElementById('register');
  if (!loginButton) return; // If the user is already logged in, do nothing
  
  if (!alreadyCalled && !window.jQuery) { // If jQuery is not present, wait one second so that the scripts are loaded and the login system is active
    setTimeout(() => login(true), 1000);
    return;
  }

  let isLoginFormValid = true;
  const loginForm = document.getElementById('user-login');
  loginForm.querySelectorAll('input').forEach(input => {
    isLoginFormValid = isLoginFormValid && input.checkValidity();
  });
  if (!isLoginFormValid) return; // If the form is not valid, then autocomplete is not active and the popup should not bother the user

  loginButton.click();
  loginForm.querySelector('button').click();
  
  function closePopupIfDisplayed(mutations, observer) {
    mutations.forEach(mutation => {
      mutation.addedNodes.forEach(node => {
        if (node.classList.contains('alert-success')) {
          document.querySelector('button.close').click();
          observer.disconnect();
        }
      });
    });
  }

  // Wait for the confirmation popup to appear then close it
  const popupObserver = new MutationObserver(closePopupIfDisplayed);
  popupObserver.observe(document.body, { childList: true });
}

function addDownloadButtonToTorrents() {
  // For every torrent, add the download button
  const torrents = document.querySelectorAll('.results table tbody tr');
  torrents.forEach(torrent => {
    const torrentId = torrent.querySelector('a[target]').target;
    const downloadIcon = document.createElement('span');
    downloadIcon.classList.add('ico_download');
    const downloadButton = document.createElement('a');
    downloadButton.href = `/engine/download_torrent?id=${torrentId}`;
    downloadButton.append(downloadIcon);
    downloadButton.style = 'float: left;';
    
    const nameLink = torrent.querySelector('td:nth-child(3) a');
    nameLink.parentNode.insertBefore(downloadButton, nameLink);
  });
}

function hideSidebar() {
  const sidebar = document.getElementById('cat');
  if (sidebar && sidebar.classList.contains('active')) {
    sidebar.querySelector('.open').click();
  }
}

function displayLargerNfo() {
  const modal = document.getElementById('nfoModal');
  if (!modal) return; // If there is no modal, the user is not on a torrent page and nothing should be done
  const modalDialog = modal.querySelector('.modal-dialog');
  modalDialog.classList.remove('modal-sm');
  modalDialog.classList.add('modal-lg');
}

function searchByLatestFirst() {
  const searchForm = document.querySelector('form.search');
  const orderInput = document.createElement('input');
  orderInput.name = 'order';
  orderInput.value = 'desc';
  orderInput.style = 'display: none';
  const sortInput = document.createElement('input');
  sortInput.name = 'sort';
  sortInput.value = 'publish_date';
  sortInput.style = 'display: none';
  searchForm.append(orderInput);
  searchForm.append(sortInput);
}

function fixFavicon() {
  const favicon = document.querySelector('link[href="/favicon.ico"]');
  favicon.href = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAJcElEQVRoge2Xe1hUZR7H3+HOjCAgYni/kJeM1Lywi4tlKRiGmoigpHPOmcuZYVAw9VnStkjaNFtrVSijx6cy0xSYGRABEwUHEFAEAc+cc+bGpU3Zp9RuuLWp3/0jHERoq83+2OeZz/N8/5jz/t7zfr/n/N5zzhDiwoULFy5cuHDh4mcBINlz9ar/K5cvj3nhs88n/R7K6uoal3n9egAAt/tm/PWuLtmWjs8Wbm7/dM8GR3ttmr29bb2trSvV1vbP+6n1trauNHtbR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KkNJc0LspIH/uTAQghZNXBN+Q6oewbhVgEijeC4o1Q20qQcPhN+AT4OzffgqwN0NjLQPFG0GIR5LWHEDrjIacBdy9PSIMCnO8OQgg8fLwxPmpO7zEJgdcgKaRBAXD39LirNcdgzcn3oLQUg+KNYIQipFpKv0sueCu9X9/fy+jw8ECqZH+uzlpyixEMoAU9lJcMmBw337nA8EenQnnuCBSiERRfAIovgNp6DCsO/Q2h0yb3+3wghEAaHIjIdAopjQbM3UhDOiRggE8Pd4ROm4L4gzuhthaD4gvACAZorSVQnDpwdGrs4w/8V/N3mLokJowu3Z+nsxz/QWk1gGo6jEV7MhDz2kZE73gOKw1/h9pqBCXkOUULeWBthVDUfYTY3VsQoVuN6c/GYSazHPNfTMHqYzlgxUKorEawYiFWFWVj/ospmMksx7TkOEToVmHx3q1Q1B8CaysELeRDIeqhsxXfUlYcKJuZvDT8F5nvQTJ+XsSDSYd3ZesuGb7Q2ougbSuCxlEE1lEIpbUAtHAE1ABiLHlg7UZo7EXQ2AqhsRdC4yiE0tY7hxaOQGnTQ+PoGe+pYx1GKKz5UFrywdqM0JkLv0ouyH5vyuLohwkhv+5PDSGE+A0fHjxvA7V6jWGPUdOU96lONH6ntRXe1tgMuP8yQms1Qicav9dezL9Mlew7Pn+Lhhk6dugD/5P5u/CQBgcPf2TZwicXZKakLnv7pdfi92dlL9+/Led+Kn5/VvYz+zJ3Lti2Ln3aykUxQcODRhFCPH+L8XuR9JxQSgjx+50k61njN11xFy5cuHDhwoULF/8n/AeQIqepPaGRrgAAAABJRU5ErkJggg==";
}

function displayNewSearchButton() {
  const buttonTitleByStatus = {
    true: "Recherche améliorée activée. Cliquer pour désactiver.",
    false: "Recherche améliorée désactivée. Cliquer pour activer.",
  };

  const buttonColorByStatus = {
    true: "color: #98e3da;",
    false: "color: inherit;",
  };

  const searchEngineByStatus = {
    true: "new_search",
    false: "engine",
  };

  const searchForm = document.querySelector('form.search');
  const searchInput = document.querySelector('form.search .input');
  const newSearchButton = document.createElement('button');
  const newSearchIcon = document.createElement('span');
  newSearchIcon.classList = 'ico_search-plus';
  newSearchButton.appendChild(newSearchIcon);

  function initFormWithStatus() {
    const newSearchStatus = GM_getValue('newSearchStatus', false);
    newSearchButton.title = buttonTitleByStatus[newSearchStatus];
    newSearchButton.style = 'left: unset; right: 15px;' + buttonColorByStatus[newSearchStatus];
    searchForm.action = `${new URL(searchForm.action).origin}/${searchEngineByStatus[newSearchStatus]}/search`;
  }

  function toggleNewSearchStatus() {
    const newSearchStatus = GM_getValue('newSearchStatus', false);
    GM_setValue('newSearchStatus', !newSearchStatus);
    initFormWithStatus();
  }

  newSearchButton.onclick = (e) => { toggleNewSearchStatus(); e.preventDefault(); };

  initFormWithStatus();
  searchInput.appendChild(newSearchButton);
}

function keepSearchWhenClickingOnSubcategoryIcon() {
  document.querySelectorAll('[class^="tag_subcat_"]').forEach(node => {
    const subcategoryId = node.className.split('tag_subcat_')[1];
    node.parentNode.href = `${document.URL}&sub_category=${subcategoryId}`;
  });
}

function replaceIPTVWithRss() {
  const iptvNode = document.evaluate('//*[text()="Regarder la télé"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE).snapshotItem(0);
  const parentNode = iptvNode.parentNode;
  const rssNode = document.createElement('a');
  rssNode.classList = 'title';
  rssNode.href = 'https://www.ygg.re/rss';
  rssNode.text = 'Flux RSS';
  iptvNode.remove();
  parentNode.appendChild(rssNode);
}