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