javascript - window.postMessage to script.google.com as popup - Stack Overflow

When run:MyPopWindow.postMessage("Test", 'mydomaine');I have a error on MyPopWind

When run: MyPopWindow.postMessage("Test", 'mydomaine'); I have a error on MyPopWindow whith script.google:

(program):1 Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('mydomaine') does not match the recipient window's origin ('').

When run: MyPopWindow.postMessage("Test", ''); I have a error on MyPopWindow:

dropping postMessage.. was from host mydomaine but expected host https : // ******-script.googleusercontent

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)

When run: MyPopWindow.postMessage("Test", 'mydomaine'); I have a error on MyPopWindow whith script.google.:

(program):1 Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('mydomaine') does not match the recipient window's origin ('https://script.google.').

When run: MyPopWindow.postMessage("Test", 'https://script.google.'); I have a error on MyPopWindow:

dropping postMessage.. was from host mydomaine but expected host https : // ******-script.googleusercontent.

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);
        });

Share asked Oct 13, 2016 at 6:49 user144039user144039 331 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

To send a message from the Google apps Script window/iframe to the parent window, I've found out that the following code works for me;

if(window.parent.parent !== window.top){
window.top.postMessage("Test", "mydomaine");
};

Sadly, I haven't, at least yet, found a way to send messages from the parent window to the Google apps script window/iframe.

-------Edit-------

I found the solution on sending a message to iframe from parent window! It's really quite simple;

The code of the parent window;

  fetch('your google apps scipt webapp url', {
  method: 'POST',
  body: JSON.stringify('the message you want to send to the 
  iframe')
  })
  .then(response => {
  if (response.ok) {
  console.log('String sent successfully to Google Apps 
  Script');
  } else {
  console.error('Error sending string to Google Apps 
  Script');
  }
  })
  .catch(error => {
  console.error('Error:', error);
  });

The code of the .gs file in your Google apps script;

    const userProperties = PropertiesService.getUserProperties();
    
    function doPost(request) {
    var payload = JSON.parse(request.postData.contents);
    userProperties.setProperty('retrievedMessage', payload);
    }

Then, In any function in the .gs file you can use:

   const retrievedMessage = userProperties.getProperty('retrievedMessage');

Note:

In the above I am using the GAS Properties Service which offers 3 methods:

  1. Script Properties
  2. User Properties (What I used in my example code)
  3. Document Properties

This error message es 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 munication 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744926079a4601444.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信