From bdc82ddb4f67abbd25b797268c06f74d599b58e6 Mon Sep 17 00:00:00 2001 From: Evan Carothers Date: Thu, 25 Aug 2016 12:21:15 -0500 Subject: [PATCH] updates to fix local caching for xdomain_only instances of the class, and better origin checking --- src/xdomain_cookie.html | 6 +++--- src/xdomain_cookie.js | 3 +++ test/test_page.html | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/xdomain_cookie.html b/src/xdomain_cookie.html index 831e686..051b6b2 100644 --- a/src/xdomain_cookie.html +++ b/src/xdomain_cookie.html @@ -34,14 +34,14 @@ window.addEventListener('message', function(event){ var origin = event.origin || event.originalEvent.origin; // For Chrome, the origin property is in the event.originalEvent object. - if (origin !== _iframe_origin) return; //incoming message not from iframe page - + if ( [_window_origin,_iframe_origin].indexOf(origin) === -1 ) return; //incoming message not from iframe page + //We must filter messages here to verify that it's the specific message/type we are looking for, and not from another script var data = null; try{ data = JSON.parse(event.data); }catch(e){} - + if(data && typeof data==='object' && 'msg_type' in data && data.msg_type==='xdsc_write' && 'namespace' in data && data.namespace === _namespace){ _set_local_cookie( data.cookie_name, data.cookie_val, parseInt(data.expires_days,10) ); //ping down to page again to update values of xdomain cookie data diff --git a/src/xdomain_cookie.js b/src/xdomain_cookie.js index 6f3f684..0537f21 100644 --- a/src/xdomain_cookie.js +++ b/src/xdomain_cookie.js @@ -113,6 +113,9 @@ _set_cookie_in_iframe( cookie_name, cookie_value, expires_days ); } + //set local cached value + _xdomain_cookie_data[cookie_name] = cookie_value; + } //function to call after instantiation to sync a cookie, supplying a cookie name, value to write if it does NOT exist, expires diff --git a/test/test_page.html b/test/test_page.html index fb5da72..0f8c2f7 100644 --- a/test/test_page.html +++ b/test/test_page.html @@ -42,6 +42,8 @@ if(!cookie_val){ + if(is_dev) console.log("SETTING EMPTY COOKIE", cookie_test_val); + xd_cookie.set( 'test_cookie', cookie_test_val, 30, xdomain_only ); //get again to verify that it was set