Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "sysend", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Send messages to other tabs/windows in the same origin and browser", | ||
@@ -5,0 +5,0 @@ "main": "sysend.js", |
@@ -0,1 +1,4 @@ | ||
[![npm](https://img.shields.io/badge/npm-1.2.0-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.2.0-yellow.svg) | ||
## sysend.js | ||
@@ -5,9 +8,13 @@ | ||
open in the same browser. They need to be in same domain. The library don't use | ||
any dependencies and use HTML5 LocalStorage API. You can send any object that | ||
can be serialized to JSON or just send empty notification. | ||
any dependencies and use HTML5 LocalStorage API or BroadcastChannel API. | ||
If your browser don't support BroadcastChannel (see [Can I Use](https://caniuse.com/#feat=broadcastchannel)) | ||
then you can send any object that can be serialized to JSON with BroadcastChannel you can send any object | ||
(it will not be serialized to string). You can also send empty notification. | ||
Tested on: | ||
GNU/Linux: in Chromium 34, FireFox 29, Opera 12.16 (64bit)<br/> | ||
Windows 10 64bit: in IE11 and Edge 38 | ||
Windows 10 64bit: in IE11 and Edge 38, Chrome 56, Firefox 51<br/> | ||
MacOS X El Captain: Safari 9, Chrome 56, Firefox 51 | ||
@@ -29,2 +36,5 @@ ## Instalation | ||
you can also get it from [unpkg.com CDN](https://unpkg.com/sysend) | ||
## Usage | ||
@@ -49,3 +59,3 @@ | ||
The demo also use iframe proxy to send message to different domain | ||
The demo also use iframe proxy to send message to different domain (on firefox you need to add CORS for the iframe see [Cross-Domain LocalStorage](https://jcubic.wordpress.com/2014/06/20/cross-domain-localstorage/)) | ||
@@ -62,3 +72,3 @@ ## API | ||
Copyright (C) 2014 [Jakub Jankiewicz](http://jcubic.pl)<br/> | ||
Copyright (C) 2014-2017 [Jakub Jankiewicz](http://jcubic.pl)<br/> | ||
Released under the [MIT license](https://opensource.org/licenses/MIT) | ||
@@ -65,0 +75,0 @@ |
/**@license | ||
* sysend.js - send messages between browser windows/tabs | ||
* Copyright (C) 2014 Jakub Jankiewicz <http://jcubic.pl> | ||
* sysend.js - send messages between browser windows/tabs version 1.2.0 | ||
* | ||
* Copyright (C) 2014-2018 Jakub Jankiewicz <http://jcubic.pl/me> | ||
* Released under the MIT license | ||
* | ||
* The idea for this implementation came from this StackOverflow question: | ||
* The idea for localStorage implementation came from this StackOverflow question: | ||
* http://stackoverflow.com/q/24182409/387194 | ||
* | ||
*/ | ||
/* global define, module, exports, localStorage, setTimeout */ | ||
(function (root, factory) { | ||
@@ -62,27 +64,47 @@ if (typeof define === 'function' && define.amd) { | ||
var callbacks = {}; | ||
window.addEventListener('storage', function(e) { | ||
if (e.key.match(re)) { | ||
var key = e.key.replace(re, ''); | ||
if (callbacks[key]) { | ||
var value = e.newValue || get(key); | ||
if (value != random_value) { | ||
var obj = JSON.parse(value); | ||
if (obj && obj[1] != random_value) { | ||
// don't call on remove | ||
callbacks[key].forEach(function(fn) { | ||
fn(obj[2], key); | ||
}); | ||
var index = 0; | ||
var channel; | ||
if (typeof window.BroadcastChannel === 'function') { | ||
channel = new window.BroadcastChannel(uniq_prefix); | ||
channel.addEventListener('message', function(event) { | ||
if (event.target.name === uniq_prefix) { | ||
var key = event.data && event.data.name; | ||
if (callbacks[key]) { | ||
callbacks[key].forEach(function(fn) { | ||
fn(event.data.data, key); | ||
}); | ||
} | ||
} | ||
}); | ||
} else { | ||
window.addEventListener('storage', function(e) { | ||
// prevent event to be executed on remove in IE | ||
if (e.key.match(re) && index++ % 2 === 0) { | ||
var key = e.key.replace(re, ''); | ||
if (callbacks[key]) { | ||
var value = e.newValue || get(key); | ||
if (value && value != random_value) { | ||
var obj = JSON.parse(value); | ||
if (obj && obj[1] != random_value) { | ||
// don't call on remove | ||
callbacks[key].forEach(function(fn) { | ||
fn(obj[2], key); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
origin_page = false; | ||
}, false); | ||
}, false); | ||
} | ||
return { | ||
broadcast: function(event, message) { | ||
set(event, to_json(message)); | ||
// clean up localstorage | ||
setTimeout(function() { | ||
remove(event); | ||
}, 0); | ||
if (channel) { | ||
channel.postMessage({name: event, data: message}); | ||
} else { | ||
set(event, to_json(message)); | ||
// clean up localstorage | ||
setTimeout(function() { | ||
remove(event); | ||
}, 0); | ||
} | ||
}, | ||
@@ -89,0 +111,0 @@ on: function(event, fn) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
0
100
155
74
12676