Scheduled Website Opener (Daily Check - Config Page)

Opens websites at scheduled times (checks once daily), config via separate page.

  1. // ==UserScript==
  2. // @name Scheduled Website Opener (Daily Check - Config Page)
  3. // @namespace your-namespace
  4. // @version 0.3
  5. // @description Opens websites at scheduled times (checks once daily), config via separate page.
  6. // @author You
  7. // @match *://*/*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const SCHEDULE_KEY = 'scheduledWebsitesDaily';
  17. const LAST_CHECK_KEY = 'lastScheduleCheck';
  18.  
  19. function loadSchedules() {
  20. const storedSchedules = GM_getValue(SCHEDULE_KEY);
  21. return storedSchedules ? JSON.parse(storedSchedules) : [];
  22. }
  23.  
  24. function getLastCheck() {
  25. return GM_getValue(LAST_CHECK_KEY, 0);
  26. }
  27.  
  28. function setLastCheck() {
  29. GM_setValue(LAST_CHECK_KEY, Date.now());
  30. }
  31.  
  32. function formatTime(date) {
  33. const hours = String(date.getHours()).padStart(2, '0');
  34. const minutes = String(date.getMinutes()).padStart(2, '0');
  35. return `${hours}:${minutes}`;
  36. }
  37.  
  38. function checkSchedules() {
  39. const now = new Date();
  40. const currentTime = formatTime(now);
  41. const currentDay = now.getDay(); // 0 (Sunday) to 6 (Saturday)
  42. const schedules = loadSchedules();
  43.  
  44. schedules.forEach(schedule => {
  45. if (schedule.days.includes(currentDay) && schedule.time === currentTime) {
  46. window.open(schedule.url, '_blank');
  47. // Optionally, you could remove the schedule after it's executed once:
  48. // GM_setValue(SCHEDULE_KEY, JSON.stringify(schedules.filter(s => s !== schedule)));
  49. }
  50. });
  51. setLastCheck();
  52. setTimeout(checkDaily, 24 * 60 * 60 * 1000);
  53. }
  54.  
  55. function checkDaily() {
  56. const now = new Date();
  57. const lastCheck = getLastCheck();
  58. const timeSinceLastCheck = now.getTime() - lastCheck;
  59.  
  60. if (timeSinceLastCheck >= 24 * 60 * 60 * 1000) {
  61. checkSchedules();
  62. } else {
  63. setTimeout(checkDaily, (24 * 60 * 60 * 1000) - timeSinceLastCheck);
  64. }
  65. }
  66.  
  67. // Start the daily check interval
  68. checkDaily();
  69.  
  70. })();