RonPaul2016 / Actu - Sources interdites

// ==UserScript==
// @name         Actu - Sources interdites
// @namespace    Actu - Sources interdites
// @version      0.2
// @description  Indique les topics dont la source est interdite, et les topics ne contenant aucune source.
// @author       RonPaul2016
// @match        http://www.jeuxvideo.com/forums/0-69-*
// @grant        none
// @require      https://code.jquery.com/jquery-3.4.0.js
// @require      https://use.fontawesome.com/releases/v5.8.1/js/all.js
// @copyright    2019+, RonPaul2016
// @license      MIT
// ==/UserScript==

const URL_SOURCES_INTERDITES = 'http://www.jeuxvideo.com/forums/42-69-57605709-1-0-1-0-liste-des-sources-interdites.htm';
const BASE_URL = 'http://www.jeuxvideo.com';

var topics_interdits = [];
var nb_topics_courant;

(function () {
  placer_bouton();
})();

function placer_bouton() {
  var search_bar = $("div.bloc-rech-forum")[0];
  $(search_bar).html($(search_bar).html() + '<button class="btn btn-lancer-rech" style="width: unset; padding: 3px" onclick="ouvrir_topic_sources();"><i id="icone_sources" class="fas fa-newspaper"></i> Sources interdites</span></button>');
}

window.ouvrir_topic_sources = function () {
  $("#icone_sources").attr("class", "fas fa-sync-alt fa-spin");
  $.get(URL_SOURCES_INTERDITES, function (data) {
    var sources_interdites = lister_sources_interdites(data);
    lister_topics(sources_interdites);
  });
}

function lister_topics(sources_interdites) {
  var liste_topics = $(".topic-list")[0];
  var topics = $(liste_topics).find("li").not(".topic-head").not(".move");

  nb_topics_courant = 0;

  for (var i = 0; i < topics.length; i++) {
    var url_topic = BASE_URL + $($(topics[i]).find(".lien-jv")[0]).attr("href");
    trouver_lien_source_topic(url_topic, sources_interdites, topics[i], topics.length);
  }
}

function trouver_lien_source_topic(url, sources_interdites, element_html, nb_topics) {
  $.get(url, function (data) {
    var sources = [];
    var texte = $($(data).find(".txt-msg")[0]).html();

    var topic_a_surligner = false;

    var pattern = /((http)s*:\/\/\w*\.*)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
    var match = texte.match(pattern);
    if (match) {
      sources = match;
    }
    else {
      topic_a_surligner = true;
    }

    if (!topic_a_surligner) {
      for (var i = 0; i < sources_interdites.length; i++) {
        for (var j = 0; j < sources.length; j++) {
          if (sources[j].includes(sources_interdites[i])) {
            topic_a_surligner = true;
            break;
          }
        }
        if (topic_a_surligner) {
          break;
        }
      }
    }

    if (topic_a_surligner) {
      surligner_topic(element_html);
    }
  }).done(function () {
    nb_topics_courant += 1;
    if (nb_topics_courant === nb_topics - 1) {
      $("#icone_sources").attr("class", "fas fa-newspaper");
    }
  });
}

function surligner_topic(element_html) {
  $("li[data-id=" + $(element_html).attr("data-id") + "]").css("background", "rgba(255, 0, 0, 0.5)");
}

function lister_sources_interdites(data) {
  var paragraphes = $($(data).find(".txt-msg")[0]).find("p");
  var texte = "";
  for (var i = 0; i < paragraphes.length; i++) {
    texte += $(paragraphes[i]).html();
  }

  var sources = trouver_liens(texte);
  return sources;
}

function trouver_liens(texte) {
  var pattern = /(www\.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
  var sources = texte.match(pattern);
  for (var i = 0; i < sources.length; i++) {
    sources[i] = sources[i].replace("www.", "");
  }

  return sources;
}