Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-90970

QWebEnginePage::acceptNavigationRequest() was not called for external object

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.12.11
    • WebEngine
    • None

    Description

      [QWebEnginePage::acceptNavigationRequest()](https://doc.qt.io/qt-5/qwebenginepage.html#acceptNavigationRequest) was not called for external objects with with relative links, even when the base url for [setHtml()](https://doc.qt.io/qt-5/qwebenginepage.html#setHtml) was set. I would give a PyQt5 example for test:

       

          from PyQt5.QtCore import *
          from PyQt5.QtGui import *
          from PyQt5.QtWidgets import *
          from PyQt5.QtWebEngineWidgets import *
          from PyQt5.QtWebEngineCore import *
          import sys,os
          class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
              def __init__(self, parent=None):
                  super().__init__(parent)
              def interceptRequest(self, info):
                  # if info.requestUrl().toString().startswith('http://changyan.itc.cn/mdevp/extensions/first-login-prompt'):
                  self.targetUrl = info.requestUrl().toString()
                  print('QWebEngineUrlRequestInterceptor----------------------------------------------', self.targetUrl)
          
          class WebEnginePage(QWebEnginePage):  
            def __init__(self, parent=None, mdicts=[]):
              super().__init__(parent)
          
            def acceptNavigationRequest(self, url, navigationType, isMainFrame):  # Navigation requests can be delegated to the Qt application instead of having the HTML handler engine process them by overloading this function. This is necessary when an HTML document is used as part of the user interface, and not to display external data, for example, when displaying a list of results.# The QWebEngineUrlRequestInterceptor class offers further options for intercepting and manipulating requests.
              urlStr = url.toString()
              print('acceptNavigationRequest----------', urlStr)
              return True  # If the function returns true, the navigation request is accepted and url is loaded.
          
          
          class WebEngineView(QWebEngineView):
              def __init__(self, parent=None):
                  super().__init__(parent)
                  p=WebEnginePage()
                  self.setPage(p)
                  self.webPage = self.page()  # QWebEnginePage()
                  print(p==self.webPage)
                  # # self.webPage.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls, True)
          
          
                  self.webPage.profile().setUrlRequestInterceptor(WebEngineUrlRequestInterceptor(self)) 
                  self.webPage.setHtml('<a href="sound://uk/want__gb_1.spx">sound scheme</a><img src="uk_pron.png"/>', baseUrl=QUrl.fromLocalFile(os.getcwd()+os.path.sep))#For external objects to be loaded, baseUrl cannot be empty;os.getcwd()+os.path.sep必须,不加的话没有trailing slash,会让webview用父路径加载资源→出不来
                  # self.webPage.setHtml('<a href="sound://uk/want__gb_1.spx">sound scheme</a><img src="uk_pron.png"/>', baseUrl=QUrl('file://1.0.3.1'))
                  
          if __name__ == "__main__":
              app = QApplication(sys.argv)
              webEngineView = WebEngineView()
              webEngineView.show()
              sys.exit(app.exec_())
      
      

      I think we should be able to see printed message

      acceptNavigationRequest---------- [+file:///H:/BaiduYunDownload/programming/Python/uk_pron.png+]

      if QWebEnginePage::acceptNavigationRequest() worked properly .

       

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt_webengine_team Qt WebEngine Team
            philip Philip Lee
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes