First, I will explain what I want to do. I am trying to open the payment gateway url from my flutter web app using html.window
. I am able to open the link in next tab and also done the all payment to success. Now, for giving the payment status to my web app back, I am checking the title of that payment gateway tab that contains status and transaction id. But I am unable to do this.
Here is the flutter code first:-
void openURLAndListen(String url)async {
js.context.callMethod('openURLAndListen', [url]);
// Wait for a brief duration to ensure JavaScript function execution
await Future.delayed(const Duration(microseconds: 500));
html.window.addEventListener('PAGE_LOADED', (event){
MessageEvent event2 = event as MessageEvent;
print("Get Message from js ${event2.data}");
});
}
Here is the Javascript function :-
function openURLAndListen(url) {
var openedWindow = window.open(url, '_blank');
openedWindow.onload = function(){
// Get the title of the page
var titles = [];
var tabs = document.querySelectorAll('title');
tabs.forEach(function(tab) {
titles.push(tab.innerText);
});
// Notify Flutter app with the page title
window.parent.postMessage({ type: "PAGE_LOADED", titles: titles }, url);
}
}
This javascript file was called in index.xml file in web folder.
Please give me the proper solution for this, correct me if the function is not correct.
I had tried to use addEventListener to call the title details on opened window.