def __init__(self, config): self.config = config super(JWebView, self).__init__() self.setAttribute(Qt.WA_DeleteOnClose, True) self.profile = QWebEngineProfile.defaultProfile() self.webpage = JWebPage(self.profile, self, config) self.setPage(self.webpage) if config["inject_JavaScript"]["JavaScript"]: from JAK.Utils import JavaScript JavaScript.inject(self.page(), config["inject_JavaScript"]) self.interceptor = Interceptor(config) if config["user_agent"]: # Set user agent self.profile.setHttpUserAgent(config["user_agent"]) if config["debug"]: self.settings().setAttribute(QWebEngineSettings.XSSAuditingEnabled, True) else: self.setContextMenuPolicy(Qt.PreventContextMenu) if config["transparent"]: # Activates background transparency self.setAttribute(Qt.WA_TranslucentBackground) self.page().setBackgroundColor(Qt.transparent) self.setStyleSheet("background:transparent;") print( "Transparency detected, make sure you set [ body {background:transparent;} ]" ) # * Set Engine options if self.config["JavascriptCanPaste"]: self.settings().setAttribute(QWebEngineSettings.JavascriptCanPaste, True) else: self.settings().setAttribute(QWebEngineSettings.JavascriptCanPaste, False) if self.config["PlaybackRequiresUserGesture"]: self.settings().setAttribute( QWebEngineSettings.PlaybackRequiresUserGesture, True) else: self.settings().setAttribute( QWebEngineSettings.PlaybackRequiresUserGesture, False) if self.config["FullScreenSupportEnabled"]: self.settings().setAttribute( QWebEngineSettings.FullScreenSupportEnabled, True) else: self.settings().setAttribute( QWebEngineSettings.FullScreenSupportEnabled, False) if self.config["AllowWindowActivationFromJavaScript"]: self.settings().setAttribute( QWebEngineSettings.AllowWindowActivationFromJavaScript, True) else: self.settings().setAttribute( QWebEngineSettings.AllowWindowActivationFromJavaScript, False) if self.config["LocalContentCanAccessRemoteUrls"]: self.settings().setAttribute( QWebEngineSettings.LocalContentCanAccessRemoteUrls, True) else: self.settings().setAttribute( QWebEngineSettings.LocalContentCanAccessRemoteUrls, False) if self.config["JavascriptCanAccessClipboard"]: self.settings().setAttribute( QWebEngineSettings.JavascriptCanAccessClipboard, True) else: self.settings().setAttribute( QWebEngineSettings.JavascriptCanAccessClipboard, False) if self.config["SpatialNavigationEnabled"]: self.settings().setAttribute( QWebEngineSettings.SpatialNavigationEnabled, True) else: self.settings().setAttribute( QWebEngineSettings.SpatialNavigationEnabled, False) if self.config["TouchIconsEnabled"]: self.settings().setAttribute(QWebEngineSettings.TouchIconsEnabled, True) else: self.settings().setAttribute(QWebEngineSettings.TouchIconsEnabled, False) if self.config["FocusOnNavigationEnabled"]: self.settings().setAttribute( QWebEngineSettings.FocusOnNavigationEnabled, True) else: self.settings().setAttribute( QWebEngineSettings.FocusOnNavigationEnabled, False) if config["online"]: self.settings().setAttribute(QWebEngineSettings.DnsPrefetchEnabled, True) print("Engine online (IPC) Disabled") self.page().profile().downloadRequested.connect( self._download_requested) # Set persistent cookies self.profile.setPersistentCookiesPolicy( QWebEngineProfile.ForcePersistentCookies) # set cookies on user folder if config["cookies_path"]: # allow specific path per application. _cookies_path = f"{os.getenv('HOME')}/.jak/{config['cookies_path']}" else: # use separate cookies database per application title = config["title"].lower().replace(" ", "-") _cookies_path = f"{os.getenv('HOME')}/.jak/{title}" self.profile.setPersistentStoragePath(_cookies_path) print(f"Cookies PATH:{_cookies_path}") else: self.settings().setAttribute(QWebEngineSettings.ShowScrollBars, False) print("Engine interprocess communication (IPC) up and running:") self._ipc_scheme_handler = IpcSchemeHandler() self.profile.installUrlSchemeHandler('ipc'.encode(), self._ipc_scheme_handler) if config["webChannel"]["active"]: from JAK.Utils import JavaScript if bindings() == "PyQt5": from PyQt5.QtCore import QFile, QIODevice from PyQt5.QtWebChannel import QWebChannel webchannel_js = QFile(':/qtwebchannel/qwebchannel.js') webchannel_js.open(QIODevice.ReadOnly) webchannel_js = bytes(webchannel_js.readAll()).decode('utf-8') webchannel_js += """ var JAK; new QWebChannel(qt.webChannelTransport, function (channel) { JAK = channel.objects.Bridge; });""" JavaScript.inject(self.page(), { "JavaScript": webchannel_js, "name": "QWebChannel API" }) self.channel = QWebChannel(self.page()) if config["webChannel"]["shared_obj"]: self.bridge_obj = config["webChannel"]["shared_obj"] else: raise NotImplementedError("QWebChannel shared QObject") self.channel.registerObject("Bridge", self.bridge_obj) self.page().setWebChannel(self.channel) print("QWebChannel bridge active") self.profile.setRequestInterceptor(self.interceptor) print(self.profile.httpUserAgent()) validate_url(self, config["web_contents"])
def _inject_script(self, script: dict): from JAK.Utils import JavaScript JavaScript.inject(self.page(), script)