Advertisement
gavin19

Night timer

Dec 3rd, 2011
347
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:00',
  9.             description: "Start time for night mode. Example, 21:00 for 9pm."
  10.         },
  11.         endTime: {
  12.             type: 'text',
  13.             value: '06:00',
  14.             description: "End time for night mode. Example, 06:00 for 6am."
  15.         },
  16.         checkInterval: {
  17.             type: 'text',
  18.             value: '15',
  19.             description: "The interval, in minutes, to check for day/night changeover."
  20.         },
  21.         autoDisableStyles: {
  22.             type: 'boolean',
  23.             value: false,
  24.             description: "Disable subreddit styles automatically upon switching to Night Mode"
  25.         }
  26.     },
  27.     description: 'Automatically turn night mode on/off at the specified times.',
  28.     isEnabled: function() {
  29.         return RESConsole.getModulePrefs(this.moduleID);
  30.     },
  31.     include: Array(/https?:\/\/([a-z]+).reddit.com\/[\?]*/i),
  32.     isMatchURL: function() {
  33.         return RESUtils.isMatchURL(this.moduleID);
  34.     },
  35.     go: function() {
  36.         if ((this.isEnabled()) && (this.isMatchURL())) {
  37.             if ((RESUtils.getOptions('styleTweaks').lightSwitch.value === true) && ((document.querySelector('#lightSwitchToggle')))) {
  38.                 var delayInterval = +((this.options.checkInterval.value) * 60000),
  39.                     ignoredSubredditsValue = false;
  40.                 if (modules['nightTimer'].options.autoDisableStyles.value === true) {
  41.                     var thisSubreddit = window.location.href.split('/')[4],
  42.                         ignoredSubreddits = modules['styleTweaks'].ignoredSubReddits;
  43.                     for (var i in ignoredSubreddits) {
  44.                         if (ignoredSubreddits[i] === thisSubreddit) {
  45.                             ignoredSubredditsValue = true;
  46.                             break;
  47.                         }
  48.                     }
  49.                 }
  50.                 this.setMode(ignoredSubredditsValue);
  51.                 window.setInterval(function() {
  52.                     modules['nightTimer'].setMode(ignoredSubredditsValue)
  53.                 }, delayInterval);
  54.             }
  55.         }
  56.     },
  57.     setMode: function(ignoredSubredditsValue) {
  58.         var theStyle, theSwitch = document.querySelector('#lightSwitchToggle');
  59.         if (document.querySelector('input[name*="subRedditStyleCheckbox"]')) {
  60.             theStyle = document.querySelector('input[name*="subRedditStyleCheckbox"]')
  61.         }
  62.         else {
  63.             theStyle = false;
  64.         }
  65.         if ((modules['nightTimer'].checkTime()) && (!theSwitch.className.match(/enabled/))) {
  66.             RESUtils.click(modules['styleTweaks'].lightSwitch);
  67.             if (modules['nightTimer'].options.autoDisableStyles.value === true && theStyle) {
  68.                 if (theStyle.checked === true) theStyle.click();
  69.             }
  70.         }
  71.         else if ((!modules['nightTimer'].checkTime()) && (theSwitch.className.match(/enabled/))) {
  72.             RESUtils.click(modules['styleTweaks'].lightSwitch);
  73.             if (modules['nightTimer'].options.autoDisableStyles.value === true && ignoredSubredditsValue === false) {
  74.             if(theStyle) theStyle.click();
  75.         }
  76.         }
  77.     },
  78.     checkTime: function() {
  79.         var nowMins = new Date().getMinutes(),
  80.             nowHrs = new Date().getHours();
  81.         var start = this.options.startTime.value.split(':'),
  82.             end = this.options.endTime.value.split(':');
  83.         for (var i = 0; i < start.length; i += 1) {
  84.             start[i] = +start[i];
  85.             end[i] = +end[i];
  86.         }
  87.         if ((((start[0] < end[0]) && (nowHrs >= start[0] && nowHrs <= end[0]))
  88.         || ((start[0] > end[0]) && (nowHrs <= start[0] || nowHrs >= end[0])))
  89.         && ((nowHrs === start[0] && nowMins >= start[1]) || (nowHrs === end[0] && nowMins <= end[1]) || (nowHrs > start[0] && nowHrs < end[0]))
  90.         || ((start[0] === end[0]) && ((nowMins >= start[1]) && (nowMins <= end[1])))) {
  91.             return true;
  92.         }
  93.         else {
  94.             return false;
  95.         }
  96.     }
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement