Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "sysend", | ||
"version": "1.2.0", | ||
"description": "Send messages to other tabs/windows in the same origin and browser", | ||
"version": "1.3.0", | ||
"description": "Send messages to other tabs/windows in the same browser", | ||
"main": "sysend.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,3 +0,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) | ||
[![npm](https://img.shields.io/badge/npm-1.3.0-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.3.0-yellow.svg) | ||
![downloads](https://img.shields.io/npm/dt/sysend.svg) | ||
@@ -7,3 +8,3 @@ ## sysend.js | ||
sysend.js is small library that allow to send message between pages that are | ||
open in the same browser. They need to be in same domain. The library don't use | ||
open in the same browser. It also support Cross-Domain comunication. The library don't use | ||
any dependencies and use HTML5 LocalStorage API or BroadcastChannel API. | ||
@@ -36,5 +37,9 @@ If your browser don't support BroadcastChannel (see [Can I Use](https://caniuse.com/#feat=broadcastchannel)) | ||
you can also get it from [unpkg.com CDN](https://unpkg.com/sysend) | ||
you can also get it from unpkg.com CDN: | ||
``` | ||
https://unpkg.com/sysend | ||
``` | ||
## Usage | ||
@@ -55,8 +60,15 @@ | ||
If you want to add support for Cross-Domain communiation you need to call proxy method with target domain: | ||
```javascript | ||
sysend.proxy('https://jcubic.pl'); | ||
sysend.proxy('https://terminal.jcubic.pl'); | ||
``` | ||
on firefox you need to add CORS for the iframe (see [Cross-Domain LocalStorage](https://jcubic.wordpress.com/2014/06/20/cross-domain-localstorage/)) | ||
## Demo | ||
Open this [demo page](http://jcubic.pl/sysend.php) in two tabs/windows | ||
Open this [demo page](http://jcubic.pl/sysend.php) in two tabs/windows. | ||
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/)) | ||
## API | ||
@@ -66,9 +78,10 @@ | ||
* on(name, callback) - callback(object, name) - add event of specified name | ||
* on(name, callback) - callback(object, name) - add event of specified name | ||
* off(name [, callback]) - remove callback | ||
* broadcast(name [, object]) - send object and fire all events with specified name (in different pages that register callback using on). You can also just send notification without object | ||
* proxy(url) - create iframe proxy for different domain, the targer domain/url should have [proxy.html](https://github.com/jcubic/sysend.js/blob/master/proxy.html) file. If url domain is the same as page domain, it's ignored. So you can put both proxy calls on both domains (new in 1.3.0) | ||
## License | ||
Copyright (C) 2014-2017 [Jakub Jankiewicz](http://jcubic.pl)<br/> | ||
Copyright (C) 2014-2018 [Jakub Jankiewicz](http://jcubic.pl)<br/> | ||
Released under the [MIT license](https://opensource.org/licenses/MIT) | ||
@@ -75,0 +88,0 @@ |
/**@license | ||
* sysend.js - send messages between browser windows/tabs version 1.2.0 | ||
* sysend.js - send messages between browser windows/tabs version 1.3.0 | ||
* | ||
@@ -62,4 +62,16 @@ * Copyright (C) 2014-2018 Jakub Jankiewicz <http://jcubic.pl/me> | ||
} | ||
function send_to_iframes(key, data) { | ||
// propagate events to iframes | ||
iframes.forEach(function(iframe) { | ||
var payload = { | ||
name: uniq_prefix, | ||
key: key, | ||
data: data | ||
}; | ||
iframe.window.postMessage(JSON.stringify(payload), "*"); | ||
}); | ||
} | ||
// object with user events as keys and values arrays of callback functions | ||
var callbacks = {}; | ||
var iframes = []; | ||
var index = 0; | ||
@@ -99,2 +111,18 @@ var channel; | ||
} | ||
// ref: https://stackoverflow.com/a/326076/387194 | ||
function is_iframe() { | ||
try { | ||
return window.self !== window.top; | ||
} catch (e) { | ||
return true; | ||
} | ||
} | ||
if (is_iframe()) { | ||
window.addEventListener('message', function(e) { | ||
var payload = JSON.parse(e.data); | ||
if (payload.name === uniq_prefix) { | ||
sysend.broadcast(payload.key, payload.data); | ||
} | ||
}); | ||
} | ||
return { | ||
@@ -111,3 +139,32 @@ broadcast: function(event, message) { | ||
} | ||
send_to_iframes(event, message); | ||
}, | ||
proxy: function(url) { | ||
if (typeof url === 'string' && !url.match(new RegExp(window.location.host, 'i'))) { | ||
var iframe = document.createElement('iframe'); | ||
iframe.style.visibility = 'hidden'; | ||
var proxy_url = url; | ||
if (!url.match(/\.html$/)) { | ||
proxy_url = url.replace(/\/$/, '') + '/proxy.html'; | ||
} | ||
iframe.addEventListener('error', function handler() { | ||
setTimeout(function() { | ||
throw new Error('html proxy file not found on "' + url + '" url'); | ||
}, 0); | ||
iframe.removeEventListener('error', handler); | ||
}); | ||
iframe.addEventListener('load', function handler() { | ||
var win; | ||
try { | ||
win = iframe.contentWindow; | ||
} catch(e) { | ||
win = iframe.contentWindow; | ||
} | ||
iframes.push({window: win, node: iframe}); | ||
iframe.removeEventListener('load', handler); | ||
}); | ||
document.body.appendChild(iframe); | ||
iframe.src = proxy_url; | ||
} | ||
}, | ||
on: function(event, fn) { | ||
@@ -114,0 +171,0 @@ if (!callbacks[event]) { |
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
15452
9
216
87