Insert img whith PostMessage API

I’m having trouble sending an image to the document with the postmessage API, I created a customizable button to do this, and I’m getting the document’s returns, when the button is selected I send the image to the document, but the image is not inserted, Note: I’m using iframe

receiveMessage(event) {
let jsonData = ‘’;
if(typeof event.data === ‘object’) {
jsonData = event.data;
}
else {
jsonData = JSON.parse(event.data);
}
console.log("Message received from the child: ", jsonData); // Message received from child
const protocol = window.location.protocol;
const host = window.location.host;
const port = window.location.port ? :${window.location.port} : ‘’;

    // Formar a URL base
    const baseUrl = `${protocol}//${host}`;

    if(jsonData.MessageId == "Clicked_Button") {
        if(jsonData.Values.Id == "brasao__") {
            document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(
                JSON.stringify({'MessageId': 'Send_UNO_Command',
                'Values': {
                    'Command': '.uno:DefaultFormat',
                    'Args': {}
                }
                }), '*');

            document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(
                JSON.stringify({
                    MessageId: 'Send_UNO_Command',
                    'Values': {
                      'Command': '.uno:InsertHeader',
                      'Args': {}
                    }
                  }), '*');    

                

            document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(
                JSON.stringify({'MessageId': 'Send_UNO_Command',
                    'Values': {
                    'Command': '.uno:CenterPara',
                    'Args': {}
                    }
                }), '*'); 


           
            var urlImg = baseUrl + '/ui/img/brasao.png';
            urlImg = urlImg.toString();
            document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(
                JSON.stringify({
                    MessageId: "Action_InsertGraphics",
                    Values: {
                        url: urlImg,
                    }
            }), '*');
            
            

            

            setTimeout(function() {

                document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(
                    JSON.stringify({'MessageId': 'Send_UNO_Command',
                    'Values': {
                        'Command': '.uno:InsertText',
                        'Args': {
                            Text: {
                                type: 'string',
                                value: 'PODER JUDICIÁRIO\r\n'
                                +' Tribunal de Justiça do Estado de Goiás\r\n'
                                +' Gabinete da Presidência\r\n'
                                },
                            ParaStyleName: {
                                type: 'string',
                                value: 'Heading 1' // Estilo de cabeçalho pré-definido
                                }
                        }
                    }
                    }), '*');
            }, 1000);
        }

        if(jsonData.Values.Id == "abrir__") { 
            if(document.getElementById("formFile") != null && document.getElementById("formFile") != undefined) {
                document.getElementById("formFile").click();
            }
        }
    }

    if(jsonData.Values!= undefined && jsonData.Values.Status == "Document_Loaded" && document.getElementById('edicaoTextoDescricao') != null && document.getElementById('edicaoTextoDescricao') != undefined) {
        document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(JSON.stringify({
            "MessageId" : "Insert_Button",
            "Values" : {
              "id" : "abrir__",
              "imgurl" : baseUrl + "/ui/img/folder.svg",
              "hint" : "Abrir",
              "insertBefore" : 'save',
              "label" : "Abrir"
            }
          }), '*');

          document.getElementById('edicaoTextoDescricao').contentWindow.postMessage(JSON.stringify({
            "MessageId" : "Insert_Button",
            "Values" : {
              "id" : "brasao__",
              "imgurl" : baseUrl + "/ui/img/brasao.png",
              "hint" : "Adicionar Brasão",
              "insertBefore" : 'save',
              "label" : "Brasão"
            }
          }), '*');
    }
}

Hey @TawanGMenezes, welcome to the forum. :slight_smile:

Are you successfully getting other PostMessage API stuff working, but just this image insertion is giving you trouble?


Note: I also see this paragraph in the API page:

It is to be noted that as mentioned in WOPI specs, Collabora Online frame will ignore all post messages coming from the host frame if Host_PostmessageReady has not been received. Further, since for embedding Collabora Online as an iframe WOPI implementation is a must, it is required that PostMessageOrigin property is present in WOPI host’s CheckFileInfo response. Otherwise, no post messages will be emitted.

Do you think this applies to your case?


Also helpful would be:

  • What browser/OS are you using?
  • Which Collabora version?

Oh? Sounds interesting. What are you trying to accomplish with your custom button? :slight_smile: