rainer.adelstonegmail.com / Forumbot

// ==UserScript==
// @name        Forumbot
// @namespace   germany.ru
// @version     1.2
// @match       https://*.germany.ru/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant       unsafeWindow
// @license     MIT
// ==/UserScript==

const bots = new Map([
  ["watcher_bot_01", ""],
  ["watcher_bоt_01", ""],
  ["watchеr_bot_01", ""]
]);
const trolls = ["johnsson", "Nitro 666", "Кнопка24", "Proricatel", "BVZZZ"];
const warning = " - это местный тролль Яша. Не кормите тролля.</p>" +
  "<p><a href=\"https://foren.germany.ru/religion/f/33253210.html#Post33253210\" target=\"_blank\">https://foren.germany.ru/.../33253210.html#Post3325...</a></p>" +
  "<p><br></p>" +
  "<p><a href=\"https://addons.mozilla.org/ru/firefox/addon/trollblock/\" target=\"_blank\">https://addons.mozilla.org/ru/firefox/addon/trollblock/</a></p>" +
  "<p><br></p>" +
  "<p><a href=\"https://chrome.google.com/webstore/detail/trollblock/hdedhpmpgjhlgojbfkmcifagaogjmodm?utm_source=chrome-app-launcher-info-dialog\" target=\"_blank\">https://chrome.google.com/webstore/.../trollblock/</a></p>" +
  "<p><br></p>" +
  "<p><a href=\"https://addons.opera.com/de/extensions/details/trollblock/?display=en\" target=\"_blank\">https://addons.opera.com/de/extensions/.../trollblock/</a></p>";

$(document).on('ready', () => {
  if ($("#header-user-info").find('a').first().html().indexOf("Войти") !== -1) {
    doLogin();
  }
  else {
    dispatch();
  }
  setTimeout(() => {
    window.location.reload();
  }, 1000 * 60 * 60);
});

function dispatch() {
  const new_post_patt = "newreply.pl";
  const current_url = window.location.href;
  if (getCookie("checking") && getCookie("checking") !== "") {
    checkLastPage();
    goToUrl(getCookie("checking"), checkBotPosts, checkUserPosts);
  }
  else if (current_url.match(new_post_patt)) {
    doPostWarning();
  }
  else {
    goToUrl("https://foren.germany.ru/religion.html", checkCookies);
  }
}

function goToUrl(url, callback1, callback2) {
  const current_url = window.location.href;
  if (current_url !== url) {
    window.location.replace(url);
  }
  else if (callback1 && typeof callback1 === 'function') {
    callback2 ? callback1(callback2) : callback1();
  }
}

function checkCookies() {
  $(".td-54-single").each(function (index) {
    if (index > 1) {
      let topic = $(this).find('a').first().html().trim();
      topic = "_" + topic.replace(/"/g, '').replace(/\s/g, '_');
      const topic_url = $(this).find('a').first().attr("href");
      const responses = $(".td-10-single").eq(index);
      let count_posts;
      if (responses.find("span").hasClass("badge")) {
        count_posts = responses.find("a").first().html();
      }
      else {
        count_posts = responses.find("span").first().html();
      }
      if (count_posts && typeof parseInt(count_posts) === 'number' && (!getCookie(topic) || getCookie(topic) !== count_posts)) {
        setCookie("checking", topic_url, "0");
        goToUrl(topic_url);
        return false;
      }
    }
  });
}

function checkBotPosts(callback) {
  let no_halt = true;
  $(".td-200px").each(function (index) {
    if (!no_halt) return no_halt;
    $(this).find("div").first().find("a").each(function () {
      const profile_patt = new RegExp(/my.germany.ru\/(1269412|1269836|1269838)/);
      if ($(this).attr("href") && $(this).attr("href").match(profile_patt)) {
        let topic = $("h3").first().text().trim();
        topic = "_" + topic.replace(/"/g, '').replace(/\s/g, '_');
        let last_post = $(".absolute-bottom-right").last().find("a").first().html();
        last_post = last_post.substring(1);
        setCookie(topic, parseInt(last_post) - 1);
        setCookie("checking", "", "0");
        goToUrl("https://foren.germany.ru/religion.html");
        no_halt = false;
        return no_halt;
      }
      else if ($(this).attr("href") && index === $(".td-200px").length - 1 && typeof callback === 'function') {
        callback();
        no_halt = false;
        return no_halt;
      }
    });
  });

}

function checkUserPosts() {
  let no_halt = true;
  $(".td-200px").each(function (index) {
    if (!no_halt) return no_halt;
    $(this).find("div").first().find("a").each(function () {
      const profile_patt = "my.germany.ru";
      if ($(this).attr("href") && $(this).attr("href").match(profile_patt)) {
        const troll_index = trolls.indexOf($(this).html());
        if (troll_index !== -1) {
          const reply_url = $(this).closest(".td-200px").parent().find(".iconmiddle-reply").first().parent().attr("href");
          if (reply_url) {
            setCookie("checking", "", "0");
            goToUrl(reply_url);
            no_halt = false;
            return no_halt;
          }
        }
        else if ($(this).attr("href") && index === $(".td-200px").length - 1) {
          let topic = $("h3").first().text().trim();
          topic = "_" + topic.replace(/"/g, '').replace(/\s/g, '_');
          let last_post = $(".absolute-bottom-right").last().find("a").first().html();
          last_post = last_post.substring(1);
          setCookie(topic, parseInt(last_post) - 1);
          setCookie("checking", "", "0");
          goToUrl("https://foren.germany.ru/religion.html");
          no_halt = false;
          return no_halt;
        }
      }
    });
  });
}

function checkLastPage() {
  const headline = $("h3").first();
  const last_page = headline.next().find(".btn").length;
  if (last_page > 1) {
    const link_patt = "foren.germany.ru/religion/f/";
    if (headline.next().find("a").first().attr("href").match(link_patt)) {
      const new_url = headline.next().find(".btn").eq(last_page - 2).attr("href");
      if (new_url) {
        setCookie("checking", new_url, "0");
      }
    }
  }
}

function doLogin() {
  const last_bot_id = getCookie("bot") ? parseInt(getCookie("bot")) : 0;
  const bot_arr = [...bots];
  let bot_name, bot_passw;
  if (bot_arr.length > last_bot_id + 1) {
    bot_name = bot_arr[last_bot_id + 1][0];
    bot_passw = bot_arr[last_bot_id + 1][1];
    setCookie("bot", last_bot_id + 1);
  }
  else {
    bot_name = bot_arr[0][0];
    bot_passw = bot_arr[0][1];
    setCookie("bot", 0);
  }
  const errors_number = getCookie("errors") ? parseInt(getCookie("errors")) : 0;
  if ($(".error").first().length > 0) {
    setCookie("errors", errors_number + 1);
  }

  if (errors_number < 10) {
    const modal = $("#modal-header");
    modal.show(function () {
      $("#username-login-popup").val(bot_name);
      modal.find("input[type=password]").first().val(bot_passw);
      modal.find("input[type=checkbox]").first().prop("checked", true);
      modal.find("input[type=submit]").first().trigger('click');
    });
  }
}

function doPostWarning() {
  let headline = $(".blockcontent").find("b").first().html();
  headline = "<p><b>" + headline.substring(12, headline.indexOf("<br>")) + "</b>";
  const random = "<p><br></p><font size=\"1\">Post Id: " + Math.round(Math.random() * 10000) + "" +
    Math.round(Math.random() * 10000) + "" +
    Math.round(Math.random() * 10000) + "" +
    Math.round(Math.random() * 10000) + "" +
    "</font>";
  let redactor = $(".redactor-editor").first();
  redactor.html("");
  $("#trtxt").val(headline + warning + random);
  redactor.closest(".well-alt").find("input[type=submit]").trigger('click');
}

function getCookie(cname) {
  let name = cname + "=";
  let ca = document.cookie.split(';');
  for (let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) === ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) === 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function setCookie(cname, cvalue, exp) {
  let now = new Date();
  now.setTime(now.getTime() + 24 * 3600 * 1000);
  const expires = exp ? " expires=" + exp + ";" : " expires=" + now.toUTCString() + ";";
  document.cookie = cname + "=" + cvalue + ";" + expires + " path=/";
}