Miniflux automatically refresh feeds

Automatically refreshes Miniflux feeds

Verzia zo dňa 17.07.2024. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         Miniflux automatically refresh feeds
// @namespace    https://reader.miniflux.app/
// @version      14
// @description  Automatically refreshes Miniflux feeds
// @author       Tehhund
// @match        *://*.miniflux.app/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=miniflux.app
// @run-at       document-start
// ==/UserScript==

let apiKey = ''; // Put your API key from Miniflux here.
const rateLimit = 43200000; // Only refresh twice per day. 43200000 miliseconds = 12 hours. If a feed has an error (e.g., too many requests) its checked_at datetime still gets updated so we won't hit the feeds with too many requests.

const refreshFeeds = async () => {
  if (!apiKey) { // If the API key isn't specified, try getting it from localstorage.
    apiKey = localStorage.getItem('miniFluxRefresherApiKey');
  } else { // If we have the API key, store it in localstorage.
    localStorage.setItem('miniFluxRefresherApiKey', apiKey);
  }
  let req = await fetch('https://reader.miniflux.app/v1/feeds', { headers: { 'X-Auth-Token': apiKey } });
  let res = JSON.parse(await req.text());
  let feedsArray = res.map(currentFeed => currentFeed);
  feedsArray.sort((a, b) => { return (new Date(a.checked_at).getTime() - new Date(b.checked_at).getTime())}); // Sort from least recently checked to most recently checked so least recent gets refreshed first.
  for (let [index, feed] of feedsArray.entries()) {
    let lastChecked = new Date(feed.checked_at).getTime();
    if (Date.now() - lastChecked > rateLimit) {
      console.log(`It's been more than 24 hours, refresh.`);
      setTimeout(
        async () => {
          let res = await fetch(`https://reader.miniflux.app/v1/feeds/${feed.id}/refresh`, {
            method: "PUT",
            headers: { 'X-Auth-Token': apiKey }
          });
          console.log(res);
          setToast(`Fetching ${feed.title} ${feed.site_url}`);
        }, 15000 * index); // Make a call every 15 seconds.
    } else console.log(`It's been less than 24 hours, do nothing.`);
  }
}

const setToast = (text) => {
  let newNode = document.createElement('div');
  newNode.id = `feedFetchStatus`;
  newNode.textContent = text;
  document.getElementsByClassName('items')[0].appendChild(newNode);
  setTimeout(() => { newNode.remove() }, 4000);
}

// run once when the page is loaded.
window.addEventListener("DOMContentLoaded", refreshFeeds);