InnovateAsterisk / Browser-Phone

A fully featured browser based WebRTC SIP phone for Asterisk
https://www.innovateasterisk.com
GNU Affero General Public License v3.0
470 stars 239 forks source link

beforeClose() in popup.html #514

Closed prathibhacdac closed 3 months ago

prathibhacdac commented 3 months ago

popup.html

 beforeClose: function( event, ui ) {
                        let phoneContext = $(`#ThePhone${imeiNo}`).get(0).contentWindow;
                        phoneContext.postMessage({action: 'callFunction'},"*");
                        //callCount=phoneContext.countSessions(0);
                        function handleMessageEvent(e) {
                          callCount = e.data.callCount;
                          alert(callCount);
                          console.log("Call count from iframe:", callCount);
                          if (callCount > 0) {
                                console.warn("You are on a call:", callCount);
                                alert("You are on a call, please end the call before closing this window.");
                                windowObj.dialog("open");
                          }
                          // Detach the event listener after it's executed
                        window.removeEventListener("message",handleMessageEvent);
                        }
                // Attach the event listener
                window.addEventListener("message", handleMessageEvent);
            }

Code included in phone.js,

window.addEventListener("message", function(event) {
    if (event.data.action === "callFunction") {
        window.parent.postMessage({callCount: countSessions(0)},"*");
    }
});

Instead of callCount=phoneContext.countSessions(0); I'm passing this data from child to parent. First time while on call countSessions(0) is returning 1, but further it returns 0.

I am facing the following problem: Blocked a frame with origin from accessing a cross-origin frame. Thats why I'm passing the data from child to parent using window.parent.postMessage.

prathibhacdac commented 3 months ago

This issue is resolved by assigning unique id to the iframe.