├── MIT-LICENSE.txt ├── README.md ├── XTab.js ├── bower.json └── test ├── .gitignore ├── index.htm └── package.json /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright © 2012—2014 StreetStrider. 2 | https://github.com/StreetStrider 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XTab 2 | Browser cross-tab events library. 3 | 4 | Allow to emit events that will be dispatched to all other tabs from that domain opened. 5 | 6 | ## install 7 | ```bash 8 | $ bower install StreetStrider/XTab 9 | ``` 10 | 11 | ```html 12 | 13 | ``` 14 | 15 | ## usage 16 | ### in basic 17 | ```javascript 18 | XTab.on('meow', function handler (cat) { ... }); 19 | XTab.once('meow', function onceHandler (onlyCat) { ... }); 20 | XTab.emit('meow', 'Boris'); 21 | ``` 22 | 23 | ### multiple handlers 24 | ```javascript 25 | function handler1 () { ... }; 26 | function handler2 () { ... }; 27 | function handler3 () { ... }; 28 | XTab.on('meow', handler1); 29 | XTab.on('meow', handler2); 30 | XTab.on('meow', handler3); 31 | ``` 32 | 33 | Handlers will be triggered in straight order of assignment. 34 | 35 | ### removing handlers 36 | ```javascript 37 | XTab.off('meow', handler1); // removes only handler1 38 | XTab.off('meow'); // removes all handlers for meow event 39 | XTab.off(); // removes all handlers 40 | ``` 41 | 42 | ### once handlers 43 | ```javascript 44 | XTab.once('woff', function run () { ... }); // triggered only once 45 | XTab.emit('woff'); 46 | XTab.emit('woff'); // no effect 47 | ``` 48 | 49 | ## license 50 | MIT. Copyright © 2012 – 2014 StreetStrider. 51 | -------------------------------------------------------------------------------- /XTab.js: -------------------------------------------------------------------------------- 1 | /** 2 | * XTab 3 | * Cross-tab events library. 4 | **/ 5 | 6 | XTab = (function (window, localStorage) { 7 | 8 | var 9 | prefix = 'XTab.', 10 | handlers = {}, 11 | XTab = {}; 12 | 13 | XTab.on = function (eventName, handler) 14 | { 15 | createHandlers(eventName); 16 | 17 | handlers[eventName].push(handler); 18 | }; 19 | 20 | XTab.once = function (eventName, handler) 21 | { 22 | XTab.on(eventName, handler); 23 | XTab.on(eventName, remover); 24 | 25 | function remover () 26 | { 27 | XTab.off(eventName, handler); 28 | XTab.off(eventName, remover); 29 | } 30 | }; 31 | 32 | XTab.off = function (eventName, handler) 33 | { 34 | if (arguments.length) 35 | { 36 | if (arguments.length > 1) 37 | { 38 | createHandlers(eventName); 39 | 40 | var index = handlers[eventName].indexOf(handler); 41 | if (~ index) 42 | { 43 | handlers[eventName].splice(index, 1); 44 | } 45 | } else 46 | { 47 | handlers[eventName] = []; 48 | } 49 | } else 50 | { 51 | handlers = {}; 52 | } 53 | }; 54 | 55 | function createHandlers (eventName) 56 | { 57 | handlers[eventName] || (handlers[eventName] = []); 58 | } 59 | 60 | XTab.emit = function (eventName, value) 61 | { 62 | var data = {}; 63 | if (arguments.length > 1) 64 | { 65 | data.value = value; 66 | } 67 | data.timemark = +new Date; 68 | 69 | localStorage.setItem(prefix + eventName, JSON.stringify(data)); 70 | }; 71 | 72 | window.addEventListener('storage', function (e) 73 | { 74 | if (e.key.substr(0, prefix.length) === prefix) 75 | { 76 | var eventName = e.key.substr(prefix.length); 77 | 78 | if (handlers[eventName] && handlers[eventName].length) 79 | { 80 | var data = JSON.parse(e.newValue); 81 | 82 | handlers[eventName].slice() 83 | .forEach(function (handler) 84 | { 85 | /* conform arity */ 86 | if (data.value) 87 | { 88 | handler.call(XTab, data.value); 89 | } 90 | else 91 | { 92 | handler.call(XTab); 93 | } 94 | }); 95 | } 96 | } 97 | }); 98 | 99 | return XTab; 100 | 101 | })(window, localStorage); 102 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "XTab", 3 | "version": "2.0.1", 4 | "description": "browser cross-tab events library", 5 | 6 | "keywords": [ "cross-tab", "tabs", "xtab", "browser", "javascript", "event", "events" ], 7 | 8 | "authors": [ "StreetStrider " ], 9 | "homepage": "https://github.com/StreetStrider/XTab", 10 | 11 | "license": "MIT", 12 | 13 | "main": "XTab.js", 14 | "ignore": 15 | [ 16 | "test" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /test/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | node_modules 3 | -------------------------------------------------------------------------------- /test/index.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | XTab :: test 6 | 7 | 8 | 9 | 10 | 12 | 13 | 33 | -------------------------------------------------------------------------------- /test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xtab-test", 3 | "version": "0.0.0", 4 | 5 | "dependencies": 6 | { 7 | "httpster": "latest" 8 | }, 9 | 10 | "scripts": 11 | { 12 | "start": "httpster --dir .." 13 | } 14 | } 15 | --------------------------------------------------------------------------------