dogdriphelper / dogdrip helper

// ==UserScript==
// @name         dogdrip helper
// @namespace    http://tampermonkey.net/
// @updateURL    https://openuserjs.org/meta/dogdriphelper/dogdrip_helper.meta.js
// @version      0.3.7
// @description  try to take over the world!
// @author       You
// @match        https://www.dogdrip.net/*
// @icon         https://www.google.com/s2/favicons?domain=dogdrip.net
// @grant        none
// @license MIT
// ==/UserScript==
(function($) {
    'use strict';

    class nextButton {
        constructor() {
            this.button = $('<a class="eq button-scroll-tool-item" />');
            this.setNormal();
        }

        click( fn ) {
            this.button.on('click', fn);
        }

        getElement() {
            return this.button;
        }

        set text( value ) {
            this.value = value;
            this.button.text(this.value);
        }

        setNormal() {
            this.text = '다음글';
        }

        setTimeout(fn, timeout) {
            fn();
            let $this = this;
            setTimeout(function() { $this.setNormal(); }, timeout||500)
        }
    }

    var viewed_list;
    var ONextButton = new nextButton();
    var originalPage;
    var processingPage;
    var nextPageTimeout;

    function reload_viewed() {
        viewed_list = (localStorage.getItem('viewed') || '').split(',').map(e=>parseInt(e.match(/\d+/)[0])).filter(e => !!e);

        if (viewed_list.length > 1000) {
            viewed_list.splice(0, viewed_list.length - 1000);
        }
    }

    function viewd(view_id) {
        return viewed_list.includes(view_id);
    }

    function not_viewed_click() {
        reload_viewed();
        let isWidget = !!$(this).parents('.xe-widget-wrapper').length;
        let article_list;
        if(isWidget) {
            article_list = $(this).parents('.xe-widget-wrapper').eq(0).find('ul li');
        } else {
            article_list = $(this).parents('main#main').find('tr:not(.notice) td.title');
        }
        let count = 0;
        let not_viewed_list = getNotViewed(article_list);
        not_viewed_list.forEach((href) => {
            window.open('javascript:setTimeout(function(){ location.href="'+href+'"; }, '+((not_viewed_list.length - count++) * 400)+')', '_blank');
        });
    }

    function getNotViewed( article_list ) {
        let not_viewed_list = [];
        article_list.each(function() {
            let url = new URL($(this).find('a').last().attr('href'));
            let article_code = getArticleNumber(url);
            if (!viewd(article_code)) {
                not_viewed_list.push(url.href);
            }
        });
        return not_viewed_list;
    }

    function getArticleNumber(url) {
        try {
            if(url) {
                return parseInt(((/\/(\d+)/g).exec(url)??(/document_srl=(\d+)/g).exec(url))[1]);
            } else {
                return parseInt(((/\/(\d+)/g).exec(location.pathname)??(/document_srl=(\d+)/g).exec(location.search))[1]);
            }
        } catch(e) {
            return false;
        }
    }

    function isListPage() {
        return !!$('.board-list').length;
    }
    function isViewPage() {
        return !!getArticleNumber();
    }

    function getNextPage() {
        let midLink = $('.link-primary.text-bold._module_title').attr('href');
        return $.get(midLink + '?page=' + (processingPage++));

    }

    var currentProcessingListPage = $('body');
    function clickNextArticle( e ) {
        if(nextPageTimeout) {
            clearTimeout(nextPageTimeout);
            nextPageTimeout = 0;
            ONextButton.setTimeout(function() {
                ONextButton.text = '취소됨';
            });
            processingPage = originalPage;
            return;
        }
        let article_list = $('main#main', currentProcessingListPage).find('tr:not(.notice) td.title');
        let notViewed = getNotViewed(article_list);
        if(!notViewed.length) {
            ONextButton.text = (processingPage)+'페이지에서 찾는중...';
            nextPageTimeout = setTimeout(() => {
                getNextPage().then(body => {
                    nextPageTimeout = 0;
                    currentProcessingListPage = $(body);
                    clickNextArticle();
                });
            }, 1000);
            return;
        } else {
            ONextButton.text = '이동중...';
            nextPageTimeout = 0;
            location.href = notViewed[0];
        }
    }

    function installNextArticle(target) {
        ONextButton.click(clickNextArticle);
        target.append(ONextButton.getElement());
    }

    reload_viewed();
    if (isViewPage()) { // 게시글
        let article_code = getArticleNumber();
        if (!viewd(article_code)) {
            viewed_list.push(article_code);
            localStorage.setItem('viewed', viewed_list.toString());
        }
        installNextArticle($('.button-scroll-tool-box'));

    }

    if(isListPage() || isViewPage() ) {
        originalPage = processingPage = parseInt($.trim($('.pagination.pagewide li.active').last().text()));
        $('body').keydown(function(e) {
            e = e || window.event;
            switch(e.target.tagName) {
                case "TEXTAREA":
                case "INPUT":
                case "VIDEO":
                    return;  // do not process event at all
            }
            switch(e.keyCode) {
                case 39:
                    clickNextArticle();
                    break;
                case 37:
                    history.back();
                    break;
                case 68:
                    $(document).scrollTop($(document).scrollTop() + 500);
                    break;
                case 85:
                    $(document).scrollTop($(document).scrollTop() - 500);
                    break;
                case 71:
                    $('.btn_voted').click();
                    break;
            }
        });
    }
    let btn = $('<button />');
    btn.text('안본거열기');
    btn.click(not_viewed_click);
    $('.xe-widget-wrapper a.eq.link').after(btn);
    $('.link-primary.text-bold._module_title').after(btn);
})($);