Miniflux automatically refresh feeds

Automatically refreshes Miniflux feeds

Från och med 2023-04-26. Se den senaste versionen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Miniflux automatically refresh feeds
// @namespace    https://reader.miniflux.app/
// @version      5
// @description  Automatically refreshes Miniflux feeds
// @author       Tehhund
// @match        *://*.miniflux.app/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=miniflux.app
// @grant        none
// ==/UserScript==

const apiKey = '';
const rateLimit = 600000; // time in miliseconds that must pass before it refreshes the feeds. 3600000 = 1 hour. 600000 = 10 minutes

const refreshFeeds = async () => {
  const earliestTime = await getLastUpdateTime();
  const now = new Date().getTime();
  if (await shouldUpdate(earliestTime, now)) {
    console.log('Miniflux: Time to refresh');
    localStorage.setItem('lastRefreshTime', now); // Save the new time. Might differ slightly from server time but not enough to matter.
    let res = await fetch('https://reader.miniflux.app/v1/feeds/refresh', {
      method: "PUT",
      headers: { 'X-Auth-Token': apiKey }
    });
    if (res.status == 204) { console.log('Successfully started refresh on all feeds.'); } else { console.log('Error, Miniflux did not return a 204 status.'); }
  } else { console.log('Miniflux: Not time to refresh yet.'); }
}

const shouldUpdate = async (earliestTime, now) => {
  let shouldUpdate = false;
  let difference = now - earliestTime; // JS stores time like Unix but miliseconds since January 01, 1970 00:00:00 UTC instead of seconds, so this subtracts miliseconds from miliseconds.
  if (difference > rateLimit) { shouldUpdate = true; }
  return shouldUpdate;
}

const getLastUpdateTime = async () => { // This relies on the calling function to update localStorage if it's time for an update.
  let earliestTime = localStorage.getItem('lastRefreshTime'); // Try to get time from localstorage.
  if (earliestTime == null) { // If it's not in localstorage, get the time from the API.
    let req = await fetch('https://reader.miniflux.app/v1/feeds', { headers: { 'X-Auth-Token': apiKey } });
    let res = JSON.parse(await req.text());
    let timesArray = res.map(currentFeed => Date.parse(currentFeed.checked_at)); // returns an array of each feed's checked_at.
    earliestTime = Math.min(...timesArray); // Instead of comparing in a For loop, let Math.min return the lowest value. That's the earliest time.
  }
  return earliestTime;
}

// run once when the page is loaded.
refreshFeeds();