zive.cz - spojeni kapitol do jedne stranky

prednacte vsechny kapitoly vybraneho clanku do otevrene stranky

Od 13.01.2015.. Pogledajte najnovija verzija.

// ==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     4
// @grant       none
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

// moje zkusenosti s JavaScriptem a jQuery nejsou zavratne,
// takze pokud odhalite chyby nebo vykonostni nedostatky,
// prosim napiste mi :)

// ----------------------------------
// -- 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]";

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

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

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

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

function getNextLink(mainAr) {
	return $(".button-row a.chapter-next", mainAr).filter(function(index) {
		var v = $(this).html();
		return v.indexOf("Následující kapitola") != -1 || v.indexOf("Následující") != -1;
	});
}

function getMainArticleDiv(page) {
	return $("#main-article", page);
}

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

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

var nextChapterTitleIndex = 0

// --

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

function moveToNextChapterTitle() {
	nextChapterTitleIndex++;
}

function appendNextChapter(newMainAr) {
	mainAr.html(mainAr.html() +
		"<br>\n<!-- Inserted by " + logTitle + " -->\n" +
		newMainAr.html());
}

function generateChapterTitleHTML() {
	var res = "<h2 style=\"margin:0\">" + getCurrentChapterTitle() + "</h2>";
	moveToNextChapterTitle();
	return res;
}

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

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

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

function getChaptersSpans() {
	return $("div.box .box-heading span").filter(function(i) {
		return $(this).html().indexOf("Kapitoly článku") != -1
	});
}

function getChapterDivs() {
	return getChaptersSpans().parent().parent().parent();
}

function getChapterTitles() {
	var titles = [];
	$("div.box-data ul li", getChapterDivs().first()).each(function() {
		var elem = $(this);
		titles.push(elem.text());
	});
	return titles;
}

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%");
	getChapterDivs().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 = $("#main-article", page);
		var subMainArTitle = extractArticleTitle(subMainAr);
		logD("sub article title: \"" + subMainArTitle + "\"");
		if (debugPrintRawArticles) logD("subMainAr:" + subMainAr);
		if (getCurrentChapterTitle() != subMainArTitle) {
			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);