禁止 QWebEngineView 访问 QFileDialog
Disable QWebEngineView access to QFileDialog
我正在尝试开发一种允许下载但禁止将某些内容上传到 Internet 的所有方式的浏览器。我用 PyQt5 开发了浏览器,但我不知道如何禁用 FileDialog(例如,在 Gmail 中单击添加附件时)。我已经设置了禁用 LocalContentCanAccessRemoteUrls 和 LocalContentCanAccessFileUrls 等标志的设置。我还继承了 QWebEnginePage 并修改了 "chooseFile" 函数,但没有任何运气。下面你可以看到我的代码,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_()
您在 WebPage
class 中定义了错误的方法。看起来您可能使用了来自 QtWebKit.QWebPage
class 的签名,而不是来自 QWebEnginePage class 的签名。这是一个最小的工作示例:
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_())
我正在尝试开发一种允许下载但禁止将某些内容上传到 Internet 的所有方式的浏览器。我用 PyQt5 开发了浏览器,但我不知道如何禁用 FileDialog(例如,在 Gmail 中单击添加附件时)。我已经设置了禁用 LocalContentCanAccessRemoteUrls 和 LocalContentCanAccessFileUrls 等标志的设置。我还继承了 QWebEnginePage 并修改了 "chooseFile" 函数,但没有任何运气。下面你可以看到我的代码,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_()
您在 WebPage
class 中定义了错误的方法。看起来您可能使用了来自 QtWebKit.QWebPage
class 的签名,而不是来自 QWebEnginePage class 的签名。这是一个最小的工作示例:
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_())