Background
I'm trying to write a greasymonkey script that allow site-A
(Google for example in this discussion) to get and process result from site-B
(yandex for example in this discussion)
The final form looks like this (hopefully):
// script on google.com
async function test1(){
let targetURL = "https://yandex.com/";
let pWin = window.open(targetURL);
let result = await GetAnswer(pWin, "what is A");
alert(result) // "A is not B"
function GetAnswer(win, question){
// get search result by postMessage
}
}
The problem
Forget about the async/await part for now, I have problem communicating with postMessage
already...
If I post message immediately, site-B
got nothing, the message sent before it's ready
let pWin = window.open(targetURL);
pWin.postMessage("search 'what is A'", targetURL);
If I try to post in onload
event, this won't work because onload
event is not accessible for cross-origin pWin
let pWin = window.open(targetURL);
pWin.onload=()=>{
pWin.postMessage("search 'what is A'", targetURL);
}
I found an answer, but can't really understand what it says...
I'm quiet new to postMessage
(the first time using it tbh), is there example or demo to learn how to communicate between cross-origin site (that not owned by me)?