RonPaul2016 / Actu - formatage d'article

// ==UserScript==
// @name         Actu - formatage d'article
// @namespace    Actu - formatage d'article
// @version      0.1
// @description  Formate automatiquement un article de presse selon les règles d'Actu.
// @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
// @resource     MODAL https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @match        http://www.jeuxvideo.com/forums/0-69-0-1-0-1-0-actualites.htm
// @copyright    2019+, RonPaul2016
// @license      MIT
// ==/UserScript==

var cssTxt = GM_getResourceText("MODAL");

GM_addStyle(cssTxt);

$.ajaxPrefilter(function (options) {
  if (options.crossDomain && jQuery.support.cors) {
    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
    options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
  }
});

const URL_INFOS_SITES = "https://api.jsonbin.io/b/5cf801ccd2127723845c8a5b/latest";

(function () {
  placer_champ_lien();
  ajouter_modal();
})();

function placer_champ_lien() {
  var form = $("form.form-post-topic");
  var conteneur = $(form).find("div.form-group")[0];
  var html = '<input id="lien_article" class="form-control js-focus-field" type="text" name="lien_article" placeholder="Saisir l\'URL de l\'article" value="" style="margin-bottom: 5px">';
  $(conteneur).prepend(html);

  $("#lien_article").keypress(function (e) {
    if (e.which === 13) {
      e.preventDefault();
      charger_article($(this).val());
    }
  });
}

function ajouter_modal() {
  var html = '<div id="modal_article" class="modal" style="padding:15px; width:75%"><h3 id="titre_article"></h3><ul id="paragraphes_article" style="list-style: none;"></ul></div>';
  $("body").append(html);
}

function charger_article(url) {
  var domaine = url.split("/")[2].split(".")[1];
  $.get(url, function (data) {
    var titre = trouver_titre(data);
    var intro = trouver_intro(data);
    var nb_paragraphes_max = 2;
    if (intro.length == 0) {
      nb_paragraphes_max = 3;
    }
    var paragraphes = trouver_paragraphes(data, intro, nb_paragraphes_max);

    afficher_contenu(url, titre, intro, paragraphes);
  });
}

function afficher_contenu(url, titre, intro, paragraphes) {
  var nouveau_titre = verifier_titre(titre);
  $("#titre_topic").val(nouveau_titre);

  $("#message_topic").text("");
  if (nouveau_titre !== titre) {
    $("#message_topic").text($("#message_topic").text() + "<u>'''Titre complet :'''</u> ''" + titre + "''\n\n");
  }

  var i;

  if (intro.length > 0) {
    $("#message_topic").text($("#message_topic").text() + "'''" + intro[0] + "'''\n\n");
    i = 0;
  }
  else {
    $("#message_topic").text($("#message_topic").text() + "'''" + paragraphes[0] + "'''\n\n");
    i = 1;
  }

  for (; i < paragraphes.length; i++) {
    $("#message_topic").text($("#message_topic").text() + paragraphes[i] + "\n\n");
  }

  $("#message_topic").text($("#message_topic").text() + "<u>'''Source :'''</u> " + url);
}

function trouver_titre(data_article) {
  var titre = $(data_article).find("h1").text().replace(/^ +/gm, '').replace(/\s\s+/g, ' ').replace(/\n/g, "");
  return titre;
}

function trouver_intro(data_article) {
  var intro = $(data_article).find("article").find("header").find("p, h2, h3");
  var res_intro = [];
  for (var i = 0; i < intro.length; i++) {
    if ($(intro[i]).text().replace(/^ +/gm, '').replace(/\s\s+/g, ' ').split(" ").length >= 15) {
      res_intro.push($(intro[i]).text().replace(/^ +/gm, '').replace(/\s\s+/g, ' ').replace(/\n/g, ""));
    }
  }

  return res_intro;
}

function trouver_paragraphes(data_article, intro, nb_paragraphes_max) {
  var nb_paragraphes = 0;
  var paragraphes = $(data_article).find("article, main, section[class*=article]").find("p, h2").toArray();
  var res_paragraphes = [];
  for (var i = 0; i < paragraphes.length; i++) {
    $(paragraphes[i]).text($(paragraphes[i]).text().replace(/^ +/gm, '').replace(/\s\s+/g, ' ').replace(/\n/g, ""));
    if (!intro.includes($(paragraphes[i]).text())) {
      if ($(paragraphes[i]).prop("tagName") !== "P") {
        res_paragraphes.push($(paragraphes[i]).text().replace(/\n/g, ""));
        if (res_paragraphes.length == 1) {
          nb_paragraphes++;
        }
      }
      else {
        if ($(paragraphes[i]).text().split(" ").length >= 15) {
          if (!($(paragraphes[i]).text().toLowerCase().includes("réserv") && $(paragraphes[i]).text().toLowerCase().includes("abonn"))) {
            nb_paragraphes++;
            res_paragraphes.push($(paragraphes[i]).text().replace(/\n/g, ""));
          }
        }
      }
    }

    if (nb_paragraphes >= nb_paragraphes_max) {
      break;
    }
  }

  return res_paragraphes;
}

function verifier_titre(titre) {
  if (titre.length > 100) {
    var titre_split = titre.split(" ");
    var nouveau_titre = "";
    for (var i = 0; i < titre_split.length; i++) {
      if ((nouveau_titre + titre_split[i]).length <= 94) {
        if (i !== 0) {
          nouveau_titre += " ";
        }
        nouveau_titre += titre_split[i];
      }
      else {
        break;
      }
    }

    nouveau_titre += " [...]";

    return nouveau_titre;
  }
  else {
    return titre;
  }
}