|
1 | 1 | /** |
2 | 2 | * cross-storage - Cross domain local storage |
3 | 3 | * |
4 | | - * @version 0.8.0 |
| 4 | + * @version 0.8.1 |
5 | 5 | * @link https://github.com/zendesk/cross-storage |
6 | 6 | * @author Daniel St. Jules <danielst.jules@gmail.com> |
7 | 7 | * @copyright Zendesk |
|
274 | 274 | var client = this; |
275 | 275 |
|
276 | 276 | this._listener = function(message) { |
277 | | - var i, error, response; |
| 277 | + var i, origin, error, response; |
278 | 278 |
|
279 | | - // Ignore invalid messages, those not from the correct hub, or when |
280 | | - // the client has closed |
281 | | - if (client._closed || !message.data || typeof message.data !== 'string' || |
282 | | - message.origin !== client._origin) { |
| 279 | + // Ignore invalid messages or those after the client has closed |
| 280 | + if (client._closed || !message.data || typeof message.data !== 'string') { |
283 | 281 | return; |
284 | 282 | } |
285 | 283 |
|
| 284 | + // postMessage returns the string "null" as the origin for "file://" |
| 285 | + origin = (message.origin === 'null') ? 'file://' : message.origin; |
| 286 | + |
| 287 | + // Ignore messages not from the correct origin |
| 288 | + if (origin !== client._origin) return; |
| 289 | + |
286 | 290 | // LocalStorage isn't available in the hub |
287 | 291 | if (message.data === 'cross-storage:unavailable') { |
288 | 292 | if (!client._closed) client.close(); |
|
337 | 341 | * establish a connected state. |
338 | 342 | */ |
339 | 343 | CrossStorageClient.prototype._poll = function() { |
340 | | - var client, interval; |
| 344 | + var client, interval, targetOrigin; |
341 | 345 |
|
342 | 346 | client = this; |
| 347 | + |
| 348 | + // postMessage requires that the target origin be set to "*" for "file://" |
| 349 | + targetOrigin = (client._origin === 'file://') ? '*' : client._origin; |
| 350 | + |
343 | 351 | interval = setInterval(function() { |
344 | 352 | if (client._connected) return clearInterval(interval); |
345 | 353 | if (!client._hub) return; |
346 | 354 |
|
347 | | - client._hub.postMessage('cross-storage:poll', client._origin); |
| 355 | + client._hub.postMessage('cross-storage:poll', targetOrigin); |
348 | 356 | }, 1000); |
349 | 357 | }; |
350 | 358 |
|
|
405 | 413 | }; |
406 | 414 |
|
407 | 415 | return new this._promise(function(resolve, reject) { |
408 | | - var timeout, originalToJSON; |
| 416 | + var timeout, originalToJSON, targetOrigin; |
409 | 417 |
|
410 | 418 | // Timeout if a response isn't received after 4s |
411 | 419 | timeout = setTimeout(function() { |
|
429 | 437 | Array.prototype.toJSON = null; |
430 | 438 | } |
431 | 439 |
|
| 440 | + // postMessage requires that the target origin be set to "*" for "file://" |
| 441 | + targetOrigin = (client._origin === 'file://') ? '*' : client._origin; |
| 442 | + |
432 | 443 | // Send serialized message |
433 | | - client._hub.postMessage(JSON.stringify(req), client._origin); |
| 444 | + client._hub.postMessage(JSON.stringify(req), targetOrigin); |
434 | 445 |
|
435 | 446 | // Restore original toJSON |
436 | 447 | if (originalToJSON) { |
|
447 | 458 | } else if (typeof exports !== 'undefined') { |
448 | 459 | exports.CrossStorageClient = CrossStorageClient; |
449 | 460 | } else if (typeof define === 'function' && define.amd) { |
450 | | - define('CrossStorageClient', [], function() { |
| 461 | + define([], function() { |
451 | 462 | return CrossStorageClient; |
452 | 463 | }); |
453 | 464 | } else { |
|
0 commit comments