Added webkit ui class with browser to python calling logic

This commit is contained in:
2024-01-04 23:49:43 -06:00
parent 873b415f47
commit d49bcd8beb
11 changed files with 330 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from ..widgets.webkit_ui import WebkitUI
@@ -41,6 +42,7 @@ class CenterContainer(Gtk.Box):
self.add(button)
self.add(glade_box)
self.add( WebkitUI() )
def _hello_world(self, widget = None, eve = None):
logger.debug("Hello, World!")
logger.debug("Hello, World!")

View File

@@ -0,0 +1,82 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gdk
from gi.repository import WebKit2
# Application imports
class WebkitUI(WebKit2.WebView):
def __init__(self):
super(WebkitUI, self).__init__()
self._setup_styling()
self._subscribe_to_events()
self._load_view()
self._setup_content_manager()
self.show_all()
def _setup_styling(self):
self.set_vexpand(True)
self.set_hexpand(True)
self.set_background_color( Gdk.RGBA(0, 0, 0, 0.0) )
def _subscribe_to_events(self):
# event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc)
...
def _load_settings(self):
self.set_settings( WebkitUISettings() )
def _load_view(self):
path = settings_manager.get_context_path()
data = settings_manager.wrap_html_to_body("")
self.load_html(content = data, base_uri = f"file://{path}/")
def _setup_content_manager(self):
content_manager = self.get_user_content_manager()
content_manager.register_script_message_handler("backend")
content_manager.connect("script-message-received", self._process_js_message)
def _process_js_message(self, user_content_manager, js_result):
js_value = js_result.get_js_value()
print(js_value.to_string())
# self._web_view.run_javascript("do_stuff()", None, None)
class WebkitUISettings(WebKit2.Settings):
def __init__(self):
super(WebkitUISettings, self).__init__()
self._set_default_settings()
# Note: Highly insecure setup but most "app" like setup I could think of.
# Audit heavily any scripts/links ran/clicked under this setup!
def _set_default_settings(self):
self.set_enable_offline_web_application_cache(False)
self.enable_html5_local_storage(False)
self.enable_html5_database(False)
self.enable_xss_auditor(False)
self.set_enable_hyperlink_auditing(False)
self.set_enable_tabs_to_links(False)
self.enable_fullscreen(False)
self.set_print_backgrounds(False)
self.enable_webaudio(False)
self.set_enable_page_cache(False)
self.enable_accelerated_2d_canvas(True)
self.set_allow_file_access_from_file_urls(True)
self.set_allow_universal_access_from_file_urls(True)
self.set_enable_webrtc(True)
sdelf.set_user_agent(f"{app_name}")

View File

@@ -13,6 +13,7 @@ from os import mkdir
# Application imports
from ..singleton import Singleton
from .start_check_mixin import StartCheckMixin
from .markdown_template_mixin import MarkdownTemplateMixin
from .options.settings import Settings
@@ -22,7 +23,7 @@ class MissingConfigError(Exception):
class SettingsManager(StartCheckMixin, Singleton):
class SettingsManager(StartCheckMixin, MarkdownTemplateMixin, Singleton):
def __init__(self):
self._SCRIPT_PTH = path.dirname(path.realpath(__file__))
self._USER_HOME = path.expanduser('~')
@@ -30,6 +31,7 @@ class SettingsManager(StartCheckMixin, Singleton):
self._USR_PATH = f"/usr/share/{app_name.lower()}"
self._USR_CONFIG_FILE = f"{self._USR_PATH}/settings.json"
self._CONTEXT_PATH = f"{self._HOME_CONFIG_PATH}/context_path"
self._PLUGINS_PATH = f"{self._HOME_CONFIG_PATH}/plugins"
self._DEFAULT_ICONS = f"{self._HOME_CONFIG_PATH}/icons"
self._CONFIG_FILE = f"{self._HOME_CONFIG_PATH}/settings.json"
@@ -146,6 +148,7 @@ class SettingsManager(StartCheckMixin, Singleton):
def get_ui_widgets_path(self) -> str: return self._UI_WIDEGTS_PATH
def get_context_menu_data(self) -> str: return self._context_menu_data
def get_context_path(self) -> str: return self._CONTEXT_PATH
def get_plugins_path(self) -> str: return self._PLUGINS_PATH
def get_icon_theme(self) -> str: return self._ICON_THEME
def get_css_file(self) -> str: return self._CSS_FILE

View File

@@ -0,0 +1,60 @@
# Python imports
# Lib imports
# Application imports
class MarkdownTemplateMixin:
def wrap_html_to_body(self, html):
return f"""\
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Markdown View</title>
<style media="screen">
html, body {{
display: block;
background-color: #32383e00;
color: #ffffff;
text-wrap: wrap;
}}
img {{
width: 100%;
height: auto;
}}
code {{
border: 1px solid #32383e;
background-color: #32383e;
padding: 4px;
}}
</style>
</head>
<body>
{html}
<span>Button in WebView
<button onclick="say_hello()">Button in WebView</button>
</span>
<!-- For internal scripts... -->
<script src="js/libs/jquery-3.7.1.min.js"></script>
<!-- For Bootstrap... -->
<script src="resources/js/libs/bootstrap5/bootstrap.bundle.min.js"></script>
<!-- For Application... -->
<script src="resources/js/ui-logic.js"></script>
<script src="resources/js/post-ajax.js"></script>
<script src="resources/js/ajax.js"></script>
<script src="resources/js/events.js"></script>
</body>
</html>
"""