Skip to content

Commit

Permalink
Merge pull request #4 from MarcinOrlowski/dev
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
MarcinOrlowski authored Oct 28, 2024
2 parents ec111c1 + b364335 commit f8ec150
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

---

* 1.2.0-dev (2024-10-09)
* 1.3.0 (2024-10-28)
* Enabled clipboard access for the embedded browser (so all the "Copy" buttons now work).
* If both '--no-tray' and `--minimized` options are given `--minimized` is being ignored.

* 1.2.0 (2024-10-09)
* Added support for downloading files.
* Added `--version` switch.

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ NOTE: `--zoom` accepts fractional values, so you can use i.e. `--zoom 1.25` to s

## Current limitations

* Due to security based limitations of embedded `QWebBrowerView` you will not be able
to save any file to your local storage nor filesystem.
* Website's Javascript code cannot write to system clipboard so you might need to manually
select given portion of the site and copy using function from context menu as any buttons
on the page that is using JS to write to the host's clipboard will not currently work.
Expand Down
2 changes: 1 addition & 1 deletion websiteapp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class Version(Enum):
MAJOR = 1
MINOR = 2
MINOR = 3
PATCH = 0

@classmethod
Expand Down
71 changes: 59 additions & 12 deletions websiteapp/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@
import fasteners
from PySide6.QtCore import QUrl, QFileSystemWatcher, QStandardPaths, Qt
from PySide6.QtGui import QAction
from PySide6.QtWebEngineCore import QWebEngineProfile, QWebEnginePage, QWebEngineSettings
from PySide6.QtWebEngineCore import (
QWebEngineProfile,
QWebEngineSettings,
)
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QSystemTrayIcon, \
QMenu, QFileDialog
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QVBoxLayout,
QWidget,
QSystemTrayIcon,
QMenu,
QFileDialog,
)
from PySide6.QtWebEngineCore import QWebEnginePage

from websiteapp.about import About
from websiteapp.const import Const
Expand Down Expand Up @@ -69,19 +80,30 @@ def __init__(self):
self.profile = QWebEngineProfile(self.args.profile, self)
self.page = QWebEnginePage(self.profile, self)

# Create and configure the webpage
self.page = QWebEnginePage(self.profile, self)
# Connect permission request handler
self.page.featurePermissionRequested.connect(self.handle_permission_request)

# Handle downloads
self.profile.downloadRequested.connect(self.on_download_requested)

self.browser = QWebEngineView(self)
web_settings = self.browser.settings()
web_settings.setAttribute(
QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)

# Enable all required clipboard permissions
web_settings.setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)
web_settings.setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanPaste, True)
web_settings.setAttribute(QWebEngineSettings.WebAttribute.AllowWindowActivationFromJavaScript, True)
# Additional profile settings for clipboard
self.profile.settings().setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)
self.profile.settings().setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanPaste, True)

self.browser.setPage(self.page)
self.browser.setZoomFactor(self.args.zoom)

# Ensure the browser widget can receive focus and key events
self.browser.setFocusPolicy(Qt.StrongFocus)
self.browser.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.browser.setFocus()

self.dbug(f'URL: {self.args.url}')
Expand Down Expand Up @@ -244,12 +266,8 @@ def run() -> None:
window = WebApp()

if window.args.minimized and window.args.no_tray:
print('Cannot use --no-tray and --minimized at the same time.')
if window.lock:
window.lock.release()
window.lock = None
app.quit()
sys.exit(1)
# Cannot use --no-tray and --minimized at the same time. Ignoring --minimized
window.args.minimized = False

if not window.args.minimized:
window.show()
Expand Down Expand Up @@ -277,3 +295,32 @@ def on_download_requested(self, download):
download.accept()
else:
download.cancel()

def handle_permission_request(self, origin, feature):
"""
Handle permission requests from the webpage.
"""
from PySide6.QtWebEngineCore import QWebEnginePage

self.dbug(f"Permission requested: {feature} from {origin}")

# Define all clipboard-related features
clipboard_features = [
QWebEnginePage.Feature.ClipboardReadWrite,
QWebEnginePage.Feature.Clipboard, # For backwards compatibility
]

if feature in clipboard_features:
self.dbug(f"Granting clipboard permission for feature: {feature}")
self.page.setFeaturePermission(
origin,
feature,
QWebEnginePage.PermissionPolicy.PermissionGrantedByUser
)
else:
self.dbug(f"Denying permission for feature: {feature}")
self.page.setFeaturePermission(
origin,
feature,
QWebEnginePage.PermissionPolicy.PermissionDeniedByUser
)

0 comments on commit f8ec150

Please sign in to comment.