|
| 1 | +/* |
| 2 | + * nano Browser plugin v1.0 |
| 3 | + * http://nanojs.org/plugins/browser |
| 4 | + * |
| 5 | + * Copyright (c) 2008-2015 James Watts |
| 6 | + * https://github.com/jameswatts |
| 7 | + * |
| 8 | + * This is FREE software, licensed under the GPL |
| 9 | + * http://www.gnu.org/licenses/gpl.html |
| 10 | + */ |
| 11 | + |
| 12 | +if (nano) { |
| 13 | + nano.plugin({}, function() { |
| 14 | + this.browser = { |
| 15 | + agent: null, |
| 16 | + engine: null, |
| 17 | + version: null, |
| 18 | + script: 0, |
| 19 | + cookie: false, |
| 20 | + platform: null, |
| 21 | + x: 0, |
| 22 | + y: 0, |
| 23 | + w: 0, |
| 24 | + h: 0, |
| 25 | + page: { |
| 26 | + x: 0, |
| 27 | + y: 0, |
| 28 | + w: 0, |
| 29 | + h: 0 |
| 30 | + }, |
| 31 | + event: null, |
| 32 | + button: null, |
| 33 | + key: null, |
| 34 | + mouse: function _mouse(e) { |
| 35 | + e = window.event || e; |
| 36 | + nano.browser.event = e; |
| 37 | + nano.browser.x = e.clientX; |
| 38 | + nano.browser.y = e.clientY; |
| 39 | + if ((nano.isset(e.which) && e.which === 1) || e.button === 0) { |
| 40 | + nano.browser.button = 'left'; |
| 41 | + } else if ((nano.isset(e.which) && e.which === 2) || e.button === 2) { |
| 42 | + nano.browser.button = 'right'; |
| 43 | + } else if ((nano.isset(e.which) && e.which === 4) || e.button === 1) { |
| 44 | + nano.browser.button = 'middle'; |
| 45 | + } else { |
| 46 | + nano.browser.button = 'other'; |
| 47 | + } |
| 48 | + }, |
| 49 | + keyboard: function _keyboard(e) { |
| 50 | + e = window.event || e; |
| 51 | + nano.browser.event = e; |
| 52 | + nano.browser.key = (nano.isset(e.which))? e.which : e.keyCode; |
| 53 | + }, |
| 54 | + screen: function _screen() { |
| 55 | + if (typeof window.innerWidth === 'number') { |
| 56 | + nano.browser.w = window.innerWidth; |
| 57 | + nano.browser.h = window.innerHeight; |
| 58 | + } else if (nano.isset(document.documentElement) && typeof document.documentElement.clientWidth === 'number') { |
| 59 | + nano.browser.w = document.documentElement.clientWidth; |
| 60 | + nano.browser.h = document.documentElement.clientHeight; |
| 61 | + } |
| 62 | + nano.browser.page.w = document.body.scrollWidth; |
| 63 | + nano.browser.page.h = document.body.scrollHeight; |
| 64 | + }, |
| 65 | + scroll: function _scroll() { |
| 66 | + if (typeof window.pageYOffset == 'number') { |
| 67 | + nano.browser.page.x = window.pageXOffset; |
| 68 | + nano.browser.page.y = window.pageYOffset; |
| 69 | + } else if (document.body && typeof document.body.scrollTop === 'number') { |
| 70 | + nano.browser.page.x = document.body.scrollLeft; |
| 71 | + nano.browser.page.y = document.body.scrollTop; |
| 72 | + } else if (document.documentElement && typeof document.documentElement.scrollTop === 'number') { |
| 73 | + nano.browser.page.x = document.documentElement.scrollLeft; |
| 74 | + nano.browser.page.y = document.documentElement.scrollTop; |
| 75 | + } |
| 76 | + }, |
| 77 | + detect: function _detect() { |
| 78 | + if (/firefox/i.test(navigator.userAgent)) { |
| 79 | + this.agent = 'firefox'; |
| 80 | + this.engine = 'gecko'; |
| 81 | + } else if (/seamonkey/i.test(navigator.userAgent)) { |
| 82 | + this.agent = 'seamonkey'; |
| 83 | + this.engine = 'gecko'; |
| 84 | + } else if (/flock/i.test(navigator.userAgent)) { |
| 85 | + this.agent = 'flock'; |
| 86 | + this.engine = 'gecko'; |
| 87 | + } else if (/chrome/i.test(navigator.userAgent)) { |
| 88 | + this.agent = 'chrome'; |
| 89 | + this.engine = 'webkit'; |
| 90 | + } else if (/camino/i.test(navigator.userAgent)) { |
| 91 | + this.agent = 'camino'; |
| 92 | + this.engine = 'gecko'; |
| 93 | + } else if (/apple/i.test(navigator.vendor)) { |
| 94 | + this.agent = 'safari'; |
| 95 | + this.engine = 'webkit'; |
| 96 | + } else if (window.opera) { |
| 97 | + this.agent = 'opera'; |
| 98 | + this.engine = 'presto'; |
| 99 | + } else if (/kde/i.test(navigator.vendor)) { |
| 100 | + this.agent = 'konqueror'; |
| 101 | + this.engine = 'khtml'; |
| 102 | + } else if (/MSIE/i.test(navigator.userAgent)) { |
| 103 | + this.agent = 'msie'; |
| 104 | + this.engine = 'trident'; |
| 105 | + } |
| 106 | + if (this.engine === 'gecko') { |
| 107 | + /firefox[\/\s](\d+\.\d+)/i.test(navigator.userAgent); |
| 108 | + this.version = RegExp.$1; |
| 109 | + } else if (this.engine === 'presto') { |
| 110 | + /opera[\/\s](\d+\.\d+)/i.test(navigator.userAgent); |
| 111 | + this.version = RegExp.$1; |
| 112 | + } else if (this.agent === 'chrome') { |
| 113 | + /chrome[\/\s](\d+\.\d+)/i.test(navigator.userAgent); |
| 114 | + this.version = RegExp.$1; |
| 115 | + } else if (this.agent === 'safari') { |
| 116 | + /version[\/\s](\d+\.\d+)/i.test(navigator.userAgent); |
| 117 | + this.version = RegExp.$1; |
| 118 | + } else if (this.engine === 'khtml') { |
| 119 | + /khtml[\/\s](\d+\.\d+)/i.test(navigator.userAgent); |
| 120 | + this.version = RegExp.$1; |
| 121 | + } else if (this.engine === 'trident') { |
| 122 | + /msie(\d+\.\d+)/i.test(navigator.userAgent); |
| 123 | + this.version = RegExp.$1; |
| 124 | + } |
| 125 | + if (/linux/i.test(navigator.platform)) { |
| 126 | + this.platform = 'linux'; |
| 127 | + } else if (/freebsd/i.test(navigator.platform)) { |
| 128 | + this.platform = 'freebsd'; |
| 129 | + } else if (/openbsd/i.test(navigator.platform)) { |
| 130 | + this.platform = 'openbsd'; |
| 131 | + } else if (/openvms/i.test(navigator.platform)) { |
| 132 | + this.platform = 'openvms'; |
| 133 | + } else if (/irix/i.test(navigator.platform)) { |
| 134 | + this.platform = 'irix'; |
| 135 | + } else if (/hp\-ux/i.test(navigator.platform)) { |
| 136 | + this.platform = 'hp-ux'; |
| 137 | + } else if (/unix/i.test(navigator.platform) || /x11/i.test(navigator.platform)) { |
| 138 | + this.platform = 'unix'; |
| 139 | + } else if (/mac/i.test(navigator.platform)) { |
| 140 | + this.platform = 'mac'; |
| 141 | + } else if (/sunos/i.test(navigator.platform)) { |
| 142 | + this.platform = 'sunos'; |
| 143 | + } else if (/win/i.test(navigator.platform)) { |
| 144 | + this.platform = 'windows'; |
| 145 | + } else if (/palmos/i.test(navigator.platform)) { |
| 146 | + this.platform = 'palm'; |
| 147 | + } else if (/symbian/i.test(navigator.platform)) { |
| 148 | + this.platform = 'symbian'; |
| 149 | + } |
| 150 | + if (this.agent === 'msie' && (this.version > 2 && this.version < 4)) { |
| 151 | + this.script = 1.0; |
| 152 | + } else if (this.agent === 'msie' && (this.version > 3 && this.version < 5)) { |
| 153 | + this.script = 1.2; |
| 154 | + } else if (this.agent === 'msie' && this.platform !== 'mac' && this.version > 4) { |
| 155 | + this.script = 1.3; |
| 156 | + } else if (this.agent === 'msie' && this.platform === 'mac' && this.version > 4) { |
| 157 | + this.script = 1.4; |
| 158 | + } else if (this.agent === 'opera' && this.version < 5) { |
| 159 | + this.script = 1.1; |
| 160 | + } else if (this.agent === 'opera' && (this.version > 4 || this.version < 7)) { |
| 161 | + this.script = 1.3; |
| 162 | + } else if (this.agent === 'opera' && this.version > 6) { |
| 163 | + this.script = 1.5; |
| 164 | + } else if (this.engine === 'khtml') { |
| 165 | + this.script = 1.5; |
| 166 | + } else if (this.engine === 'gecko' && this.version < 1.5) { |
| 167 | + this.script = 1.5; |
| 168 | + } else if (this.engine === 'gecko' && (this.version > 1 && this.version < 2)) { |
| 169 | + this.script = 1.6; |
| 170 | + } else if (this.engine === 'gecko' && (this.version > 1.5 && this.version < 3)) { |
| 171 | + this.script = 1.7; |
| 172 | + } else if (this.engine === 'gecko' && (this.version > 2 && this.version < 4)) { |
| 173 | + this.script = 1.8; |
| 174 | + } else if (this.agent === 'chrome') { |
| 175 | + this.script = 1.7; |
| 176 | + } else if (this.agent === 'safari' && (this.version > 2 && this.version < 3.2)) { |
| 177 | + this.script = 1.6; |
| 178 | + } else if (this.agent === 'safari' && this.version > 3.1) { |
| 179 | + this.script = 1.7; |
| 180 | + } |
| 181 | + if (navigator.cookieEnabled) this.cookie = true; |
| 182 | + if (document.addEventListener) { |
| 183 | + document.addEventListener('mousemove', nano.browser.mouse, true); |
| 184 | + document.addEventListener('mousedown', nano.browser.mouse, true); |
| 185 | + document.addEventListener('keydown', nano.browser.keyboard, true); |
| 186 | + window.addEventListener('resize', nano.browser.screen, true); |
| 187 | + window.addEventListener('scroll', nano.browser.scroll, true); |
| 188 | + } else if (document.attachEvent) { |
| 189 | + document.attachEvent('onmousemove', nano.browser.mouse); |
| 190 | + document.attachEvent('onmousedown', nano.browser.mouse); |
| 191 | + document.attachEvent('onkeydown', nano.browser.keyboard); |
| 192 | + window.attachEvent('onresize', nano.browser.screen); |
| 193 | + window.attachEvent('onscroll', nano.browser.scroll); |
| 194 | + } else { |
| 195 | + document.onmousemove = function(e) { |
| 196 | + nano.browser.mouse(e); |
| 197 | + }; |
| 198 | + document.onmousedown = function(e) { |
| 199 | + nano.browser.mouse(e); |
| 200 | + }; |
| 201 | + document.onkeydown = function(e) { |
| 202 | + nano.browser.keyboard(e); |
| 203 | + }; |
| 204 | + window.onresize = function(e) { |
| 205 | + nano.browser.screen(e); |
| 206 | + }; |
| 207 | + window.onscroll = function(e) { |
| 208 | + nano.browser.scroll(e); |
| 209 | + }; |
| 210 | + } |
| 211 | + } |
| 212 | + }; |
| 213 | + this.browser.detect(); |
| 214 | + nano(function() { |
| 215 | + nano.browser.screen(); |
| 216 | + nano.browser.scroll(); |
| 217 | + }); |
| 218 | + }); |
| 219 | +} |
0 commit comments