SweClockers JavaScript sidladdare

Lägger till en knapp som läser in kommande inlägg utan uppdatering av sidan.

Versione datata 16/05/2015. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         SweClockers JavaScript sidladdare
// @namespace    hAPsdWxok4bTePK8JZdG
// @author       LemonIllusion
// @version      1.0.3
// @match        http://www.sweclockers.com/forum/trad/*
// @match        http://www.sweclockers.com/forum/post/*
// @description  Lägger till en knapp som läser in kommande inlägg utan uppdatering av sidan.
// ==/UserScript==

/**
 * Get HTML asynchronously
 * @param  {String}   url      The URL to get HTML from
 * @param  {Function} callback A callback funtion. Pass in "response" variable to use returned HTML.
 * Source: http://gomakethings.com/getting-html-asynchronously-from-another-page/
 */
var getHTML = function ( url, callback ) {

    // Feature detection
    if ( !window.XMLHttpRequest ) return;

    // Create new request
    var xhr = new XMLHttpRequest();

    // Setup callback
    xhr.onload = function() {
        if ( callback && typeof( callback ) === 'function' ) {
            callback( this.responseXML );
        }
    }

    // Get the HTML
    xhr.open( 'GET', url );
    xhr.responseType = 'document';
    xhr.send();

};

// Kolla om det finns fler sidor
if (document.getElementsByClassName("next-page")[0] !== undefined) {
    var nextExists = true;
    var nextURL = document.getElementsByClassName("next-page")[0].href;
    console.log("SweClockers JavaScript sidladdare: Det finns fler sidor att läsa in.");
    document.getElementsByClassName("forumPosts")[0].innerHTML += '<div id="jsLoadPage" class="forumPost"><div class="postHeader table"><div class="row"><div class="cell headerLink" style="text-align:center;cursor:pointer">Ladda fler</div></div></div></div>'
    document.getElementById("jsLoadPage").onclick = loadPage;
} else {
    var nextExists = false;
    document.getElementsByClassName("forumPosts")[0].innerHTML += '<div class="forumPost"><div class="postHeader table"><div class="row"><div class="cell headerLink" style="text-align:center">Det finns inga fler inlägg att ladda</div></div></div></div>'
    console.log("SweClockers JavaScript sidladdare: Det finns inga fler sidor att läsa in.");
}

function loadPage () {
    if (nextExists) { // Om det finns fler sidor
        document.getElementById("jsLoadPage").getElementsByClassName("cell")[0].style.cssText = "text-align:center";
        document.getElementById("jsLoadPage").getElementsByClassName("cell")[0].innerHTML = "Laddar...";
        document.getElementById("jsLoadPage").onclick = "";
        getHTML( nextURL, function (response) {
            document.getElementById("jsLoadPage").parentNode.removeChild(document.getElementById("jsLoadPage"));
            document.getElementsByClassName("forumPosts")[0].innerHTML += response.getElementsByClassName("forumPosts")[0].innerHTML; // Lägg till posts från nästa sida
            console.log("SweClockers JavaScript sidladdare: Nästa sida har lästs in");
            if (response.getElementsByClassName("next-page")[0] !== undefined) { // Kolla om det finns fler sidor efter den senast inlästa
                nextExists = true;
                nextURL = response.getElementsByClassName("next-page")[0].href;
                document.getElementsByClassName("forumPosts")[0].innerHTML += '<div id="jsLoadPage" class="forumPost"><div class="postHeader table"><div class="row"><div class="cell headerLink" style="text-align:center;cursor:pointer">Ladda fler</div></div></div></div>'
                document.getElementById("jsLoadPage").onclick = loadPage;
                console.log("SweClockers JavaScript sidladdare: Det finns fler sidor att läsa in.");
            } else {
                nextExists = false;
                document.getElementsByClassName("forumPosts")[0].innerHTML += '<div class="forumPost"><div class="postHeader table"><div class="row"><div class="cell headerLink" style="text-align:center">Det finns inga fler inlägg att ladda</div></div></div></div>'
                console.log("SweClockers JavaScript sidladdare: Det finns inga fler sidor att läsa in.");
            }
        });
    } else {
        console.log("SweClockers JavaScript sidladdare: Sista sidan har redan laddats.");
    }
}