Created
September 9, 2013 22:12
-
-
Save toadkicker/6502218 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(){ | |
| //allowed domains | |
| var whitelist = ["foo.example.com", "www.example.com"]; | |
| function verifyOrigin(origin){ | |
| var domain = origin.replace(/^https?:\/\/|:\d{1,4}$/g, "").toLowerCase(), | |
| i = 0, | |
| len = whitelist.length; | |
| while(i < len){ | |
| if (whitelist[i] == domain){ | |
| return true; | |
| } | |
| i++; | |
| } | |
| return false; | |
| } | |
| function handleRequest(event){ | |
| if (verifyOrigin(event.origin)){ | |
| var data = JSON.parse(event.data), | |
| value = localStorage.getItem(data.key); | |
| event.source.postMessage(JSON.stringify({id: data.id, key:data.key, value: value}), event.origin); | |
| } | |
| } | |
| if(window.addEventListener){ | |
| window.addEventListener("message", handleRequest, false); | |
| } else if (window.attachEvent){ | |
| window.attachEvent("onmessage", handleRequest); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment