InnovateAsterisk / Browser-Phone

A fully featured browser based WebRTC SIP phone for Asterisk

Home Page:https://www.innovateasterisk.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

beforeClose() in popup.html

prathibhacdac opened this issue · comments

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.

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