diff --git a/app.js b/app.js index 4764271..20cd91e 100644 --- a/app.js +++ b/app.js @@ -9,7 +9,7 @@ import { $, bind } from 'wkit' async function test1() { try { - var result = await window.webkit.messageHandlers.app.postMessage({ + var result = window.webkit.messageHandlers.app.postMessage({ value: 'Test 1' }) $('#output').innerHTML = 'output: ' + result @@ -20,14 +20,13 @@ async function test1() { async function test2() { let key = 'cb_' + Math.random().toString().slice(2) - window.$on(key, function (v) { + native.$on(key, function (v) { $('#output').innerHTML = v }) - window.webkit.messageHandlers.app.postMessage({ + await window.webkit.messageHandlers.app.postMessage({ value: new Date(), key }) - $('#output').innerHTML = result } bind($('.btn1'), 'click', test1) diff --git a/inject.js b/inject.js index abda9e4..1b09817 100644 --- a/inject.js +++ b/inject.js @@ -4,9 +4,9 @@ * @date 2023/07/21 17:38:11 */ -Object.assign(window, { +class EventEmitter { // - __events__: Object.create(null), + __events__ = Object.create(null) $on(name, fn) { if (this.__events__[name]) { @@ -14,12 +14,12 @@ Object.assign(window, { } else { this.__events__[name] = [fn] } - }, + } $once(name, fn) { fn.__once__ = true this.$on(name, fn) - }, + } $off(name, fn) { if (this.__events__[name]) { @@ -29,7 +29,7 @@ Object.assign(window, { this.__events__[name] = [] } } - }, + } $emit(name, ...args) { if (this.__events__[name]) { @@ -44,9 +44,11 @@ Object.assign(window, { } } } - }, + } $destroy() { this.__events__ = Object.create(null) } -}) +} + +window.native = new EventEmitter() diff --git a/main.py b/main.py index 80390a5..6189272 100755 --- a/main.py +++ b/main.py @@ -4,13 +4,16 @@ import gi, json, os gi.require_version("Gtk", "3.0") gi.require_version("WebKit2", "4.1") -# gi.require_version("JavaScriptCore", "4.1") -from gi.repository import Gtk, WebKit2, JavaScriptCore +from gi.repository import Gtk, WebKit2 + +# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon +try: + gi.require_version('AyatanaAppIndicator3', '0.1') + from gi.repository import AyatanaAppIndicator3 as AppIndicator3 +except (ValueError, ImportError): + AppIndicator3 = None -class MyScriptMessageHandler(WebKit2.ScriptMessageReply): - def __init__(self): - super().__init__() class WebKitWindow(Gtk.Window): def __init__(self): @@ -49,11 +52,26 @@ class WebKitWindow(Gtk.Window): self.webview.load_uri("http://127.0.0.1:10086/index.html") # self.webview.load_uri("https://benchmark.wkit.fun") + - self.add(self.webview) + + + def create_tray(self): + if AppIndicator3 : + indicator = AppIndicator3.Indicator.new( + "youtube", + "youtube", + AppIndicator3.IndicatorCategory.APPLICATION_STATUS + ) + else: + indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + + return indicator + + def file_path(self, filepath): root = os.path.dirname(os.path.realpath(__file__)) return os.path.join(root, filepath) @@ -66,7 +84,7 @@ class WebKitWindow(Gtk.Window): key = data.get('key') if key : - scripts = '$emit("' + key + '", "这是py返回的值 ' + data.get('value') + '")' + scripts = 'native.$emit("' + key + '", "这是py返回的值 ' + data.get('value') + '")' self.webview.evaluate_javascript(scripts, -1) @@ -89,4 +107,8 @@ win = WebKitWindow() win.connect("destroy", Gtk.main_quit) win.show_all() +tray = win.create_tray() + +print(tray) + Gtk.main() \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..58d4bfd --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[tool.python3-build.dependencies] +gtk3 = ">=3.0" +webkit2 = ">=4.1" \ No newline at end of file diff --git a/setup.py b/setup.py deleted file mode 100755 index fc15974..0000000 --- a/setup.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/python3 - -from setuptools import setup - -setup( - name='myapp', - version='0.1', - description='My GTK3 + WebKit2 application', - author='Your Name', - author_email='your@email.com', - url='https://github.com/yourusername/myapp', - py_modules=['main'], - install_requires=[ - 'gi', - 'webkit2gtk', - ], - entry_points={ - 'gui_scripts': [ - 'myapp=main:WebKitWindow' - ] - }, -) \ No newline at end of file