Miniflux automatically refresh feeds

Automatically refreshes Miniflux feeds

当前为 2024-07-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Miniflux automatically refresh feeds
  3. // @namespace https://reader.miniflux.app/
  4. // @version 17
  5. // @description Automatically refreshes Miniflux feeds
  6. // @author Tehhund
  7. // @match *://*.miniflux.app/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=miniflux.app
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12.  
  13. let apiKey = ''; // Put your API key from Miniflux here.
  14. 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.
  15.  
  16. const refreshFeeds = async () => {
  17. let toastDiv = document.createElement('div');
  18. toastDiv.id='toastDiv';
  19. toastDiv.style.marginBottom = "5rem";
  20. document.body.appendChild(toastDiv);
  21. setTimeout(() => { toastDiv.remove() }, 900000); // remove after 15 minutes.
  22. if (!apiKey) { // If the API key isn't specified, try getting it from localstorage.
  23. apiKey = localStorage.getItem('miniFluxRefresherApiKey');
  24. } else { // If we have the API key, store it in localstorage.
  25. localStorage.setItem('miniFluxRefresherApiKey', apiKey);
  26. }
  27. let req = await fetch('https://reader.miniflux.app/v1/feeds', { headers: { 'X-Auth-Token': apiKey } });
  28. let res = JSON.parse(await req.text());
  29. let feedsArray = res.map(currentFeed => currentFeed);
  30. 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.
  31. for (let [index, feed] of feedsArray.entries()) {
  32. let lastChecked = new Date(feed.checked_at).getTime();
  33. if (Date.now() - lastChecked > rateLimit) {
  34. console.log(`It's been more than 24 hours, refresh.`);
  35. setTimeout(
  36. async () => {
  37. let res = await fetch(`https://reader.miniflux.app/v1/feeds/${feed.id}/refresh`, {
  38. method: "PUT",
  39. headers: { 'X-Auth-Token': apiKey }
  40. });
  41. console.log(res);
  42. setToast(`Fetching ${feed.title} ${feed.site_url}`);
  43. }, 15000 * index); // Make a call every 15 seconds.
  44. } else console.log(`It's been less than 24 hours, do nothing.`);
  45. }
  46. }
  47.  
  48. const setToast = (text) => {
  49. let newNode = document.createElement('div');
  50. newNode.id = `feedFetchStatus`;
  51. newNode.textContent = text;
  52. //document.getElementsByClassName('items')[0].appendChild(newNode);
  53. document.getElementById('toastDiv').appendChild(newNode);
  54. setTimeout(() => { newNode.remove() }, 4000);
  55. }
  56.  
  57. // run once when the page is loaded.
  58. window.addEventListener("DOMContentLoaded", refreshFeeds);