Skip to content

Commit

Permalink
update notes
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Nov 17, 2016
1 parent 33941ec commit 93d8141
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions other-notes/gyro-hack-in-iframe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# # gyro hack in iframe

> If you use the gyro plugin in an iframe, it can only work if the domain of the main window is the same domain as the iframe. This is a (security) limitation of the browser.
## #solution

get gyro information in the main window & post it to the child iframe.

- iframe
- postMessage
- JSON

在主窗体获取传感器信息,并使用postMessage传递信息到iframe子页面,测试中发现postMessage需要的信息为字符串,所有先做一次JSON.stringify,在子页面做一次JSON.parse即可。

## #code

```
//main window
if (window.DeviceOrientationEvent) {
var iframe_embed_hack = {
enabled: true
};
function onUpdate(){
document.getElementById('child').contentWindow.postMessage(JSON.stringify(iframe_embed_hack),"*");
}
function onScreenOrientationChangeEvent(){
iframe_embed_hack.screenOrientation = window.orientation || 0;
}
function onDeviceOrientationChangeEvent(event){
iframe_embed_hack.deviceOrientation = event;
onUpdate();
}
window.addEventListener('orientationchange', onScreenOrientationChangeEvent, false);
window.addEventListener('deviceorientation', onDeviceOrientationChangeEvent, false);
}
```

```
//the child iframe
function messageHandler(data){
window.iframe_embed_hack = data;
}
window.addEventListener("message", function(ev) {
messageHandler(JSON.parse(ev.data));
}, false );
```

0 comments on commit 93d8141

Please sign in to comment.