Skip to content

Commit

Permalink
Switched to using new QWebEngineProfile class
Browse files Browse the repository at this point in the history
This should allow us to set the user_agent for the browser, and carry
active session cookies to new windows.  Should also allow us to set
private browsing mode (aka offTheRecord mode).

Also cleaned up some code cruft from wcgbrowser.
  • Loading branch information
alandmoore committed Jul 24, 2015
1 parent 3bf5f97 commit f3d7502
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Requirements
============

- Python 3
- PyQT5 (v.5.4 or higher)
- PyQT5 (v.5.5 or higher)
- Python YAML library (http://pyyaml.org)

It should work on any platform, but it's only been tested on Arch Linux.
Expand Down
2 changes: 1 addition & 1 deletion admbrowser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ icon_theme: "oxygen"
# user_agent allows the user agent string to be overriden, useful for impersonating other browsers.
# Default: WebKit default user agent

#user_agent: "Firefox IE WebKit Chrome Mosaic Navigator"
#user_agent: "ADMBrowser"

# user_css allows a custom CSS file to be applied to each page ADMBrowser visits. Accepts any URL supported by QT
# Default: none
Expand Down
85 changes: 48 additions & 37 deletions browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from PyQt5.QtWebEngineWidgets import (
QWebEngineView,
QWebEnginePage,
QWebEngineProfile,
QWebEngineSettings
)
from PyQt5.QtNetwork import (
Expand Down Expand Up @@ -300,10 +301,28 @@ def __init__(self, options, parent=None):
self.whitelist = set(self.whitelist) # uniquify and optimize
debug("Generated whitelist: " + str(self.whitelist))

# create the web engine profile
self.create_webprofile()

# Now construct the UI
self.build_ui()

# ## END OF CONSTRUCTOR ## #

def create_webprofile(self):
"""Create a webengineprofile to use in all views."""
if self.config.get("privacy_mode"):
webprofile = QWebEngineProfile()
else:
webprofile = QWebEngineProfile.defaultProfile()
debug("Browser session is private: {}"
.format(webprofile.isOffTheRecord()))
if self.config.get("user_agent"):
webprofile.setHttpUserAgent(self.config["user_agent"])
debug("Set user agent to \"{}\""
.format(webprofile.httpUserAgent()))
self.webprofile = webprofile

def build_ui(self):
"""Set up the user interface for the main window.
Expand All @@ -319,24 +338,33 @@ def build_ui(self):
or """Click here when you are done.
It will clear your browsing history"""
""" and return you to the start page.""")
qb_mode_callbacks = {'close': self.close, 'reset': self.reset_browser}
to_mode_callbacks = {'close': self.close,
'reset': self.reset_browser,
'screensaver': self.screensaver}
qb_mode_callbacks = {
'close': self.close,
'reset': self.reset_browser
}
to_mode_callbacks = {
'close': self.close,
'reset': self.reset_browser,
'screensaver': self.screensaver
}
self.screensaver_active = False

# ##Start GUI configuration## #
self.browser_window = AdmWebView(self.config)
self.browser_window = AdmWebView(
self.config,
webprofile=self.webprofile
)
self.browser_window.setObjectName("web_content")
self.setCentralWidget(self.browser_window)

# Icon theme setting
QIcon.setThemeName(self.config.get("icon_theme"))

if (
self.config.get("icon_theme") is not None
and QT_VERSION_STR > '4.6'
):
QIcon.setThemeName(self.config.get("icon_theme"))
self.setCentralWidget(self.browser_window)
debug("loading {}".format(self.config.get("start_url")))
self.browser_window.setUrl(QUrl(self.config.get("start_url")))

# Window size settings
if self.config.get("window_size", '').lower == 'full':
self.showFullScreen()
elif (self.config.get("window_size", '').lower() == 'max'):
Expand Down Expand Up @@ -603,8 +631,9 @@ def __init__(self, config, parent=None, **kwargs):
super(AdmWebView, self).__init__(parent)
self.kwargs = kwargs
self.config = config
self.setPage(AdmWebPage())
self.page().user_agent = config.get('user_agent', None)
# create a web profile for the pages
self.webprofile = kwargs.get("webprofile")
self.setPage(AdmWebPage(None, self.webprofile))
self.page().force_js_confirm = config.get("force_js_confirm")
self.settings().setAttribute(
QWebEngineSettings.JavascriptCanOpenWindows,
Expand Down Expand Up @@ -637,18 +666,6 @@ def __init__(self, config, parent=None, **kwargs):
self.page().printRequested.connect(self.print_webpage)
self.print_action.setToolTip("Print this web page")

# Set up the proxy if there is one set
# if config.get("proxy_server"):
# if ":" in config["proxy_server"]:
# proxyhost, proxyport = config["proxy_server"].split(":")
# else:
# proxyhost = config["proxy_server"]
# proxyport = 8080
# Not sure how to set proxy server for QWebEngineView??
# self.nam.setProxy(QNetworkProxy(
# QNetworkProxy.HttpProxy, proxyhost, int(proxyport)
# ))

# connections for admwebview
self.page().authenticationRequired.connect(
self.auth_dialog
Expand All @@ -669,9 +686,8 @@ def createWindow(self, type):
"""
if self.config.get("allow_popups"):
self.popup = AdmWebView(
None,
self.config,
networkAccessManager=self.nam,
parent=None,
**self.kwargs
)
# This assumes the window manager has an "X" icon
Expand Down Expand Up @@ -923,10 +939,13 @@ class AdmWebPage(QWebEnginePage):
This was subclassed so that some functions can be overridden.
"""
def __init__(self, parent=None):
def __init__(self, parent=None, profile=None):
"""Constructor for the class"""
super(AdmWebPage, self).__init__(parent)
self.user_agent = None
debug(profile.httpUserAgent())
if not profile:
super(AdmWebPage, self).__init__(parent)
else:
super(AdmWebPage, self).__init__(profile, parent)

def javaScriptConsoleMessage(self, message, line, sourceid):
"""Handle console.log messages from javascript.
Expand Down Expand Up @@ -955,14 +974,6 @@ def javaScriptAlert(self, frame, msg):
if not self.suppress_alerts:
return QWebEnginePage.javaScriptAlert(self, frame, msg)

def userAgentForUrl(self, url):
"""Handle reqests for the browser's user agent
Overridden from QWebEnginePage so we can force
a user agent from the config.
"""
return self.user_agent or QWebEnginePage.userAgentForUrl(self, url)

def certificateError(self, error):
"""Handle SSL errors in the browser.
Expand Down

0 comments on commit f3d7502

Please sign in to comment.