修复输入法跟随; 增加代理设置API

master
yutent 2023-09-06 12:21:56 +08:00
parent d2f08af833
commit 80d486e87b
6 changed files with 60 additions and 25 deletions

View File

@ -2,9 +2,9 @@
# @author yutent<yutent.io@gmail.com>
# @date 2023/08/31 11:55:25
from ._version import *
from ._webengine import WebEngine
from ._settings import create_setting
from ._hotreload import create_hmr_server
build = (0, 3, 0)
version = '.'.join(map(str, build))

View File

@ -11,13 +11,14 @@ from gi.repository import WebKit2
class Inject:
def __init__(self, webview):
def __init__(self, webview, env = {}):
self.manager = webview.get_user_content_manager()
script_data = open(self.abspath('./inject.js'), 'r').read()
frame = WebKit2.UserContentInjectedFrames.ALL_FRAMES
time = WebKit2.UserScriptInjectionTime.END
script_data = script_data.replace("'{{env}}'", json.dumps(env))
script = WebKit2.UserScript(script_data, frame, time, None, None)
self.manager.add_script(script)

View File

@ -8,6 +8,7 @@ import gi, os
gi.require_version("WebKit2", "4.1")
from gi.repository import WebKit2
from ._version import version
class Settings(WebKit2.Settings):
@ -21,7 +22,7 @@ class Settings(WebKit2.Settings):
self.set_javascript_can_open_windows_automatically(True)
self.set_user_agent_with_application_details('WebEngine', '0.3.0')
self.set_user_agent_with_application_details('WebEngine', version)
# indexedDB 和 localStorage 和 离线缓存
@ -72,6 +73,7 @@ def create_setting(options = None):
if options is not None:
if options.get('devtools'):
setting.enable_devtools()
setting.mock_devices()
if options.get('useragent'):
setting.set_useragent(options.get('useragent'))
@ -82,6 +84,7 @@ def create_setting(options = None):
if options.get('disable_fullscreen'):
setting.disable_fullscreen()
def wrapper(app, extra = None):
app.set_settings(setting)

View File

@ -0,0 +1,4 @@
#!/usr/bin/env python3
build = (0, 3, 0)
version = '.'.join(map(str, build))

View File

@ -3,7 +3,7 @@
# @date 2023/08/08 14:07:26
import gi, os, json, shutil, hashlib
import gi, os, json, shutil, hashlib, time, threading
gi.require_version("WebKit2", "4.1")
@ -52,6 +52,18 @@ def noop():
pass
# 类型js的settimeout的修饰器
def set_timeout(timeout = 0.5):
def decorator(callback):
def wrapper(*args):
t = threading.Timer(timeout, callback, args=args)
t.start()
return t
return wrapper
return decorator
class WebEngine(WebKit2.WebView):
__gsignals__ = {
@ -77,14 +89,10 @@ class WebEngine(WebKit2.WebView):
im = self.get_input_method_context()
im.set_enable_preedit(True)
# 解决输入法候选框跟随问题
im.connect('preedit-started', self.on_preedit_changed)
im.connect('preedit-changed', self.on_preedit_changed)
im.connect('preedit-finished', self.on_preedit_changed)
im.set_enable_preedit(False)
Inject(self).connect(self.called_by_js)
Inject(self, env).connect(self.called_by_js)
def set_root(self, root):
@ -103,14 +111,6 @@ class WebEngine(WebKit2.WebView):
self.load_uri(f"app://{url}")
def on_preedit_changed(self, im):
seat = self.display.get_default_seat()
p = seat.get_pointer().get_position() # 光标位置
x, y = self.get_position() # 窗口位置
im.notify_focus_in()
im.notify_cursor_area(p.x - x, p.y - y, 0, 0) # 修正输入法跟随
def call_js(self, method, data = None, err = None):
if err is not None:
@ -341,6 +341,23 @@ class WebEngine(WebKit2.WebView):
self.notify.create(title, summary, icon, progress, urgency, params.get('callback'))
case 'proxy':
dm = self.get_website_data_manager()
output = True
if params['action'] == 'disable':
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.NO_PROXY, None)
elif params['action'] == 'system':
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.DEFAULT, None)
else:
try:
proxy = WebKit2.NetworkProxySettings(params['url'], params['ignore'])
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.CUSTOM, proxy)
except Exception as err:
_error = err
output = False
case 'md5':
output = hashlib.md5(str(params.get('value'))).hexdigest()

View File

@ -189,10 +189,8 @@ class EventEmitter {
window.native = new EventEmitter()
Object.assign(native, {
async init() {
let env = await handler('init') //.then(r => (native.env = r))
native.env = env
},
env: '{{env}}',
quit() {
return handler('quit', {}, null)
},
@ -403,5 +401,17 @@ Object.assign(native, {
return handler('md5', { value })
},
proxy: {
disable() {
return handler('proxy', { action: 'disable' })
},
system() {
return handler('proxy', { action: 'system' })
},
custom(url = '', ignore = null) {
return handler('proxy', { action: 'enable', url, ignore })
}
},
handler
})