You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
with `list()` method and `open`/`close` events you can implement dynamic list of windows/tab. That will change when new window/tab is open or close.
107
107
108
+
```javascript
109
+
let list = [];
110
+
111
+
sysend.track('open', data=> {
112
+
if (data.id!==sysend.id) {
113
+
list.push(data);
114
+
populate_list(list);
115
+
}
116
+
});
117
+
118
+
sysend.track('close', data=> {
119
+
list =list.filter(tab=>data.id!==tab.id);
120
+
populate_list(list);
121
+
});
122
+
123
+
sysend.track('ready', () => {
124
+
sysend.list().then(tabs=> {
125
+
list = tabs;
126
+
populate_list(list);
127
+
});
128
+
});
129
+
130
+
functionpopulate_list() {
131
+
select.innerHTML='';
132
+
list.forEach(tab=> {
133
+
constoption=document.createElement('option');
134
+
option.value=tab.id;
135
+
option.innerText=tab.id;
136
+
select.appendChild(option);
137
+
});
138
+
}
139
+
```
140
+
141
+
In version 1.16.0 this code was abstracted into:
142
+
143
+
```javascript
144
+
sysend.track('update', (list) => {
145
+
populate_list(list);
146
+
});
147
+
```
148
+
149
+
This can be simplified with point free style:
150
+
151
+
```javascript
152
+
sysend.track('update', populate_list);
153
+
```
154
+
155
+
### RPC mechanism
156
+
108
157
In version 1.15.0 new API was added called `rpc()` (build on top of tracking mechanism) that allow to use RPC (Remote Procedure Call) between open windows/tabs.
on Firefox you need to add **CORS** for the proxy.html that will be loaded into iframe (see [Cross-Domain LocalStorage](https://jcubic.wordpress.com/2014/06/20/cross-domain-localstorage/)).
137
186
138
-
### Security protection
139
-
140
-
Since version 1.10.0 as a security mesure Cross-Domain communication has been limited to only those domains that are allowed.
141
-
To allow domain to listen to sysend communication you need to specify channel inside iframe. You need add your origins to the
142
-
`sysend.channel()` function (origin is combination of protocol domain and optional port).
143
-
144
187
### Serialization
145
188
146
189
if you want to send custom data you can use serializer (new in 1.4.0) this API
Since version 1.10.0 as a security mesure Cross-Domain communication has been limited to only those domains that are allowed.
215
+
To allow domain to listen to sysend communication you need to specify channel inside iframe. You need add your origins to the
216
+
`sysend.channel()` function (origin is combination of protocol domain and optional port).
217
+
218
+
169
219
## Demos
170
220
171
221
* [Simple demo using iframes](https://jcubic.pl/sysend-demo/).
@@ -189,8 +239,8 @@ sysend object:
189
239
| `emit(name, [, object])` | same as `broadcast()` but also invoke the even on same page | name - string - The name of the event<br>object - optional any data | 1.5.0 |
190
240
| `post(<window_id>, [, object])` | send any data to other window | window_id - string of the target window (use `'primary'` to send to primary window)<br>object - any data | 1.6.0 / `'primary'` target 1.14.0 |
191
241
| `list()` | returns a Promise of objects `{id:<UUID>, primary}` for other windows, you can use those to send a message with `post()` | NA | 1.6.0 |
192
-
| `track(event, callback)` | track inter window communication events | event - any of the strings: `"open"`, `"close"`, `"primary"`, <br>`"secondary"`, `"message"`<br>callback - different function depend on the event:<br>* `"message"` - `{data, origin}` - where data is anything the `post()` sends, and origin is `id` of the sender.<br>* `"open"` - `{count, primary, id}` when new window/tab is opened<br>* `"close"` - `{count, primary, id, self}` when window/tab is closed<br>* `"primary"` and `"secondary"` function has no arguments and is called when window/tab become secondary or primary.<br>* `"ready"` - event when tracking is ready. | 1.6.0 except `ready` - 1.10.0 |
193
-
| `untrack(event [,callback])` | remove single event listener all listeners for a given event | event - any of the strings `'open'`, `'close'`, `'primary'`, `'secondary'`, or `'message'`. | 1.6.0 |
242
+
| `track(event, callback)` | track inter window communication events | event - any of the strings: `"open"`, `"close"`, `"primary"`, <br>`"secondary"`, `"message"`, `"update"`<br>callback - different function depend on the event:<br>* `"message"` - `{data, origin}` - where data is anything the `post()` sends, and origin is `id` of the sender.<br>* `"open"` - `{count, primary, id}` when new window/tab is opened<br>* `"close"` - `{count, primary, id, self}` when window/tab is closed<br>* `"primary"` and `"secondary"` function has no arguments and is called when window/tab become secondary or primary.<br>* `"ready"` - event when tracking is ready. | 1.6.0 except `ready` - 1.10.0 and `update` - 1.16.0 |
243
+
| `untrack(event [,callback])` | remove single event listener all listeners for a given event | event - any of the strings `'open'`, `'close'`, `'primary'`, `'secondary'`, `'message'`, or `'update'`. | 1.6.0 |
194
244
| `isPrimary()` | function returns true if window is primary (first open or last that remain) | NA | 1.6.0 |
195
245
| `channel()` | function restrict cross domain communication to only allowed domains. You need to call this function on proxy iframe to limit number of domains (origins) that can listen and send events. | any number of origins (e.g. 'http://localhost:8080' or 'https://jcubic.github.io') you can also use valid URL. | 1.10.0 |
196
246
| `useLocalStorage([toggle])` | Function set or toggle localStorage mode. | argument is optional and can be `true` or `false`. | 1.14.0 |
0 commit comments