From f6ac50c4cfbd576c47ca92273af22639f7e60ec0 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 26 Jul 2023 13:28:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E5=A4=A7=E7=A5=A8?= =?UTF-8?q?=E5=AD=90webkit=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 58 +++++++++++++++++++++++++++------------- notes/tray.py | 11 +++++++- notes/webkit-settings.py | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 notes/webkit-settings.py diff --git a/main.py b/main.py index b9183a1..41ea53b 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,14 @@ gi.require_version("WebKit2", "4.1") from gi.repository import Gtk, Gdk, WebKit2, GLib from gi.repository.GdkPixbuf import Pixbuf +# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon +try: + gi.require_version('AyatanaAppIndicator3', '0.1') + # 需要安装这个包 gir1.2-ayatanaappindicator3-0.1 + from gi.repository import AyatanaAppIndicator3 as AppIndicator3 +except (ValueError, ImportError): + AppIndicator3 = None + class WebKitWindow(Gtk.Window): def __init__(self): @@ -21,10 +29,18 @@ class WebKitWindow(Gtk.Window): settings.set_enable_page_cache(True) settings.set_enable_offline_web_application_cache(True) - settings.set_property('javascript-can-access-clipboard', True) - settings.set_property('javascript-can-open-windows-automatically', True) + settings.set_enable_developer_extras(True) - settings.set_property("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) weapp/1.0.0 Version/16.4 Safari/605.1.15") + + + + settings.set_enable_html5_database(True) + settings.set_enable_html5_local_storage(True) + + settings.set_javascript_can_access_clipboard(True) + settings.set_javascript_can_open_windows_automatically(True) + + settings.set_user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) weapp/1.0.0 Version/16.4 Safari/605.1.15") manager = WebKit2.UserContentManager() @@ -42,10 +58,9 @@ class WebKitWindow(Gtk.Window): self.webview.set_settings(settings) - self.webview.connect("decide-policy", self.on_decide_policy) - self.webview.load_uri("http://127.0.0.1:10086/index.html") - # self.webview.load_uri("https://benchmark.wkit.fun") + # self.webview.load_uri("http://127.0.0.1:10086/index.html") + self.webview.load_uri("https://benchmark.wkit.fun") self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) @@ -58,9 +73,24 @@ class WebKitWindow(Gtk.Window): def create_tray(self): - indicator = Gtk.StatusIcon.new_from_icon_name('youtube') - indicator.connect('activate', self.toggle_visible) + + if AppIndicator3 : + indicator = AppIndicator3.Indicator.new( + "youtube", + "youtube", + AppIndicator3.IndicatorCategory.APPLICATION_STATUS + ) + indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) + else: + # windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找) + indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) + # linux + indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + return indicator + # indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + # indicator.connect('activate', self.toggle_visible) + # return indicator def toggle_visible(self, icon): @@ -157,6 +187,7 @@ class WebKitWindow(Gtk.Window): scripts = 'native.$emit("' + callback + '",' + json.dumps(image) + ')' + self.webview.send_message_to_page(WebKit2.UserMessage.new('blabla')) self.webview.evaluate_javascript(scripts, -1) case _: @@ -168,17 +199,6 @@ class WebKitWindow(Gtk.Window): - def on_decide_policy(self, webview, decision, decision_type): - if decision_type == WebKit2.PolicyDecisionType.NAVIGATION_ACTION: - navigation_action = decision.get_navigation_action() - request = navigation_action.get_request() - uri = request.get_uri() - if uri == "about:blank": - # Open the developer tools window - inspector = webview.get_inspector() - inspector.show() - else: - decision.use() diff --git a/notes/tray.py b/notes/tray.py index 3e44f52..9ea36ab 100644 --- a/notes/tray.py +++ b/notes/tray.py @@ -48,10 +48,19 @@ def create_tray(): "youtube", AppIndicator3.IndicatorCategory.APPLICATION_STATUS ) + # 加上这句, 图标才会显示 + indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) else: # windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找) indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) # linux indicator = Gtk.StatusIcon.new_from_icon_name('youtube') - return indicator \ No newline at end of file + # 图标的点击事件 + indicator.connect('activate', toggle_visible) + + return indicator + + +def toggle_visible(): + pass \ No newline at end of file diff --git a/notes/webkit-settings.py b/notes/webkit-settings.py new file mode 100644 index 0000000..bc88804 --- /dev/null +++ b/notes/webkit-settings.py @@ -0,0 +1,48 @@ +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("WebKit2", "4.1") + +from gi.repository import Gtk, WebKit2 + +settings = WebKit2.Settings() + + +settings.set_enable_page_cache(True) +settings.set_enable_offline_web_application_cache(True) + +settings.set_javascript_can_access_clipboard(True) +settings.set_javascript_can_open_windows_automatically(True) + +settings.set_user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) weapp/1.0.0 Version/16.4 Safari/605.1.15") + + +# indexedDB 和 localStorage 和 离线缓存 +settings.set_enable_html5_database(True) +settings.set_enable_html5_local_storage(True) +settings.set_enable_offline_web_application_cache(True) + +# 平滑滚动 +settings.set_enable_smooth_scrolling(True) + + +settings.set_enable_fullscreen(True) + +# 媒体播放是否需要 用户主动行为 +settings.set_media_playback_requires_user_gesture(False) + +# 允许视频播放窗口不强制全屏 +settings.set_media_playback_allows_inline(True) + + +# 关闭之后, 可以随便跨域请求, 但是有安全隐患(不过, 只要保证你页面只运行你自己的代码, 及信任的代码, 就不会有任何问题) +settings.set_disable_web_security(True) + + +# 允许开发者控制台 +settings.set_enable_developer_extras(True) + +# 是用于启用或禁用模拟捕获设备的设置选项。 +# 在开发网页应用程序时,经常需要使用摄像头或麦克风等捕获设备进行测试和调试。但是,在某些情况下,可能无法直接访问实际的物理捕获设备,或者不希望在开发过程中实际使用这些设备。 +# 通过启用WebKit2.Settings:enable-mock-capture-devices设置选项,可以使用虚拟的模拟捕获设备来替代实际的物理设备。这样,开发人员可以在没有真实设备的情况下进行捕获设备相关的功能测试和调试,提高开发效率并简化开发流程。 +settings.set_enable_mock_capture_devices(True) \ No newline at end of file