window.postMessage以script.google.com作为弹出窗口 [英] window.postMessage to script.google.com as popup
问题描述
运行时:
MyPopWindow.postMessage(Test,' mydomaine ');
我在MyPopWindow whith script.google.com上发生错误:
$ b
<程序>:1无法执行'postMessage' 'DOMWindow':提供的目标
原点(' mydomaine ')与
收件人窗口的原点(' https://script.google.com )。
运行时:
MyPopWindow.postMessage(Test,' https://script.google.com ') ;
MyPopWindow出现错误:
丢弃postMessage ..来自主机 mydomaine ,但预期主机
https:// ****** - script.googleusercontent.com
源页面 mydomaine :
window.addEventListener(DOMContentLoaded, function(){window.addEventListener(message,function(e){//等待孩子发信号表明它已经加载if(e.data ===loaded&& e.origin === iframe .src.split(/)。splice(0,3).join(/)){//向孩子发送一条消息alert(e.data);}})},false)$
以我的Google Apps脚本作为WebApp运行的源代码:
$ b
$ b
document.addEventListener('DOMContentLoaded',function(){//指示我们加载的父级。 window.parent.postMessage(loaded,*); //收听来自父母的消息。 window.addEventListener(message,function(e){if(event.origin!=='mydomain')return; var message = e.data; alert(message);},false);
解决方案
此错误讯息来自其中一个应用程式脚本 Javascript驱动程式档案一>。
这是Google为了防止用户使用postMessage系统而添加的额外安全功能。
Google似乎希望强制您使用他们接受的协议之一Windows /域之间的通信,即执行API 。
我尝试了其他方法,例如传递URL参数,但至今都没有工作,因为我无法从所有应用程序脚本运行的iframe中访问它们。
<我相信Execution API是你最好的选择。
When run: MyPopWindow.postMessage("Test", 'mydomaine'); I have a error on MyPopWindow whith script.google.com:
(program):1 Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('mydomaine') does not match the recipient window's origin ('https://script.google.com').
When run: MyPopWindow.postMessage("Test", 'https://script.google.com'); I have a error on MyPopWindow:
dropping postMessage.. was from host mydomaine but expected host https : // ******-script.googleusercontent.com
Source in page on mydomaine:
window.addEventListener("DOMContentLoaded", function() {
window.addEventListener("message", function(e) {
// wait for child to signal that it's loaded.
if ( e.data === "loaded" && e.origin === iframe.src.split("/").splice(0, 3).join("/")) {
// send the child a message.
alert(e.data);
}
})
}, false)
Source on my Google Apps Script runing as WebApp:
document.addEventListener('DOMContentLoaded', function () {
// signal the parent that we're loaded.
window.parent.postMessage("loaded", "*");
// listen for messages from the parent.
window.addEventListener("message", function(e) {
if(event.origin !== 'mydomain') return;
var message = e.data;
alert(message);
}, false);
});
This error message comes from one of App Scripts Javascript driver files. This is extra security Google added on to prevent people from using the postMessage system.
It seems Google wants to force you to use one of their accepted protocols for communication between windows/domains, namely the Execution API. I have attempted other methods like passing URL parameters, but so far none have worked because I could not access them from within the iframe that all apps scripts run in.
I believe the Execution API is your best bet.
这篇关于window.postMessage以script.google.com作为弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!