How to open web API calls to the default browser window in PyQt5 Python

Viewed 22

I have a browser code built in python using PyQt5 and I want to implement a functionality which is whenever the browser receives an API call from a website to some other service it should open that in the default browser. For example, whenever we want to login to a website using google and we click on the google option, we get a new window to select our google account. That's the same functionality I want to implement.

Find the complete code here: https://pastebin.com/41n9eghQ

class WebPage(QWebEnginePage):

    linkClicked = Signal(QUrl)

    def acceptNavigationRequest(self, url, navigation_type, isMainFrame):
        if navigation_type == QWebEnginePage.NavigationTypeLinkClicked:
            self.linkClicked.emit(url)
            return False

        return super(WebPage, self).acceptNavigationRequest(
            url, navigation_type, isMainFrame)

The above class inside the code accepts and processes the in-browser navigation requests.

def createWindow(self, webwindowtype):
        import webbrowser
        try:
            webbrowser.open(to_text_string(self.url().toString()))
        except ValueError:
            pass

The above function opens the window in default browser whenever an API call gets triggered, the only problem here is right now I am passing the url of the current website i.e. self.url() and not the url of the API and I don't understand how to do so.

Is there a way to capture the API request and pass that url to the webbrowser.open() function.

Any help would be appreciated, thanks for your attention!

0 Answers
Related