Advertisement
gavin19

RES - Night mode timer

Oct 29th, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. modules['nightTimer'] = {
  2.     moduleID: 'nightTimer',
  3.     moduleName: 'Night Timer',
  4.     category: 'UI',
  5.     options: {
  6.         startTime: {
  7.             type: 'text',
  8.             value: [21],
  9.             description: "Start time for night mode (as an hour). Example, 21 for 9pm."
  10.         },
  11.         endTime: {
  12.             type: 'text',
  13.             value: [6],
  14.             description: "End time for night mode (as an hour). Example, 6 for 6am."
  15.         },
  16.         checkInterval: {
  17.             type: 'text',
  18.             value: [5],
  19.             description: "The interval, in minutes, to check for day/night changeover."
  20.         }
  21.     },
  22.     description: 'Automatically turn night mode on/off at the specified times.',
  23.     isEnabled: function() {
  24.         return RESConsole.getModulePrefs(this.moduleID);
  25.     },
  26.     include: new Array(/https?:\/\/([a-z]+).reddit.com\/[\?]*/i),
  27.     isMatchURL: function() {
  28.         return RESUtils.isMatchURL(this.moduleID);
  29.     },
  30.     flipSwitch: function() {
  31.         var now, theSwitch;
  32.         var start = +(this.options.startTime.value);
  33.         var end = +(this.options.endTime.value);
  34.         if (document.querySelector('#lightSwitchToggle')) {
  35.             theSwitch = document.querySelector('#lightSwitchToggle');
  36.             now = new Date().getHours();
  37.             if (start > end) {
  38.                 if (now >= start) {
  39.                     if (!theSwitch.className.match(/enabled/)) {
  40.                         RESUtils.click(modules['styleTweaks'].lightSwitch);
  41.                     }
  42.                 }
  43.                 if ((now < start) && (now >= end)) {
  44.                     if (theSwitch.className.match(/enabled/)) {
  45.                         RESUtils.click(modules['styleTweaks'].lightSwitch);
  46.                     }
  47.                 }
  48.             }
  49.             if (start < end) {
  50.                 if ((now >= start) && (now < end)) {
  51.                     if (!theSwitch.className.match(/enabled/)) {   
  52.                         RESUtils.click(modules['styleTweaks'].lightSwitch);
  53.                     }
  54.                 }
  55.                 if ((now > end) || (now < start)) {
  56.                     if (theSwitch.className.match(/enabled/)) {
  57.                         RESUtils.click(modules['styleTweaks'].lightSwitch);
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     },
  63.     go: function() {
  64.         if ((this.isEnabled()) && (this.isMatchURL())) {
  65.             if (RESUtils.getOptions('styleTweaks').lightSwitch.value === true) {
  66.                 var delay = +((this.options.checkInterval.value) * 60000);
  67.                 modules['nightTimer'].flipSwitch();
  68.                 window.setInterval(function checkTime() {
  69.                     modules['nightTimer'].flipSwitch();
  70.                 }, delay);
  71.             } //
  72.         }
  73.     }
  74. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement