Miniflux automatically refresh feeds

Automatically refreshes Miniflux feeds

当前为 2023-04-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Miniflux automatically refresh feeds
  3. // @namespace https://reader.miniflux.app/
  4. // @version 5
  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. // @grant none
  10. // ==/UserScript==
  11.  
  12. const apiKey = '';
  13. const rateLimit = 600000; // time in miliseconds that must pass before it refreshes the feeds. 3600000 = 1 hour. 600000 = 10 minutes
  14.  
  15. const refreshFeeds = async () => {
  16. const earliestTime = await getLastUpdateTime();
  17. const now = new Date().getTime();
  18. if (await shouldUpdate(earliestTime, now)) {
  19. console.log('Miniflux: Time to refresh');
  20. localStorage.setItem('lastRefreshTime', now); // Save the new time. Might differ slightly from server time but not enough to matter.
  21. let res = await fetch('https://reader.miniflux.app/v1/feeds/refresh', {
  22. method: "PUT",
  23. headers: { 'X-Auth-Token': apiKey }
  24. });
  25. if (res.status == 204) { console.log('Successfully started refresh on all feeds.'); } else { console.log('Error, Miniflux did not return a 204 status.'); }
  26. } else { console.log('Miniflux: Not time to refresh yet.'); }
  27. }
  28.  
  29. const shouldUpdate = async (earliestTime, now) => {
  30. let shouldUpdate = false;
  31. 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.
  32. if (difference > rateLimit) { shouldUpdate = true; }
  33. return shouldUpdate;
  34. }
  35.  
  36. const getLastUpdateTime = async () => { // This relies on the calling function to update localStorage if it's time for an update.
  37. let earliestTime = localStorage.getItem('lastRefreshTime'); // Try to get time from localstorage.
  38. if (earliestTime == null) { // If it's not in localstorage, get the time from the API.
  39. let req = await fetch('https://reader.miniflux.app/v1/feeds', { headers: { 'X-Auth-Token': apiKey } });
  40. let res = JSON.parse(await req.text());
  41. let timesArray = res.map(currentFeed => Date.parse(currentFeed.checked_at)); // returns an array of each feed's checked_at.
  42. earliestTime = Math.min(...timesArray); // Instead of comparing in a For loop, let Math.min return the lowest value. That's the earliest time.
  43. }
  44. return earliestTime;
  45. }
  46.  
  47. // run once when the page is loaded.
  48. refreshFeeds();