Chrono-reader

Przeglądanie powiadomień na wykop.pl w kolejności chronologicznej

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Chrono-reader
// @namespace    https://github.com/piomar123/
// @version      1.1.0
// @description  Przeglądanie powiadomień na wykop.pl w kolejności chronologicznej
// @author       piomar123
// @match        http://www.wykop.pl/*
// @match        https://www.wykop.pl/*
// @grant        none
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @name:en         Chrono-reader
// @description:en  Chronological tag notification reader for wykop.pl
// ==/UserScript==
// Badge bullet style by krejdd

var jq = $.noConflict();

jq('div#nav ul.clearfix:last > li.notification.m-tag').last().before(jq('<li/>', {
    id: 'chronoTag',
    html: '<a id="chronoTagButton" class="dropdown-show hashtag" href="#"><b id="chronoTagBullet" style="background:#0080FF; top:22px; right:16px; min-width:10px; width:10px; min-height:10px; height:10px; border-radius:5px; overflow:hidden;">&nbsp;</b>#</a><div class="notificationsContainer"></div>',
    class: "notification m-tag piomar-chronotag",
    title: "Najstarszy nieprzeczytany tag",
    alt: "Najstarszy nieprzeczytany tag"
}));

jq("#chronoTagButton").click(function(event){
    event.preventDefault();
    var tagiURL = "/powiadomienia/tagi/";
    var notificationSelector = '#content ul.menu-list li.type-light-warning a[href*="pl/wpis/"]';
    var emptyPageAlertSelector = "#content ul.menu-list li.type-alert p.empty";
    var noUnreadMessage = "Nie znalazłem nowszych wpisów";
    var maxPage = 30;

    var data = {
        unreadCount: 0,
        currentTotal: 0,
        oldestUrl: null
    };
    jq.ajax(tagiURL)
        .done(firstPageDownloaded(data))
        .fail(handleFail);

    function firstPageDownloaded(data){
        return function(html){
            html = parse(html);
            data.unreadCount = Number(jq('#hashtagsNotificationsCount', html).html());
            var notifications = jq(notificationSelector, html);
            data.currentTotal = notifications.length;
            data.oldestUrl = notifications.last().attr("href");
            if(data.currentTotal < data.unreadCount){
                downloadPage(data, 2);
                return;
            }
            if(data.unreadCount === 0){
                alert("Wszystko przeczytane");
                return;
            }
            if(!data.oldestUrl){
                alert(noUnreadMessage);
                return;
            }
            window.location.href = data.oldestUrl;
        };
    }

    function downloadPage(data, page){
        jq.ajax(tagiURL + "strona/" + page + "/")
            .done(nextPageDownloaded(data, page))
            .fail(handleFail);
    }

    function nextPageDownloaded(data, page){
        return function(html){
            html = parse(html);
            //data.unreadCount = Number($('#hashtagsNotificationsCount', html).html());
            var notifications = jq(notificationSelector, html);
            // possible race condition
            data.currentTotal += notifications.length;
            data.oldestUrl = notifications.last().attr("href") || data.oldestUrl;
            if(data.currentTotal < data.unreadCount && page < maxPage && checkPageNotEmpty(html)){
                downloadPage(data, page+1);
                return;
            }
            if(!data.oldestUrl){
                alert(noUnreadMessage);
                return;
            }
            window.location.href = data.oldestUrl;
        };
    }

    function parse(html){
        html = html.replace(/<img[^>]*>/g, "");
        return jq.parseHTML(html);
    }

    function checkPageNotEmpty(html){
        return jq(emptyPageAlertSelector, html).length === 0;
    }

    function handleFail(jqXHR, textStatus, err){
        alert(textStatus + ": " + err);
    }
});