Comparing version 1.17.0 to 1.17.1
{ | ||
"name": "sysend", | ||
"version": "1.17.0", | ||
"version": "1.17.1", | ||
"description": "Communication and Synchronization between browser tabs/windows. Works cross-domain.", | ||
@@ -5,0 +5,0 @@ "main": "sysend.js", |
@@ -5,4 +5,4 @@ <p align="center"> | ||
[![npm](https://img.shields.io/badge/npm-1.17.0-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.17.0-yellow.svg) | ||
[![npm](https://img.shields.io/badge/npm-1.17.1-blue.svg)](https://www.npmjs.com/package/sysend) | ||
![bower](https://img.shields.io/badge/bower-1.17.1-yellow.svg) | ||
![downloads](https://img.shields.io/npm/dt/sysend.svg) | ||
@@ -48,3 +48,3 @@ [![jsdelivr](https://img.shields.io/jsdelivr/npm/hm/sysend)](https://www.jsdelivr.com/package/npm/sysend) | ||
You can register two and more domains, you will have a token that you need to add to the proxy html file (in the head tag): | ||
You can register two and more domains, you will have a token that you need to add to the HTML files (in the head tag): | ||
@@ -61,3 +61,3 @@ ```html | ||
Right now the API only works with localStorage fallback. | ||
Right now the API only works with localStorage fallback (when inside iframes). | ||
@@ -64,0 +64,0 @@ ## Installation |
/**@license | ||
* sysend.js - send messages between browser windows/tabs version 1.17.0 | ||
* sysend.js - send messages between browser windows/tabs version 1.17.1 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (C) 2014-2023 Jakub T. Jankiewicz <https://jcubic.pl/me> |
129
sysend.js
/**@license | ||
* sysend.js - send messages between browser windows/tabs version 1.17.0-beta | ||
* sysend.js - send messages between browser windows/tabs version 1.17.1 | ||
* | ||
@@ -21,2 +21,3 @@ * Copyright (C) 2014-2023 Jakub T. Jankiewicz <https://jcubic.pl/me> | ||
})(typeof window !== 'undefined' ? window : this, function() { | ||
var DEBUG = false; | ||
var collecting_timeout = 400; | ||
@@ -71,2 +72,3 @@ // we use prefix so `foo' event don't collide with `foo' locaStorage value | ||
if (channel && !force_ls) { | ||
log('broadcast', { event, data }); | ||
channel.postMessage({name: event, data: serialize(data)}); | ||
@@ -84,2 +86,3 @@ } else { | ||
emit: function(event, data) { | ||
log('emit', { event, data }); | ||
sysend.broadcast(event, data); | ||
@@ -103,3 +106,3 @@ invoke(event, data); | ||
domains.push(origin(url)); | ||
const iframe = document.createElement('iframe'); | ||
var iframe = document.createElement('iframe'); | ||
iframe.style.width = iframe.style.height = 0; | ||
@@ -109,3 +112,3 @@ iframe.style.position = 'absolute'; | ||
iframe.style.border = 'none'; | ||
let proxy_url = url; | ||
var proxy_url = url; | ||
if (!url.match(/\.html|\.php|\?/)) { | ||
@@ -122,3 +125,3 @@ proxy_url = url.replace(/\/$/, '') + '/proxy.html'; | ||
iframe.addEventListener('load', function handler() { | ||
let win; | ||
var win; | ||
// fix for Safari | ||
@@ -199,8 +202,9 @@ // https://stackoverflow.com/q/42632188/387194 | ||
list: function() { | ||
const id = list_id++; | ||
const marker = { target: target_id, id: id }; | ||
const timer = delay(sysend.timeout); | ||
var id = list_id++; | ||
var marker = { target: target_id, id: id, origin: self.origin }; | ||
var timer = delay(sysend.timeout); | ||
return new Promise(function(resolve) { | ||
const ids = []; | ||
var ids = []; | ||
sysend.on(make_internal('__window_ack__'), function(data) { | ||
log('__window_ack__', { data, marker }); | ||
if (data.origin.target === target_id && data.origin.id === id) { | ||
@@ -215,2 +219,3 @@ ids.push({ | ||
timer().then(function() { | ||
log('timeout', { ids }); | ||
resolve(ids); | ||
@@ -235,13 +240,13 @@ }); | ||
rpc: function(object) { | ||
const prefix = ++rpc_count; | ||
const req = `__${prefix}_rpc_request__`; | ||
const res = `__${prefix}_rpc_response__`; | ||
let request_index = 0; | ||
const timeout = 1000; | ||
var prefix = ++rpc_count; | ||
var req = `__${prefix}_rpc_request__`; | ||
var res = `__${prefix}_rpc_response__`; | ||
var request_index = 0; | ||
var timeout = 1000; | ||
function request(id, method, args = []) { | ||
const req_id = ++request_index; | ||
var req_id = ++request_index; | ||
return new Promise((resolve, reject) => { | ||
sysend.track('message', function handler({data, origin}) { | ||
if (data.type === res) { | ||
const { result, error, id: res_id } = data; | ||
var { result, error, id: res_id } = data; | ||
if (origin === id && req_id === res_id) { | ||
@@ -259,3 +264,3 @@ if (error) { | ||
sysend.post(id, { method, id: req_id, type: req, args }); | ||
const timer = setTimeout(() => { | ||
var timer = setTimeout(() => { | ||
reject(new Error('Timeout error')); | ||
@@ -268,4 +273,4 @@ }, timeout); | ||
if (data.type == req) { | ||
const { method, args, id } = data; | ||
const type = res; | ||
var { method, args, id } = data; | ||
var type = res; | ||
if (Object.hasOwn(object, method)) { | ||
@@ -287,3 +292,3 @@ try { | ||
}, true); | ||
const error_msg = 'You need to specify the target window/tab'; | ||
var error_msg = 'You need to specify the target window/tab'; | ||
return Object.fromEntries(Object.keys(object).map(name => { | ||
@@ -339,3 +344,3 @@ return [name, (id, ...args) => { | ||
if (is_promise(obj)) { | ||
const ret = obj.then(callback); | ||
var ret = obj.then(callback); | ||
if (error === null) { | ||
@@ -397,2 +402,8 @@ return ret; | ||
// ------------------------------------------------------------------------- | ||
function log() { | ||
if (DEBUG) { | ||
console.log.apply(null, [self.origin].concat(Array.from(arguments))); | ||
} | ||
} | ||
// ------------------------------------------------------------------------- | ||
function iframe_loaded() { | ||
@@ -476,2 +487,3 @@ var iframes = Array.from(document.querySelectorAll('iframe')); | ||
function get(key) { | ||
log({get: key}); | ||
return ls().getItem(make_internal(key)); | ||
@@ -482,2 +494,3 @@ } | ||
// storage event is not fired when value is set first time | ||
log({set: key, value}); | ||
if (id == 0) { | ||
@@ -490,2 +503,3 @@ ls().setItem(make_internal(key), random_value); | ||
function remove(key) { | ||
log({remove: key}); | ||
ls().removeItem(uniq_prefix + key); | ||
@@ -621,2 +635,3 @@ } | ||
function setup_ls() { | ||
log('setup_ls()'); | ||
// we need to clean up localStorage if broadcast called on unload | ||
@@ -650,3 +665,3 @@ // because setTimeout will never fire, even setTimeout 0 | ||
function setup_update_tracking() { | ||
let list = []; | ||
var list = []; | ||
@@ -660,2 +675,3 @@ function update() { | ||
list.push(data); | ||
log({ list, action: 'open' }); | ||
update(); | ||
@@ -667,2 +683,3 @@ } | ||
list = list.filter(tab => data.id !== tab.id); | ||
log({ list, action: 'close' }); | ||
update(); | ||
@@ -672,10 +689,13 @@ }, true); | ||
sysend.track('ready', () => { | ||
sysend.list().then(tabs => { | ||
list = tabs; | ||
update(); | ||
}); | ||
setTimeout(function() { | ||
sysend.list().then(tabs => { | ||
list = tabs; | ||
log({ list, action: 'ready' }); | ||
update(); | ||
}); | ||
}, 0); | ||
}, true); | ||
} | ||
// ------------------------------------------------------------------------- | ||
function init_channel() { | ||
function setup_channel() { | ||
if (sa_handle) { | ||
@@ -693,2 +713,3 @@ if (sa_handle.hasOwnProperty('BroadcastChannel')) { | ||
if (event.target.name === uniq_prefix) { | ||
log('message', { data: event.data, iframe: is_proxy_iframe() }); | ||
if (is_proxy_iframe()) { | ||
@@ -713,19 +734,5 @@ var payload = { | ||
// ------------------------------------------------------------------------- | ||
function init() { | ||
if (typeof window.BroadcastChannel === 'function') { | ||
if (is_proxy_iframe() && document.requestStorageAccess) { | ||
document.requestStorageAccess({ | ||
all: true | ||
}).then(function(handle) { | ||
sa_handle = handle; | ||
init_channel(); | ||
}); | ||
} else { | ||
init_channel(); | ||
} | ||
} else if (is_private_mode) { | ||
warn('Your browser don\'t support localStorgage. ' + | ||
'In Safari this is most of the time because ' + | ||
'of "Private Browsing Mode"'); | ||
} | ||
function seutp() { | ||
setup_channel(); | ||
if (!is_private_mode) { | ||
@@ -741,2 +748,3 @@ setup_ls(); | ||
var payload = JSON.parse(e.data); | ||
log('iframe', payload, payload.name === uniq_prefix); | ||
if (payload && payload.name === uniq_prefix) { | ||
@@ -762,2 +770,3 @@ var data = unserialize(payload.data); | ||
sysend.on(make_internal('__primary__'), function() { | ||
log('__primary__'); | ||
has_primary = true; | ||
@@ -767,2 +776,3 @@ }); | ||
sysend.on(make_internal('__open__'), function(data) { | ||
log('__open__', data); | ||
var id = data.id; | ||
@@ -790,2 +800,3 @@ target_count++; | ||
sysend.on(make_internal('__close__'), function(data) { | ||
log('__close__', data); | ||
--target_count; | ||
@@ -811,5 +822,7 @@ var last = target_count === 1; | ||
sysend.on(make_internal('__window__'), function(data) { | ||
log('__window__', { data }) | ||
sysend.broadcast(make_internal('__window_ack__'), { | ||
id: target_id, | ||
origin: data.id, | ||
from: self.origin, | ||
primary: primary | ||
@@ -820,2 +833,3 @@ }); | ||
sysend.on(make_internal('__message__'), function(data) { | ||
log('__message__', data); | ||
if (data.target === 'primary' && primary) { | ||
@@ -829,3 +843,7 @@ trigger(handlers.message, data); | ||
addEventListener('beforeunload', function() { | ||
sysend.emit(make_internal('__close__'), { id: target_id, wasPrimary: primary }); | ||
log('beforeunload'); | ||
sysend.emit(make_internal('__close__'), { | ||
id: target_id, | ||
wasPrimary: primary | ||
}); | ||
}, { capture: true }); | ||
@@ -835,2 +853,3 @@ | ||
sysend.list().then(function(list) { | ||
log('sysend.list()', list); | ||
target_count = list.length; | ||
@@ -855,3 +874,25 @@ primary = list.length === 0; | ||
} | ||
// ------------------------------------------------------------------------- | ||
function init() { | ||
if (typeof window.BroadcastChannel === 'function') { | ||
if (is_proxy_iframe() && document.requestStorageAccess) { | ||
document.requestStorageAccess({ | ||
all: true | ||
}).then(function(handle) { | ||
sa_handle = handle; | ||
log({init: handle}); | ||
seutp(); | ||
}).catch(seutp); | ||
} else { | ||
seutp(); | ||
} | ||
return sysend; | ||
} else if (is_private_mode) { | ||
warn('Your browser don\'t support localStorgage. ' + | ||
'In Safari this is most of the time because ' + | ||
'of "Private Browsing Mode"'); | ||
} | ||
seutp(); | ||
} | ||
return sysend; | ||
}); |
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
50884
878