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 3
  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. //
  11. // ==/UserScript==
  12.  
  13. const apiKey = '';
  14. const rateLimit = 300000; // time in miliseconds that must pass before it refreshes the feeds. 3600000 = 1 hour. 300000 = 5 minutes.
  15.  
  16. const refreshFeeds = async () => {
  17. if (await shouldUpdate()) {
  18. console.log('Miniflux: Time to refresh');
  19. let req = await fetch('https://reader.miniflux.app/v1/feeds/refresh', {
  20. method: "PUT",
  21. headers: {
  22. 'X-Auth-Token': apiKey
  23. }
  24. });
  25. let res = await req;
  26. if (res.status == 204) { console.log('Successfully started refresh on all feeds.'); } else { console.log('Error, Miniflux did not return a 204 status.'); }
  27. } else { console.log('Miniflux: Not time to refresh yet.'); }
  28. }
  29.  
  30. const shouldUpdate = async () => {
  31. let shouldUpdate = false;
  32. let jsTime = getLastUpdateTime();
  33. let now = new Date().getTime();
  34. let difference = now - jsTime; // JS stores time like Unix but miliseconds since January 01, 1970 00:00:00 UTC instead of seconds, so this subtracts miliseconds from miliseconds.
  35. if (difference > rateLimit) { shouldUpdate = true; }
  36. return shouldUpdate;
  37. }
  38.  
  39. const getLastUpdateTime = async () => {
  40. // Try to get time from localstorage.
  41. let earliestTime = localStorage.getItem('lastRefreshTime');
  42. // If it's not in localstorage, get time from feeds and store the earliest time.
  43. if (earliestTime == null) {
  44. let req = await fetch('https://reader.miniflux.app/v1/feeds', { headers: { 'X-Auth-Token': apiKey } });
  45. let res = JSON.parse(await req.text());
  46. let timesArray = res.map(currentFeed => Date.parse(currentFeed.checked_at)); // returns an array of each feed's checked_at.
  47. earliestTime = Math.min(...timesArray); // Instead of comparing in a For loop, let Math.min return the lowest value which is the earliest time.
  48. localStorage.setItem('lastRefreshTime', earliestTime);
  49. } else { console.log('lastRefreshTime was in localStorage: ' + earliestTime);}
  50. return earliestTime;
  51. }
  52.  
  53. // run once when the page is loaded.
  54. refreshFeeds();