Greasy Fork is available in English.

zive.cz - spojeni kapitol do jedne stranky

prednacte vsechny kapitoly vybraneho clanku do otevrene stranky

Verze ze dne 2015-10-20 16:07:16 UTC. Zobrazit nejnovější verzi.

// ==UserScript==
// @name        zive.cz - spojeni kapitol do jedne stranky
// @namespace   monnef.tk
// @author      moen
// @description prednacte vsechny kapitoly vybraneho clanku do otevrene stranky
// @include     http://www.zive.cz/*
// @version     6
// @grant       none
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

// ----------------------------------
// -- zacatek uzivatelskeho nastaveni

// onlyPartialHide - nastaveni zpracovani vybranych
//   elementu (napr. tlacitka pro navigaci mezi kapitolami, seznam kapitol)
// true - pouze zpruhledneni
// false - uplne skryti
var onlyPartialHide = false;

// pokud zapnuto (true), pak pro nacitani dalsich kapitol vlozi nahodne
// dlouhy cekaci interval
var insertWaiting = false;
var waitingMin = 250; // ms
var waitingMax = 2500; // ms

// -- konec uzivatelskeho nastaveni
// ----------------------------------

var debug = false;
var debugPrintRawArticles = false;
var logTitle = "[chaptJoiner]";
var mainArticleSelector = "#ctl04_maindata";

function log(s) {
  console.log(logTitle + " " + s);
}

function logD(s) {
  if (debug) console.log(logTitle + "[D] " + s);
}

this.$ = this.jQuery = jQuery.noConflict(true);

String.prototype.startsWith = function (prefix) {
  return this.indexOf(prefix) === 0;
};

String.prototype.endsWith = function (suffix) {
  return this.match(suffix + "$") == suffix;
};

log("started");
logD("debug output");

function getNextLink(mainAr) {
  return $("a.ar-btn", mainAr).filter(function (index) {
    var v = $(this).html();
    return v.indexOf("Další kapitola") != -1;
  });
}


function getMainArticleDiv(page) {
  return $(mainArticleSelector, page);
}

var mainAr = getMainArticleDiv($("html"));
if (mainAr.length === 0) {
  logD("main article not found");
  return;
}
if (debug) mainAr.css("border", "1px solid red");

var mainArContent = mainAr.find(".ar-content");
if (mainArContent.length === 0) {
  logD("main article content not found");
  return;
}
if (debug) mainArContent.css("border", "1px solid darkred");

var chapterTitles = getChapterTitles();
logD("got " + chapterTitles.length + " chapter titles: " + chapterTitles);

var nextChapterTitleIndex = 0

// --

function getCurrentChapterTitle() {
  return chapterTitles[nextChapterTitleIndex];
}

function moveToNextChapterTitle() {
  nextChapterTitleIndex++;
}

function appendNextChapter(newMainAr) {
  var newMainArContent = newMainAr.find(".ar-content");
  if (newMainArContent.length == 0) {
    logD("can't find content in new main ar");
  }
  mainArContent.html(mainArContent.html() +
    "<br>\n<!-- Inserted by " + logTitle + " -->\n" +
    newMainArContent.html());
}

function generateChapterTitleHTML() {
  var res = "<h3 style=\"margin:0\" title=\"Generated by chaptJoiner\">" + getCurrentChapterTitle() + "</h3>";
  moveToNextChapterTitle();
  return res;
}

function appendChapterTitle() {
  mainArContent.html(mainArContent.html() + generateChapterTitleHTML());
}

function prependChapterTitle() {
  if (chapterTitles.length !== 0) mainArContent.prepend(generateChapterTitleHTML());
}

var applyHiding = function (idx) {
  var elem = $(this);
  if (onlyPartialHide) {
    elem.css("opacity", "0.1");
  } else {
    elem.hide();
  }
}

function getChapterWrapper() {
  return $(".ar-chapters");
}

function getChapterOptions() {
  return $(".ar-chapters option");
}

function getChapterTitlesFromOptions() {
  var titles = [];
  $(getChapterOptions()).each(function () {
    var elem = $(this);
    var text = elem.text();
    text = text.replace(/^\s*[0-9]+: /, "").trim();
    titles.push(text);
  });
  return titles;
}

function getChapterListItems() {
  return $(".ar-chapters li");
}

function getChapterTitlesFromListItems() {
  var titles = [];
  $(getChapterListItems()).each(function () {
    var elem = $(this);
    var text = elem.text();
    titles.push(text);
  });
  return titles;
}

function getChapterTitles() {
  return getChapterTitlesFromListItems();
}

function makeFinalTouches() {
  var sigId = "chaptJoinerSig";
  $(".button-row a.chapter-next, .button-row a.chapter-previous").each(applyHiding);
  mainAr.append($("<div id='" + sigId + "'>Skript spojující kapitoly do jedné stránky vám napsal <a href='http://monnef.tk'>moen</a>.</div>"));
  $("#" + sigId).
    css("text-align", "right").
    css("font-size", "120%");
  getChapterWrapper().each(applyHiding);
  $("p.ar-tags").each(applyHiding);
}

function extractArticleTitle(elem) {
  var h3 = $("div h3", elem);
  if (h3.length > 0) {
    return h3.first().text();
  }
  return "";
}

function queueNextChapter(url) {
  $.ajax({
    url: url
  }).done(function (data) {
    logD("got response, parsing");
    var page = $.parseHTML(data);
    logD("parsed");
    var subMainAr = $(mainArticleSelector, page);
    var subMainArTitle = extractArticleTitle(subMainAr);
    logD("sub article title: \"" + subMainArTitle + "\"");
    if (debugPrintRawArticles) logD("subMainAr:" + subMainAr);
    var currentChapterTitle = getCurrentChapterTitle();
    if (currentChapterTitle !== subMainArTitle && !subMainArTitle.startsWith(currentChapterTitle)) {
      appendChapterTitle();
    } else {
      moveToNextChapterTitle();
    }
    appendNextChapter(subMainAr);
    processCurrMainAr(subMainAr, false);
  });
}

function processCurrMainAr(currentMainAr, isFirst) {
  var linkToNext = getNextLink(currentMainAr);
  if (linkToNext.length === 0) {
    logD("next link not found");
    if (!isFirst) makeFinalTouches();
    return;
  } else {
    logD("link found");
  }
  var linkTarget = linkToNext.prop('href');
  if (debug) {
    linkToNext.parent().css("border", "1px dashed blue");
    logD("link points @ " + linkTarget);
  }
  var toFire = function () {
    queueNextChapter(linkTarget);
  };
  if (insertWaiting) {
    var ms = Math.floor(Math.random() * (waitingMax - waitingMin)) + waitingMin;
    logD("waiting for " + ms + "ms");
    window.setTimeout(toFire, ms);
  } else {
    toFire();
  }
}

if (extractArticleTitle(mainAr) != getCurrentChapterTitle()) {
  prependChapterTitle();
} else {
  moveToNextChapterTitle();
}
processCurrMainAr(mainAr, true);