From 98023484cfd23e5aff40267afd2b29f0d729daab Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 31 Jul 2023 17:09:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- inject.js | 30 +++++++++++++++++++++++++++- main.py | 53 ++++++++++++++++++++++++++++++++++++++++---------- webview/app.js | 34 ++++++++++++++++++++++++++++++-- 4 files changed, 106 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ed8ebf5..b187fbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__ \ No newline at end of file +__pycache__ +*.txt \ No newline at end of file diff --git a/inject.js b/inject.js index b47d05b..e09156e 100644 --- a/inject.js +++ b/inject.js @@ -186,7 +186,35 @@ Object.assign(native, { return handler('quit', {}, null) }, fs: { - read(filepath) {} + read(filepath) { + return handler('fs', { action: 'read', filepath }) + }, + write(filepath, content = '', append = false) { + return handler('fs', { + action: 'write', + filepath, + content, + append + }) + }, + exists(filepath) { + return handler('fs', { action: 'exists', filepath }) + }, + isfile(filepath) { + return handler('fs', { action: 'isfile', filepath }) + }, + isdir(filepath) { + return handler('fs', { action: 'isdir', filepath }) + }, + remove(filepath) { + return handler('fs', { action: 'remove', filepath }) + }, + rename(filepath, target) { + return handler('fs', { action: 'rename', filepath, target }) + }, + copy(filepath, target) { + return handler('fs', { action: 'copy', filepath, target }) + } }, image(filepath) { return handler('image', { value: filepath }).then(r => new NativeImage(r)) diff --git a/main.py b/main.py index 5a14429..35920e5 100755 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -import gi, json, os +import gi, json, os, shutil gi.require_version("Gtk", "3.0") @@ -173,7 +173,48 @@ class WebKitWindow(Gtk.Window): match event: case 'fs': - pass + filepath = params['filepath'] + if params['action'] == 'read': + file = open(filepath) + output = file.read() + + if params['action'] == 'write': + try: + file = open(filepath, 'a' if params['append'] else 'w') + output = file.write(params['content']) + finally: + if file: + file.close() + + if params['action'] == 'exists': + output = os.path.exists(filepath) + + if params['action'] == 'list': + file = open(filepath) + output = file.read() + + if params['action'] == 'remove': + if os.path.isfile(filepath): + output = os.remove(filepath) + elif os.path.isdir(filepath): + output = os.removedirs(filename) + + if params['action'] == 'rename': + if os.path.exists(filepath): + output = shutil.move(filepath, params['target']) + + if params['action'] == 'copy': + if os.path.exists(filepath): + output = shutil.copy2(filepath, params['target']) + + if params['action'] == 'isfile': + output = os.path.isfile(filepath) + + if params['action'] == 'isdir': + output = os.path.isdir(filepath) + + + case 'clipboard': # 读文本 @@ -304,14 +345,6 @@ class WebKitWindow(Gtk.Window): output = self.is_visible() - - - - - - - case _: - output = {"foo": 123, "bar": (11,22,33)} # 有回调则返回结果 if callback : self.call_js(callback, output) diff --git a/webview/app.js b/webview/app.js index 9f38ed2..22560ff 100644 --- a/webview/app.js +++ b/webview/app.js @@ -10,7 +10,8 @@ import { html, css, Component } from 'wkit' class App extends Component { static props = { input: '', - img: '' + img: '', + content: '' } static styles = css` @@ -28,7 +29,7 @@ class App extends Component { 打开控制台 -
loading...
+
${this.content || 'loading...'}
@@ -50,6 +51,35 @@ class App extends Component { > unregisterall + + + `