This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0

增加一大票子webkit设置

master
yutent 2023-07-26 13:28:11 +08:00
parent a57caef220
commit f6ac50c4cf
3 changed files with 97 additions and 20 deletions

56
main.py
View File

@ -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):
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')
indicator.connect('activate', self.toggle_visible)
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()

View File

@ -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')
# 图标的点击事件
indicator.connect('activate', toggle_visible)
return indicator
def toggle_visible():
pass

48
notes/webkit-settings.py Normal file
View File

@ -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)