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
py-gtk-notes/main.py

191 lines
5.1 KiB
Python
Raw Normal View History

2023-07-21 19:13:51 +08:00
#!/usr/bin/python3
import gi, json, os
2023-07-21 19:13:51 +08:00
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1")
from gi.repository import Gtk, Gdk, WebKit2, GLib
from gi.repository.GdkPixbuf import Pixbuf
2023-07-24 15:05:01 +08:00
2023-07-21 19:13:51 +08:00
class WebKitWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="WebKit Example")
self.set_default_size(800, 600)
settings = WebKit2.Settings()
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_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")
manager = WebKit2.UserContentManager()
script = open(self.file_path('./inject.js'), 'r').read()
frame = WebKit2.UserContentInjectedFrames.ALL_FRAMES
time = WebKit2.UserScriptInjectionTime.END
script = WebKit2.UserScript(script, frame, time, None, None)
manager.add_script(script)
manager.connect('script-message-received::app', self.on_script_message)
manager.register_script_message_handler('app')
self.webview = WebKit2.WebView.new_with_user_content_manager(manager)
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.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
2023-07-24 15:05:01 +08:00
2023-07-21 19:13:51 +08:00
self.add(self.webview)
2023-07-24 15:05:01 +08:00
def create_tray(self):
2023-07-24 19:37:20 +08:00
indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
indicator.connect('activate', self.toggle_visible)
2023-07-24 15:05:01 +08:00
return indicator
2023-07-24 19:37:20 +08:00
def toggle_visible(self, icon):
# trayIcon
print(icon)
if self.is_active():
self.hide()
else:
self.present()
2023-07-24 15:05:01 +08:00
2023-07-21 19:13:51 +08:00
def file_path(self, filepath):
root = os.path.dirname(os.path.realpath(__file__))
return os.path.join(root, filepath)
def on_script_message(self, webview, message):
data = json.loads(message.get_js_value().to_json(0))
2023-07-25 12:29:31 +08:00
event = data.get('event')
callback = data.get('callback')
params = data.get('data')
2023-07-25 12:29:31 +08:00
match event:
case 'fs':
pass
case 'clipboard':
output = None
# 读文本
if params['action'] == 'wait_for_text':
output = self.clipboard.wait_for_text()
# 写文本
elif params['action'] == 'set_text':
self.clipboard.set_text(params['value'], -1)
# 写图片
elif params['action'] == 'set_image':
image = params['value']
# 前端传进来的值, 如果是路径的话, 直接读取
if type(image) == str:
image = Pixbuf.new_from_file(image)
else:
image = Pixbuf.new_from_data(
data = bytes(image['bytes']),
colorspace = image['colorspace'],
has_alpha = image['has_alpha'],
bits_per_sample = image['bits_per_sample'],
width = image['width'],
height = image['height'],
rowstride = image['rowstride']
)
self.clipboard.set_image(image)
self.clipboard.store()
# 清除剪切板
elif params['action'] == 'clear':
self.clipboard.clear()
# 回调给前端
if callback and output:
scripts = 'native.$emit("' + callback + '",' + json.dumps(output) + ')'
print(scripts)
self.webview.evaluate_javascript(scripts, -1)
# 退出app
case 'quit':
Gtk.main_quit()
# 读取图片, 返回图片像素数据
case 'image':
filename = params['value']
pixbuf = Pixbuf.new_from_file(filename)
image = {
"width": pixbuf.get_width(),
"height": pixbuf.get_height(),
"colorspace": pixbuf.get_colorspace(),
"has_alpha": pixbuf. get_has_alpha(),
"bits_per_sample": pixbuf.get_bits_per_sample(),
"rowstride": pixbuf.get_rowstride(),
"filepath": filename,
"bytes": list(pixbuf.get_pixels())
}
scripts = 'native.$emit("' + callback + '",' + json.dumps(image) + ')'
self.webview.evaluate_javascript(scripts, -1)
2023-07-25 12:29:31 +08:00
case _:
if callback :
res = {"foo": 123, "bar": (11,22,33)}
2023-07-25 12:29:31 +08:00
scripts = 'native.$emit("' + callback + '",' + json.dumps(res) + ')'
print(scripts)
self.webview.evaluate_javascript(scripts, -1)
2023-07-21 19:13:51 +08:00
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()
win = WebKitWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
2023-07-24 15:05:01 +08:00
tray = win.create_tray()
2023-07-21 19:13:51 +08:00
Gtk.main()