RonPaul2016 / Doublons Actu

// ==UserScript==
// @name         Doublons Actu
// @namespace    Doublons Actu
// @version      0.3
// @description  Indique les topics doublons, et ouvre automatiquement les deux topics pour comparer.
// @author       RonPaul2016
// @require      https://code.jquery.com/jquery-3.4.0.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js
// @require      https://use.fontawesome.com/releases/v5.8.1/js/all.js
// @resource     MODAL https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css
// @match        http://www.jeuxvideo.com/forums/0-69-0-1-0-1-0-actualites.htm
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @copyright    2019+, RonPaul2016
// @license      MIT
// ==/UserScript==

var cssTxt = GM_getResourceText("MODAL");

GM_addStyle(cssTxt);

var doublons_charges = false;

(function () {
  placer_bouton();

})();

function placer_bouton() {
  var search_bar = $("div.bloc-rech-forum")[0];
  $(search_bar).html($(search_bar).html() + '<button id="bouton_doublons" class="btn btn-lancer-rech" style="width: unset; padding: 3px"><i id="icone_doublons" class="fas fa-copy"></i> Doublons</span></button>');
  $("#bouton_doublons").click(function () {
    if (doublons_charges) {
      $("#modal_doublons").modal();
    }
    else {
      rechercher();
    }
  });
  var html = '<div id="modal_doublons" class="modal" style="padding:15px"></div>';
  $("body").append(html);
}

function rechercher() {
  $("#icone_doublons").attr("class", "fas fa-sync-alt fa-spin");
  $.get("http://www.jeuxvideo.com/recherche/forums/0-69-0-1-0-1-0-actualites.htm?search_in_forum=doublon&type_search_in_forum=texte_message", function (text) {
    var html = text;
    var liste_topics = selectionner_topics(html);
    trouver_liens_topics(liste_topics);
  });
}

function afficher_topics(topics) {
  var html_modal = "<ul class='topic-list topic-list-admin' style='margin-bottom: 0px'>";
  for (var i = 0; i < topics.length; i++) {
    html_modal += "<li id='doublon_" + i + "'>" + $(topics[i].topic).html() + "</li>";
  }

  html_modal += "</ul>";
  $("#modal_doublons").append(html_modal);

  for (i = 0; i < topics.length; i++) {
    $("#doublon_" + i).find("a.topic-title").click({
      topics: [
        topics[i].message,
        $(topics[i].topic).find("span.topic-subject").find("a").attr("href")
      ]
    }, ouvrir_topics);
  }
  $("#modal_doublons").modal();
}

function ouvrir_topics(event) {
  event.preventDefault();
  for (var i = 0; i < event.data.topics.length; i++) {
    window.open(event.data.topics[i], '_blank');
  }
}

function selectionner_topics(html) {
  html = $(html);
  var conteneur_topics = $(".topic-list", html);
  var liste_objets = $("li", conteneur_topics);

  var liste_topics = [];
  for (var i = 0; i < liste_objets.length; i++) {
    if ($(liste_objets[i]).attr("class") == "") {
      if ($(liste_objets[i]).find("img.topic-img").attr("title") !== "Topic fermé") {
        var pseudo = $(liste_objets[i + 1]).find("span.topic-author").text().replace(/ /g, "").replace(/\n/g, "").toLowerCase();
        var id_message = $(liste_objets[i + 1]).find("span.topic-subject").find("a.topic-title").attr("href").split("#post_")[1];
        var lien_message = "http://www.jeuxvideo.com/" + pseudo + "/forums/message/" + id_message;
        liste_topics.push({
          "topic": mise_en_page_topic(liste_objets[i]),
          "message": lien_message
        });
      }

      i++;
    }
  }

  return liste_topics;
}

function mise_en_page_topic(topic) {
  var new_topic = topic;

  $(new_topic).find("span.topic-admin, span.pointer").remove();
  $(new_topic).find("span.topic-count").remove();
  $(new_topic).find("span.topic-count-adm").remove();
  $(new_topic).find("span.topic-date").remove();
  $(new_topic).find("span.topic-select").remove();

  return new_topic;
}

function trouver_liens_topics(topics) {
  var count = {
    "count": 0
  }
  var i;
  for (i = 0; i < topics.length; i++) {
    charger_message(topics, i, count);
  }
}

function charger_message(topics, index, count) {
  $.get(topics[index].message, function (data) {
    count.count += 1

    var lien = $(data).find("div.txt-msg").find("a");
    if (lien) {
      lien = lien.attr("href");
      if (lien) {
        if (lien.includes("jeuxvideo.com")) {
          topics[index].message = lien;
        }
      }
    }

    if (count.count === topics.length) {
      $("#icone_doublons").attr("class", "fas fa-copy");
      doublons_charges = true;
      afficher_topics(topics);
    }
  });
}