Search code examples
pyqtpyqt5qwebviewqfiledialog

Disable QWebEngineView access to QFileDialog


I'm trying to develop a browser that allows downloads but disables all ways for uploading something to the internet. I developed the browser with PyQt5 but I can't figure out how to disable the FileDialog (ex when clicking on add an attachment in Gmail). I already set up settings disabling things like LocalContentCanAccessRemoteUrls and LocalContentCanAccessFileUrls among other flags. I also subclassed the QWebEnginePage and modified the "chooseFile" function without any luck. Below you can see my code, here you can find the UI and resources file.

# Python import
import sys

# PYQT Imports
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
# from QtWebEngineWidgets.QWebEngineView import QWebEngineView

# UI IMPORT
import UI_Sailor


class WebPage(QtWebEngineWidgets.QWebEnginePage):
    def chooseFile(self, frame=None, path=''):
        print ('Called this')
        return


class Sailor(QtWidgets.QMainWindow, UI_Sailor.Ui_Sailor):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        # self.setWindowIcon(QtGui.QIcon(':/Icons/Icons/Icon_Small.png'))
        self.setWindowTitle('Sailor - 1.0.0')

        glob_settings = QtWebEngineWidgets.QWebEngineSettings.globalSettings()
        self.setWebSettings(glob_settings)

        self.create_connections()

    def setWebSettings(self, object_settings):
        print ('Called')
        # Global web settings
        object_settings.setAttribute(1, True)  # JaveScript
        object_settings.setAttribute(3, False)  # Clipboard (JaveScript)
        object_settings.setAttribute(5, False)  # Local Storage
        object_settings.setAttribute(6, False)  # LocalContentCanAccessRemoteUrls
        object_settings.setAttribute(7, False)  # XSSAuditingEnabled
        object_settings.setAttribute(9, False)  # LocalContentCanAccessFileUrls
        object_settings.setAttribute(11, True)  # ScrollAnimatorEnabled
        object_settings.setAttribute(14, True)  # FullScreenSupportEnabled

        # Check that the options are applied
        # print (object_settings.testAttribute(1))  # JaveScript
        # print (object_settings.testAttribute(2))  # JaveScript
        # print (object_settings.testAttribute(3))  # Clipboard (JaveScript)
        # print (object_settings.testAttribute(5))  # Local Storage
        # print (object_settings.testAttribute(6))  # LocalContentCanAccessRemoteUrls
        # print (object_settings.testAttribute(7))  # XSSAuditingEnabled
        # print (object_settings.testAttribute(9))  # LocalContentCanAccessFileUrls
        # print (object_settings.testAttribute(11))  # ScrollAnimatorEnabled
        # print (object_settings.testAttribute(14))  # FullScreenSupportEnabled

    def create_connections(self):
        self.TXT_Address.returnPressed.connect(self.web_load_url)
        # self.WEB_View.urlChanged.connect(self.debug)

    def web_load_url(self):
        # Creates the proper url
        url_base = str(self.TXT_Address.text())
        url_clean = self.url_handler(url_base)
        # Loads the url
        # self.WEB_View.setUrl(QtCore.QUrl(url_clean))
        self.WEB_View.setPage(WebPage(self.WEB_View))
        self.WEB_View.load(QtCore.QUrl(url_clean))

        self.setWebSettings(self.WEB_View.settings())


    def debug(self):
        print ('called')


    def url_handler(self, url):
        if 'http' not in url:
            return 'http://{}'.format(url)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = Sailor()
    form.show()
    app.exec_()

Solution

  • You defined the wrong method in the WebPage class. It looks like you may have used the signature from the QtWebKit.QWebPage class, rather than the one from the QWebEnginePage class. Here is a minimal working example:

    import sys
    from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
    
    class WebPage(QtWebEngineWidgets.QWebEnginePage):
        def chooseFiles(self, mode, oldfiles, mimetypes):
            print('Called this')
            return []
    
    class Window(QtWebEngineWidgets.QWebEngineView):
        def __init__(self):
            super(Window, self).__init__()
            self.setPage(WebPage(self))
            self.setHtml('<input type="file">')
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.setGeometry(600, 100, 400, 200)
        window.show()
        sys.exit(app.exec_())