mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-06 22:52:25 +02:00
hack to have a SW work on iframe
This commit is contained in:
@ -164,24 +164,38 @@ export default class ContentWrap extends Component {
|
|||||||
log('✉️ Sending message to detached window');
|
log('✉️ Sending message to detached window');
|
||||||
this.detachedWindow.postMessage({ contents }, '*');
|
this.detachedWindow.postMessage({ contents }, '*');
|
||||||
} else {
|
} else {
|
||||||
|
const refreshAndDo = fn => {
|
||||||
|
Promise.race([
|
||||||
|
// Just in case onload promise doesn't resolves
|
||||||
|
new Promise(resolve => {
|
||||||
|
setTimeout(resolve, 200);
|
||||||
|
}),
|
||||||
|
new Promise(resolve => {
|
||||||
|
this.frame.onload = resolve;
|
||||||
|
})
|
||||||
|
]).then(fn);
|
||||||
|
// Setting to blank string cause frame to reload
|
||||||
|
this.frame.src = this.frame.src;
|
||||||
|
};
|
||||||
const writeInsideIframe = () => {
|
const writeInsideIframe = () => {
|
||||||
// this.frame.contentDocument.open();
|
const sandbox = this.frame.getAttribute('sweet');
|
||||||
|
console.log('setting back sandbox attr', sandbox);
|
||||||
|
this.frame.setAttribute('sandbox', sandbox);
|
||||||
|
this.frame.removeAttribute('sweet');
|
||||||
console.log('sending postmessage');
|
console.log('sending postmessage');
|
||||||
this.frame.contentWindow.postMessage({ contents }, '*');
|
this.frame.contentWindow.postMessage({ contents }, '*');
|
||||||
|
// this.frame.contentDocument.open();
|
||||||
// this.frame.contentDocument.write(contents);
|
// this.frame.contentDocument.write(contents);
|
||||||
// this.frame.contentDocument.close();
|
// this.frame.contentDocument.close();
|
||||||
};
|
};
|
||||||
Promise.race([
|
refreshAndDo(() => {
|
||||||
// Just in case onload promise doesn't resolves
|
const sandbox = this.frame.getAttribute('sandbox');
|
||||||
new Promise(resolve => {
|
console.log('removing sandbox', sandbox);
|
||||||
setTimeout(resolve, 200);
|
this.frame.setAttribute('sweet', sandbox);
|
||||||
}),
|
this.frame.removeAttribute('sandbox');
|
||||||
new Promise(resolve => {
|
refreshAndDo(writeInsideIframe);
|
||||||
this.frame.onload = resolve;
|
});
|
||||||
})
|
// refreshAndDo(writeInsideIframe);
|
||||||
]).then(writeInsideIframe);
|
|
||||||
// Setting to blank string cause frame to reload
|
|
||||||
this.frame.src = this.frame.src;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// we need to store user script in external JS file to prevent inline-script
|
// we need to store user script in external JS file to prevent inline-script
|
||||||
|
Reference in New Issue
Block a user