Пятничный трекер

b989b3cb950f7c6d6990e451e66205b9
(function () {
    function injectTracker() {

        const MAX_TRACKS = 10;

        // To add the tracker as a first block on the right sidebar, change "false" to "true"
        const insertAsFirstBlock = false;

        // set "true" if you want to tract articles only with unseen comments
        const onlyWithUnseenComments = false;

        // styling
        const trackerBlockCss = ``;

        const deleteSvg = '';
        const refeshSvg = '';

        const oldTrackerBlock = animateRefreshIcon();

        fetch('https://habr.com/ru/tracker/')
            .then(response => response.text())
            .then(response => processTracker(response));

        function processTracker(response) {
            const m = response.match(/]+id="tracker_feed_form"[^>]*>(.*?)<\/form>/s);
            if (!m)
                return;

            const d = document.querySelector('.sidebar_right');
            if (!d)
                return;

            oldTrackerBlock?.parentNode.removeChild(oldTrackerBlock);

            const parseDiv = document.createElement("div");
            parseDiv.innerHTML = m[1];

            const newDiv = addNewElement(d, 'div', 'default-block default-block_sidebar block-tracker', trackerBlockCss, insertAsFirstBlock);

            const headerDiv = addNewElement(newDiv, 'div', 'default-block__header', '

Tracker ' + refeshSvg + '

'); headerDiv.querySelector('.tracker-refresh-btn').addEventListener('click', injectTracker); const newContent = addNewElement(newDiv, 'div', 'default-block__content'); const newUl = addNewElement(newContent, 'ul', 'content-list content-list_most-read'); let currentTrack = 0; [...parseDiv.querySelectorAll('table.tracker-table tr')].filter(el => { if (currentTrack >= MAX_TRACKS) return false; currentTrack += addTrack(el, newUl); }); if (currentTrack === 0) addNewElement(newContent, 'div', '', onlyWithUnseenComments ? 'No articles with new comments' : 'No articles to track'); parseDiv.remove(); } function addTrack(el, newUl) { let firstTd = el.querySelector('td:nth-child(1)'); if (!firstTd) return 0; const trackerHtml = el.querySelector('td:nth-child(3)')?.innerHTML; if (onlyWithUnseenComments && !trackerHtml?.includes('+')) return 0; const newLi = addNewElement(newUl, 'li', 'content-list__item content-list__item_devided post-info'); const newTopDiv = addNewElement(newLi, 'div', 'tracker-stats-info'); addNewElement(newTopDiv, 'span', 'post_author', firstTd?.innerHTML.replaceAll('${deleteSvg}`); newPost.querySelector('.remove-tracker').addEventListener('click', removeTracker); return 1; } function removeTracker(event) { const arctileEl = event.currentTarget.parentNode.querySelector('* > a'); if (!arctileEl) return; const m = arctileEl.getAttribute('href')?.match(/\/([0-9]+)\//); if (!m) return; const postId = m[1]; event.currentTarget.classList.add('icon-anim'); fetch('https://habr.com/json/tracker/feed/remove/', { method: 'POST', body: `post[${postId}]=on`, headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }).then(() => injectTracker()); } function addNewElement(parentElement, tageName, className, html, makeAsFirstChild) { const newEl = document.createElement(tageName); if (makeAsFirstChild) parentElement.insertBefore(newEl, parentElement.firstChild); else parentElement.appendChild(newEl); if (className) newEl.className = className; if (html) newEl.innerHTML = html; return newEl; } function animateRefreshIcon() { let oldTrackerBlock = document.querySelector('div.block-tracker'); if (oldTrackerBlock) document.querySelector('.tracker-refresh-icon')?.classList.add('icon-anim'); return oldTrackerBlock; } } injectTracker(); })();

© Habrahabr.ru