2023-08-31 16:57:55 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# @author yutent<yutent.io@gmail.com>
|
|
|
|
|
# @date 2023/08/08 14:07:26
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gi, os
|
|
|
|
|
|
|
|
|
|
gi.require_version("WebKit2", "4.1")
|
|
|
|
|
|
|
|
|
|
from gi.repository import WebKit2
|
2023-09-06 12:21:56 +08:00
|
|
|
|
from ._version import version
|
2023-08-31 16:57:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(WebKit2.Settings):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
|
|
WebKit2.Settings.__init__(self)
|
|
|
|
|
|
|
|
|
|
self.set_enable_page_cache(True)
|
|
|
|
|
|
|
|
|
|
self.set_javascript_can_access_clipboard(True)
|
|
|
|
|
self.set_javascript_can_open_windows_automatically(True)
|
|
|
|
|
|
|
|
|
|
|
2023-09-06 12:21:56 +08:00
|
|
|
|
self.set_user_agent_with_application_details('WebEngine', version)
|
2023-08-31 16:57:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# indexedDB 和 localStorage 和 离线缓存
|
|
|
|
|
self.set_enable_html5_database(True)
|
|
|
|
|
self.set_enable_html5_local_storage(True)
|
|
|
|
|
self.set_enable_offline_web_application_cache(True)
|
|
|
|
|
|
|
|
|
|
# 平滑滚动
|
|
|
|
|
self.set_enable_smooth_scrolling(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.set_enable_fullscreen(True)
|
|
|
|
|
|
|
|
|
|
# 媒体播放是否需要 用户主动行为
|
|
|
|
|
self.set_media_playback_requires_user_gesture(False)
|
|
|
|
|
|
|
|
|
|
# 允许视频播放窗口不强制全屏
|
|
|
|
|
self.set_media_playback_allows_inline(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_useragent(self, str):
|
|
|
|
|
self.set_user_agent(str)
|
|
|
|
|
|
|
|
|
|
# 关闭之后, 可以随便跨域请求, 但是有安全隐患(不过, 只要保证你页面只运行你自己的代码, 及信任的代码, 就不会有任何问题)
|
|
|
|
|
def disable_security(self):
|
|
|
|
|
self.set_disable_web_security(True)
|
|
|
|
|
|
|
|
|
|
# 禁止全屏 (默认允许)
|
|
|
|
|
def disable_fullscreen(self):
|
|
|
|
|
self.set_enable_fullscreen(False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 启用开发者工具
|
|
|
|
|
def enable_devtools(self):
|
|
|
|
|
self.set_enable_developer_extras(True)
|
|
|
|
|
|
|
|
|
|
# 模拟设备
|
|
|
|
|
def mock_devices(self):
|
|
|
|
|
# 是用于启用或禁用模拟捕获设备的设置选项。
|
|
|
|
|
# 在开发网页应用程序时,经常需要使用摄像头或麦克风等捕获设备进行测试和调试。但是,在某些情况下,可能无法直接访问实际的物理捕获设备,或者不希望在开发过程中实际使用这些设备。
|
|
|
|
|
# 通过启用WebKit2.Settings:enable-mock-capture-devices设置选项,可以使用虚拟的模拟捕获设备来替代实际的物理设备。这样,开发人员可以在没有真实设备的情况下进行捕获设备相关的功能测试和调试,提高开发效率并简化开发流程。
|
|
|
|
|
self.set_enable_mock_capture_devices(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_setting(options = None):
|
|
|
|
|
setting = Settings()
|
|
|
|
|
if options is not None:
|
|
|
|
|
if options.get('devtools'):
|
|
|
|
|
setting.enable_devtools()
|
2023-09-06 12:21:56 +08:00
|
|
|
|
setting.mock_devices()
|
2023-08-31 16:57:55 +08:00
|
|
|
|
|
|
|
|
|
if options.get('useragent'):
|
|
|
|
|
setting.set_useragent(options.get('useragent'))
|
|
|
|
|
|
|
|
|
|
if options.get('web_security') is False:
|
|
|
|
|
setting.disable_security()
|
|
|
|
|
|
|
|
|
|
if options.get('disable_fullscreen'):
|
|
|
|
|
setting.disable_fullscreen()
|
|
|
|
|
|
2023-09-06 12:21:56 +08:00
|
|
|
|
|
2023-08-31 16:57:55 +08:00
|
|
|
|
def wrapper(app, extra = None):
|
|
|
|
|
app.set_settings(setting)
|
|
|
|
|
|
|
|
|
|
return wrapper
|