Miniflux automatically refresh feeds

Automatically refreshes Miniflux feeds

目前為 2024-10-27 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Miniflux automatically refresh feeds
// @namespace    https://reader.miniflux.app/
// @version      21
// @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 () => {
  let toastDiv = document.createElement('div');
  toastDiv.id = 'toastDiv';
  toastDiv.style.marginBottom = "5rem";
  document.body.appendChild(toastDiv);
  setTimeout(() => { toastDiv.remove() }, 900000); // remove after 15 minutes.
  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); // Turn JSON into an array for sorting.
  feedsArray.sort((a, b) => { return (new Date(a.checked_at) - new Date(b.checked_at)) }); // 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 12 hours, refresh.`);
      setTimeout(
        async () => {
          let req = await fetch(`https://reader.miniflux.app/v1/feeds/${feed.id}`, { headers: { 'X-Auth-Token': apiKey } });
          let response = JSON.parse(await req.text());
          let lastChecked = new Date(response.checked_at).getTime();
          if (Date.now() - lastChecked > rateLimit) { // Since navigating, refreshing the page, or using another device could cause duplicate refreshes, double check that each feed still hasn't been refreshed recently.
            let newNode = setToast(`Fetching ${feed.title} ${feed.site_url}.`);
            let res = await fetch(`https://reader.miniflux.app/v1/feeds/${feed.id}/refresh`, {
              method: "PUT",
              headers: { 'X-Auth-Token': apiKey }
            });
            newNode.textContent += ` Complete.`;
            console.log(res);
          }
        }, 15000 * index); // Make a call every 15 seconds.}
    } else console.log(`It's been less than 12 hours, do nothing.`);
  }
}

const setToast = (text) => {
  let newNode = document.createElement('div');
  newNode.id = `feedFetchStatus`;
  newNode.textContent = text;
  document.getElementById('toastDiv').appendChild(newNode);
  setTimeout(() => { newNode.remove() }, 4000);
  return newNode;
}

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