mwalcott3 / Mobile Webnovel.com Ad Bypass

// ==UserScript==
// @namespace     https://openuserjs.org/users/mwalcott3
// @name          Mobile Webnovel.com Ad Bypass
// @description   Testing out a method to bypass ads on webnovel.com
// @copyright     2017, mwalcott3 (https://openuserjs.org/users/mwalcott3)
// @license       MIT; https://opensource.org/licenses/MIT
// @version       0.0.2
// @match         *://m.webnovel.com/book/*/*
// @run-at        document-end
// @grant none
// ==/UserScript==

// ==OpenUserJS==
// @author mwalcott3
// ==/OpenUserJS==

(function() {
    //'use strict';

    // TODO error handling and better formatting for the non rich text content
    function bypassLock() {
        [...document.querySelectorAll('div.cha-txt-hide')].map( (node) => node.parentNode ).forEach( (el)=>{
            let csrfToken = /_csrfToken=([^;]+)/.exec(document.cookie)[1];
            // TODO bookId only needs to be found once. Move it outside the loop.
            let bookId = document.getElementById('readContent').dataset.reportCbid;
            let chapterId = el.dataset.chapterid;
            let url = 'https://m.webnovel.com/ajax/chapter/GetChapterContentToken?_csrfToken=' + csrfToken +'&bookId=' + bookId + '&chapterId=' + chapterId;
            console.log(url);

            el.querySelector('div.cha-txt-hide').classList.remove('cha-txt-hide');
            el.querySelector('div.cha-watch-ad').remove();
            el.querySelector('div.cha-txt').innerHTML = "";

            fetch(url).then( (response) => response.json() ).then( (result)=> {
                console.log(result);
                let token = result.data.token;
                let url = 'https://m.webnovel.com/ajax/chapter/GetChapterContentByToken?_csrfToken=' + csrfToken + '&token=' + token;
                console.log(url);
                setTimeout( () => {
                    fetch(url).then((response) => response.json() ).then( (result)=> {
                        console.log(result);
                        if (result.data.isRichFormat) {
                            el.querySelector('div.cha-txt').innerHTML = result.data.content;
                        } else {
                            // TODO: Do this with ptags and a docFrag instead
                            // Split then map
                            el.querySelector('div.cha-txt').innerHTML = result.data.content.replace(/\r?\n/g, '<p></p>');
                        }
                    });
                },3500);

            });
        });
    }

    let target = document.getElementById('readContent');
    console.log(target);

    let observer = new MutationObserver(function(mutations) {
        bypassLock();
    });

    bypassLock();
    observer.observe(target, {childList: true});

})();