├── .gitignore ├── index.js ├── README.md ├── package.json └── assets └── plugin.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | *.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | website: { 3 | assets: './assets', 4 | js: [ 5 | 'plugin.js' 6 | ] 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gitbook AutoTheme Plugin 2 | 3 | [![npm](https://img.shields.io/npm/v/gitbook-plugin-autotheme.svg?style=plastic)](https://npmjs.org/package/gitbook-plugin-autotheme) [![npm](https://img.shields.io/npm/dm/gitbook-plugin-autotheme.svg?style=plastic)](https://npmjs.org/package/gitbook-plugin-autotheme) [![npm](https://img.shields.io/npm/dt/gitbook-plugin-autotheme.svg?style=plastic)](https://npmjs.org/package/gitbook-plugin-autotheme) 4 | 5 | Config: 6 | 7 | ```json 8 | { 9 | "plugins": ["autotheme"], 10 | "pluginsConfig": { 11 | "autotheme": { 12 | "white": [9, 10, 11, 12, 13, 14, 15, 16], 13 | "sepia": [6, 7, 8, 17, 18, 19], 14 | "night": [20, 21, 22, 23, 0, 1, 2, 3, 4, 5] 15 | } 16 | } 17 | } 18 | ``` 19 | 20 | 演示: 21 | 22 | 如果喜欢,请打赏 23 | 24 | ## LICENSE 25 | 26 | MIT 27 | 28 | Alipay Donation(通过支付宝捐赠): 29 | 30 | ![qr](https://cloud.githubusercontent.com/assets/1890238/15489630/fccbb9cc-2193-11e6-9fed-b93c59d6ef37.png) 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gitbook-plugin-autotheme", 3 | "version": "1.0.1", 4 | "description": "Gitbook 自动切换主题插件", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/willin/gitbook-plugin-autotheme.git" 9 | }, 10 | "keywords": ["gitbook", "plugin", "auto", "theme", "change"], 11 | "author": "Willin Wang", 12 | "license": "MIT", 13 | "bugs": { 14 | "url": "https://github.com/willin/gitbook-plugin-autotheme/issues" 15 | }, 16 | "homepage": "https://github.com/willin/gitbook-plugin-autotheme#readme", 17 | "engines": { 18 | "gitbook": "*" 19 | }, 20 | "gitbook": { 21 | "properties": { 22 | "white": { 23 | "type": "array", 24 | "title": "White Hours", 25 | "required": false, 26 | "default": [9, 10, 11, 12, 13, 14, 15, 16] 27 | }, 28 | "sepia": { 29 | "type": "array", 30 | "title": "Sepia Hours", 31 | "required": false, 32 | "default": [6, 7, 8, 17, 18, 19] 33 | }, 34 | "night": { 35 | "type": "array", 36 | "title": "Night Hours", 37 | "required": false, 38 | "default": [20, 21, 22, 23, 0, 1, 2, 3, 4, 5] 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /assets/plugin.js: -------------------------------------------------------------------------------- 1 | require(['gitbook'], function (gitbook) { 2 | (function () { 3 | if (typeof window.Element === "undefined" || "classList" in document.documentElement) return; 4 | 5 | var prototype = Array.prototype, 6 | push = prototype.push, 7 | splice = prototype.splice, 8 | join = prototype.join; 9 | 10 | function DOMTokenList(el) { 11 | this.el = el; 12 | // The className needs to be trimmed and split on whitespace 13 | // to retrieve a list of classes. 14 | var classes = el.className.replace(/^\s+|\s+$/g,'').split(/\s+/); 15 | for (var i = 0; i < classes.length; i++) { 16 | push.call(this, classes[i]); 17 | } 18 | }; 19 | 20 | DOMTokenList.prototype = { 21 | add: function(token) { 22 | if(this.contains(token)) return; 23 | push.call(this, token); 24 | this.el.className = this.toString(); 25 | }, 26 | contains: function(token) { 27 | return this.el.className.indexOf(token) != -1; 28 | }, 29 | item: function(index) { 30 | return this[index] || null; 31 | }, 32 | remove: function(token) { 33 | if (!this.contains(token)) return; 34 | for (var i = 0; i < this.length; i++) { 35 | if (this[i] == token) break; 36 | } 37 | splice.call(this, i, 1); 38 | this.el.className = this.toString(); 39 | }, 40 | toString: function() { 41 | return join.call(this, ' '); 42 | }, 43 | toggle: function(token) { 44 | if (!this.contains(token)) { 45 | this.add(token); 46 | } else { 47 | this.remove(token); 48 | } 49 | 50 | return this.contains(token); 51 | } 52 | }; 53 | 54 | window.DOMTokenList = DOMTokenList; 55 | 56 | function defineElementGetter (obj, prop, getter) { 57 | if (Object.defineProperty) { 58 | Object.defineProperty(obj, prop,{ 59 | get : getter 60 | }); 61 | } else { 62 | obj.__defineGetter__(prop, getter); 63 | } 64 | } 65 | 66 | defineElementGetter(Element.prototype, 'classList', function () { 67 | return new DOMTokenList(this); 68 | }); 69 | 70 | })(); 71 | 72 | var white; 73 | var sepia; 74 | var night; 75 | var classes = ['color-theme-1', 'color-theme-2']; 76 | 77 | function toggleClass(className) { 78 | var div = document.getElementsByTagName('div')[0]; 79 | for (var i = 0; i < classes.length; i++){ 80 | if (classes[i] === className) { 81 | div.classList.add(className); 82 | } else { 83 | div.classList.remove(classes[i]); 84 | } 85 | } 86 | }; 87 | 88 | function changeTheme() { 89 | var now = new Date(); 90 | var hour = now.getHours(); 91 | if (white.indexOf(hour) > -1) { 92 | toggleClass('color-theme-0'); 93 | } else if (sepia.indexOf(hour) > -1) { 94 | toggleClass('color-theme-1'); 95 | } else if (night.indexOf(hour) > -1) { 96 | toggleClass('color-theme-2'); 97 | } 98 | }; 99 | gitbook.events.bind('start', function (e, config) { 100 | white = config.autotheme.white || [9, 10, 11, 12, 13, 14, 15, 16]; 101 | sepia = config.autotheme.sepia || [6, 7, 8, 17, 18, 19]; 102 | night = config.autotheme.night || [20, 21, 22, 23, 0, 1, 2, 3, 4, 5]; 103 | changeTheme(); 104 | }); 105 | 106 | gitbook.events.bind('page.change', function() { 107 | changeTheme(); 108 | }); 109 | }); 110 | --------------------------------------------------------------------------------