Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
How to add additional window.onmessage in javascript?

I made some javascript tool for internal usage that will be injected to some of our websites. My tool should provide communitcation between our main (host) websita and some iframe, so the tool itself is injected without the iframe tag. I don't know what exactly we will have on this website(s), they might or might not use window.onmessage. In my JS tool I need to use window.onmessage, so if the host website uses it as well, we will have a conflict. Can you, please, advise, how can I add additional onmessage or check and if it exist and wrapp it somehow ?

Here is an example of my code in the JS tool:

window.onmessage = (event) => {
    try{
        separator = "[@-------->]";
        GetReqEventTag = 'PluginGetRequestEvent'
        var content = event.data;
        if((typeof content === 'string' || content instanceof String) && content.startsWith(GetReqEventTag)){


            var requestParams = content.split(separator);
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                    event.source.window.postMessage([GetReqEventTag, requestParams[1], xmlHttp.responseText].join(separator), '*')
                }
            };
            xmlHttp.open("GET", requestParams[2], true);
            xmlHttp.send(null);
        }
    }
    catch (ex) {
        console.error("Unable to send request to Plugin backend: " +ex, ex.stack);
    }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda