├── .gitignore ├── .jshintrc ├── Gruntfile.js ├── LICENSE.md ├── README.md ├── bower.json ├── demo ├── day.html ├── index.html └── styles │ ├── dark.css │ └── main.css ├── goodnight.js ├── goodnight.min.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | bower_components 4 | *.log -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "boss": true, 3 | "curly": true, 4 | "eqeqeq": true, 5 | "eqnull": true, 6 | "expr": true, 7 | "immed": true, 8 | "noarg": true, 9 | "onevar": true, 10 | "quotmark": "double", 11 | "smarttabs": true, 12 | "trailing": true, 13 | "unused": true, 14 | "node": true 15 | } -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.initConfig({ 3 | jshint: { 4 | files: ["goodnight.js", "Gruntfile.js"], 5 | options: { 6 | jshintrc: ".jshintrc" 7 | } 8 | }, 9 | 10 | uglify: { 11 | dist: { 12 | src: ["goodnight.js"], 13 | dest: "goodnight.min.js" 14 | }, 15 | }, 16 | 17 | watch: { 18 | files: ["goodnight.js", "demo/index.html"], 19 | tasks: ["jshint", "uglify"] 20 | } 21 | 22 | }); 23 | 24 | grunt.loadNpmTasks("grunt-contrib-jshint"); 25 | grunt.loadNpmTasks("grunt-contrib-uglify"); 26 | grunt.loadNpmTasks("grunt-contrib-watch"); 27 | 28 | grunt.registerTask("default", ["jshint", "uglify", "watch"]); 29 | }; 30 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jared Cubilla 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Goodnight 2 | 3 | A super small Javascript plugin for applying special CSS styles at night: https://jaredcubilla.github.io/goodnight/ 4 | 5 | ## Usage 6 | 7 | Download the `goodnight.min.js` file. Include it in your HTML 8 | 9 | ```html 10 | 11 | ``` 12 | 13 | `Goodnight.css(path)` adds a custom styles for you when it's nighttime. 14 | 15 | You can add as many styles as you want, and the files will be added if it's nighttime. 16 | 17 | ```js 18 | Goodnight.css('path/to/style.css'); 19 | Goodnight.css('path/to/another.css'); 20 | Goodnight.css('path/to/night.css'); 21 | ``` 22 | 23 | You can also specify the hours of which the dark styles are used. By default, your dark CSS file is activated from 6PM to midnight and midnight to 6AM, but you can specify the exact hours using the `Goodnight.AM` and `Goodnight.PM` variables. You should set the hours before adding any styles. 24 | 25 | ```js 26 | Goodnight.AM = 5 // (5AM) 27 | Goodnight.PM = 20 // (8PM) 28 | Goodnight.css('path/to/style.css') // this now activates dark styles before 5AM and after 8PM 29 | ``` 30 | 31 | If you want to provide easy way to revert back to light styles, you can call `Goodnight.toggle()`. For example you might create button for turning on and off these styles and call `Goodnight.toggle()` inside the event handler. 32 | 33 | ```js 34 | Goodnight.css('path/to/style.css'); 35 | 36 | document.querySelector('#some-button').addEventListener('click', function () { 37 | Goodnight.toggle(); // This removes styles from document.head 38 | }, false); 39 | ``` 40 | 41 | You can also append a specific class to the body at night. Simply use `Goodnight.class()` (`goodnight` is given if not specified). 42 | 43 | ```js 44 | Goodnight.class('my-night-class'); 45 | ``` 46 | 47 | ## Tips 48 | 49 | If you leave this script at the bottom of the ``, there will be a flash of daytime styles during nighttime, due to the browser processing the document until the script. It's best if you include the script in the ``. 50 | 51 | If you use [Bower](http://bower.io/), it is available as a package there too. Run the following... 52 | 53 | ``` 54 | bower install goodnight 55 | ``` 56 | 57 | ... and Goodnight should be installed as a bower package. 58 | 59 | If you're too lazy to download it, you could always just do this: 60 | 61 | ```html 62 | 63 | ``` 64 | 65 | ## Contributing 66 | 67 | If something's wrong or needs changing, feel free to leave issues at Github's issue tracker. If you're feeling generous, please send pull requests for fixing bugs or adding cool new stuff. 68 | 69 | ## License 70 | 71 | Licensed under MIT. Created by [Jared Cubilla](https://github.com/JaredCubilla). 72 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "goodnight", 3 | "version": "0.2.3", 4 | "description": "Apply CSS styles depending on date or time.", 5 | "keywords": ["goodnight", "css", "styles", "plugin", "night", "date", "time"], 6 | 7 | "main": "goodnight.js", 8 | "ignore": [ 9 | ".jshintrc", 10 | "Gruntfile.js", 11 | "README.md", 12 | "demo/" 13 | ], 14 | 15 | "repository": { 16 | "type": "git", 17 | "url": "git://github.com/JaredCubilla/goodnight.git" 18 | }, 19 | 20 | "license": "MIT" 21 | } -------------------------------------------------------------------------------- /demo/day.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Goodnight Demo - Day 6 | 7 | 8 | 19 | 20 | 21 |
22 |

Night theme should show between 06:00 -> 18:00.

23 |

Click anywhere to toggle styles

24 | 25 | 26 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Goodnight Demo 6 | 7 | 8 | 13 | 14 | 15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /demo/styles/dark.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #2c3e50; 3 | color: #ecf0f1; 4 | } -------------------------------------------------------------------------------- /demo/styles/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #ecf0f1; 3 | } -------------------------------------------------------------------------------- /goodnight.js: -------------------------------------------------------------------------------- 1 | (function (g, ns) { 2 | "use strict"; 3 | 4 | function gn() { 5 | var Goodnight = { AM: 6, PM: 18 }, hours = new Date().getHours(), ln = []; 6 | 7 | Goodnight.night = function () { 8 | return hours < this.PM ? hours < this.AM ? true : false : true; 9 | }; 10 | 11 | Goodnight.css = function (path) { 12 | if (!path) { 13 | return; 14 | } 15 | 16 | var item = document.createElement("link"); 17 | item.rel = "stylesheet"; 18 | item.href = path; 19 | ln.push(item); 20 | 21 | if (this.night()) { 22 | document.documentElement.firstChild.appendChild(item); 23 | } 24 | }; 25 | 26 | Goodnight.class = function (cssClass) { 27 | if (this.night()) { 28 | document.body.className += " " + (cssClass || "goodnight"); 29 | } 30 | }; 31 | 32 | Goodnight.toggle = function () { 33 | for (var i = 0; i < ln.length; i++) { 34 | if (ln[i].parentNode) { 35 | ln[i].parentNode.removeChild(ln[i]); 36 | } else { 37 | document.documentElement.firstChild.appendChild(ln[i]); 38 | } 39 | } 40 | }; 41 | 42 | return Goodnight; 43 | } 44 | 45 | g[ns] = gn(); 46 | })(window, "Goodnight"); 47 | -------------------------------------------------------------------------------- /goodnight.min.js: -------------------------------------------------------------------------------- 1 | !function(a,b){"use strict";function c(){var a={AM:6,PM:18},b=(new Date).getHours(),c=[];return a.night=function(){return b