Qidian AdBlock

Discussion in 'Tech Discussion' started by noisypixy, Aug 1, 2017.

  1. noisypixy

    noisypixy Sacatunn que pen, que summum que tun.

    Joined:
    Jun 25, 2016
    Messages:
    716
    Likes Received:
    950
    Reading List:
    Link
    Update 2018-06-05 21:13 UTC: Fixed by @ekojsalim, check out https://forum.novelupdates.com/posts/4111892/

    ---


    So, I just made this GreaseMonkey script during my work break. It should allow you to bypass Qidian's forced video ads.

    It keeps running all the time, so it should work even if new chapters are loaded on scrolling.

    Once it detects an ad-wall, it blurs the text a bit while it's loading, and after a few seconds it should return to normal with the full content of the chapter.

    This does not play any ads under the hood; the delay is because Qidian generates a "token" specific to that chapter but it takes a while to become active.

    Code:
    // ==UserScript==
    // @name        webnovel.com skip video ads
    // @namespace   http://forum.novelupdates.com/
    // @version     4
    // @run-at      document-end
    // @match       http://webnovel.com/book/*
    // @match       https://webnovel.com/book/*
    // @match       http://www.webnovel.com/book/*
    // @match       https://www.webnovel.com/book/*
    // ==/UserScript==
    
    //------------------------------------------------------------------------------
    // This script is released to the public domain. No attribution is required.
    //------------------------------------------------------------------------------
    
    // How frequently this script should check for new chapters.
    //
    // The amount is in milliseconds.
    const INTERVAL_CHAPTER_CHECK = 1000;
    
    // When a token is not ready yet, this is how much time we should wait
    // before trying again.
    //
    // The amount is in milliseconds.
    const INTERVAL_TOKEN_CHECK = 1000;
    
    /**
     * Check for new chapters and try to remove the adwall from them.
     */
    function main() {
      Array.from(
        // Locked chapters.
        document.querySelectorAll('.cha-content._lock')
      ).forEach((lock) => {
        // Remove this class so this chapter won't be processed the next time
        // `main` is called.
        lock.classList.remove('_lock');
    
        // Remove the video.
        const v = lock.closest('.chapter_content').querySelector('.lock-video');
        if (v) {
          v.remove();
        }
    
        // Element with the chapter content.
        const contentElement = lock.querySelector('.cha-words');
    
        contentElement.style.opacity = '0.1';
    
        // Get the ID for the series ("book").
        //
        // Some chapters have the `data-bid` property, but not all of them.
        // That's why it's better to just get this from the URL.
        const bid = window.location.href.split('/book/')[1].split('/')[0];
    
        // Get the ID for the chapter.
        const { cid } = lock.querySelector('[data-cid]').dataset;
    
        // Both ID are required.
        if (!bid || !cid) {
          return;
        }
    
        return fetch(
          `https://www.webnovel.com/apiajax/chapter/GetChapterContentToken?bookId=${bid}&chapterId=${cid}`
        )
          .then(resp => resp.json())
          .then(data => data.data.token)
          .then(token => encodeURIComponent(token))
          .then(token => new Promise((resolve) => {
            // The raw body of the chapter.
            //
            // It will be plain text, so we must manually build the HTML for it.
            let content = '';
    
            // Try to get the content of the chapter, and fulfill the promise once
            // we have it.
            //
            // This function will retry until it succeeds.
            function tick() {
              const url = `https://www.webnovel.com/apiajax/chapter/GetChapterContentByToken?token=${token}`;
    
              fetch(url)
                .then(resp => resp.json())
                .then((data) => {
                  content = data.data.content.trim();
    
                  if (content) {
                    resolve(content);
                  } else {
                    setTimeout(tick, INTERVAL_TOKEN_CHECK);
                  }
                })
                .catch((err) => {
                  console.error(err.stack);
    
                  tick();
                });
            }
    
            tick();
          }))
          .then((content) => {
            // Build the HTML for the chapter content.
            //
            // For now we only split on line breaks and wrap each piece
            // with "<p></p>" tags.
            const chapterHtml = content
              .split('\n')
              .map(p => p.trim())
              .filter(p => !!p)
              .map(p => `<p>${p}</p>`)
              .join('');
    
            // Update the chapter content and turn opacity back to 100%.
            contentElement.innerHTML = chapterHtml;
            contentElement.style.opacity = '1';
          })
          .catch((err) => {
            console.error(err.stack);
          });
      });
    }
    
    // Since Qidian may load new chapters without refreshing the page, we must
    // continuously check for new chapters in the page.
    setInterval(main, INTERVAL_CHAPTER_CHECK);
    
    Enjoy.

    P.S.: I'm not taking part on this WW vs QI bullshit drama. I'm completely neutral and only made this script because it's fun.

    Update 2017-08-25 14:48 UTC: Added more "@match" lines as per @mrttao comment.
    Update 2017-08-03 22:52 UTC: Added comments. Script is now explicitly on the public domain.
    Update 2018-06-04 18:14 UTC: Apparently this doesn't work anymore (based on the latest posts on this thread as of this moment). RIP. I don't even like CN, so I probably won't fix it unless I get really bored again.
     
    Last edited: Jun 5, 2018
    Bazikiller, Wick, Astaroth and 122 others like this.
  2. PewDiePie

    PewDiePie King of YouTube

    Joined:
    Mar 28, 2017
    Messages:
    114
    Likes Received:
    309
    Reading List:
    Link
    God bless you and your dog.
     
    gauthamc, Bazikiller, GonZ555 and 5 others like this.
  3. Valentino

    Valentino Well-Known Member

    Joined:
    Mar 8, 2016
    Messages:
    7
    Likes Received:
    14
    Reading List:
    Link
    Not all heroes wear capes.....
     
  4. AFZ3643

    AFZ3643 Well-Known Member

    Joined:
    Dec 27, 2015
    Messages:
    53
    Likes Received:
    30
    Reading List:
    Link
    How do I use this script
     
  5. TeaNinja

    TeaNinja [Chuunikage of the Hidden Teapot Village]

    Joined:
    Jul 26, 2017
    Messages:
    275
    Likes Received:
    573
    Reading List:
    Link
    Holy shit, you must be bored as fuck.
     
  6. zentetsuken

    zentetsuken Well-Known Member

    Joined:
    May 16, 2016
    Messages:
    258
    Likes Received:
    192
    Reading List:
    Link
    OP clearly lying, this script was made to show dem godly skills. So any & all of you that still wanna read QI, y'all better worship OP!

    Press F to worship @noisypixy
     
  7. Danis

    Danis {(Harem Sect Leader)}{Trickster}&{Lurker}

    Joined:
    Jan 2, 2016
    Messages:
    4,127
    Likes Received:
    2,864
    Reading List:
    Link
    wew look like paywall is soon............................
     
    Wujigege likes this.
  8. PewDiePie

    PewDiePie King of YouTube

    Joined:
    Mar 28, 2017
    Messages:
    114
    Likes Received:
    309
    Reading List:
    Link
    *presses F*
     
    gauthamc, Pyoo and Tramsloof like this.
  9. inclement

    inclement Well-Known Member

    Joined:
    Dec 1, 2015
    Messages:
    14
    Likes Received:
    5
    Reading List:
    Link
    You are an immortal level cultivator !
    Thank you!
     
  10. oliver

    oliver Well-Known Member

    Joined:
    May 14, 2016
    Messages:
    1,392
    Likes Received:
    1,854
    Reading List:
    Link
    Yea, let's make a script to bypass the 15 seconds skippable ads so they'll make a paywall sooner.
     
  11. noisypixy

    noisypixy Sacatunn que pen, que summum que tun.

    Joined:
    Jun 25, 2016
    Messages:
    716
    Likes Received:
    950
    Reading List:
    Link
    First install GreaseMonkey (it's "TamperMonkey" on Chrome, but I don't use Chrome).

    Then copy the script.

    Then go to your Firefox addons, at the left you'll have the "GreaseMonkey" tab.

    Then click on "New user script"
    [​IMG]

    Then "Use script from clipboard"
    [​IMG]

    And it's done. You'll probably get a popup window, but you can safely close it.
     
  12. Oblivion

    Oblivion Well-Known Member

    Joined:
    Jun 27, 2016
    Messages:
    85
    Likes Received:
    37
    Reading List:
    Link
    Spoken like a true man. Your profile pic also doesn't have a cape yet he's a hero! :D
     
    Valentino likes this.
  13. Pertiest

    Pertiest Active Member

    Joined:
    Dec 7, 2016
    Messages:
    11
    Likes Received:
    5
    Reading List:
    Link
    Tx, mate
     
  14. Digix

    Digix Owl-sama Follower

    Joined:
    Mar 12, 2017
    Messages:
    1,191
    Likes Received:
    1,539
    Reading List:
    Link
    Script works 10/10
     
  15. noisypixy

    noisypixy Sacatunn que pen, que summum que tun.

    Joined:
    Jun 25, 2016
    Messages:
    716
    Likes Received:
    950
    Reading List:
    Link
    Not really. If WW or any group pulls something similar I'd do the same, as long as it's interesting to program or I'm bored.

    I don't even like Chinese novels (only read like 3-4).
     
    Aurega, zeus, thymee and 6 others like this.
  16. Ahmed_kashf

    Ahmed_kashf Well-Known Member

    Joined:
    Nov 2, 2016
    Messages:
    57
    Likes Received:
    21
    Reading List:
    Link
    face smake: level9999
     
  17. zentetsuken

    zentetsuken Well-Known Member

    Joined:
    May 16, 2016
    Messages:
    258
    Likes Received:
    192
    Reading List:
    Link
    OP is like a real life Dr. House. I'm so skilled, I only do interesting stuff or if I'm bored! All the more reason to worship OP.

    Well jokes aside, great works on the script! Absolutely well done! & this is coming from non QI reader. :aww::hmm::aww:
     
    Indiboy likes this.
  18. PewDiePie

    PewDiePie King of YouTube

    Joined:
    Mar 28, 2017
    Messages:
    114
    Likes Received:
    309
    Reading List:
    Link
  19. joeglens

    joeglens Well-Known Member

    Joined:
    Oct 24, 2015
    Messages:
    136
    Likes Received:
    120
    Reading List:
    Link
    Is the delay 15 seconds? Because thats how long it takes even if the ad is only 8 seconds long
     
  20. WarStalkeR

    WarStalkeR Well-Known Member

    Joined:
    Feb 18, 2017
    Messages:
    73
    Likes Received:
    59
    Reading List:
    Link
    Well, he's probably one of these security experts "Hey look, they added something, let's bypass it, its legal anyway" kind and hence the result. Well, now I don't need to write it at very least :D