diff --git a/app/sw.js b/app/sw.js deleted file mode 100644 index 8e493ad..0000000 --- a/app/sw.js +++ /dev/null @@ -1,24 +0,0 @@ -function injectedFunction(color) { - document.body.style.backgroundColor = color; -} - - -chrome.runtime.onMessage.addListener(async ({ name, options }) => { - if (name === 'addRecord') { - console.log('Hello World!'); - - // get active tab - // const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); - // chrome.scripting.executeScript({ - // target: { tabId: tab.id }, - // func: injectedFunction, - // args: ["orange"], - // }); - - const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); - const response = await chrome.tabs.sendMessage(tab.id, { greeting: "hello" }); - // do something with response here, not outside the function - console.log(response); - - } -}); diff --git a/botext/__init__.py b/botext/__init__.py index 8b13789..8cb3f9e 100644 --- a/botext/__init__.py +++ b/botext/__init__.py @@ -1 +1,49 @@ +import win32pipe +import win32file +import struct + +class HostClient: + def __init__(self): + self.pipe_name = r"\\.\pipe\BotExtPipe" + self.pipe = None + + def connect(self): + self.pipe = win32file.CreateFile( + self.pipe_name, + win32file.GENERIC_READ | win32file.GENERIC_WRITE, + 0, + None, + win32file.OPEN_EXISTING, + 0, + None, + ) + + def write(self, data): + if not self.pipe: + self.connect() + length = len(data) + length_bytes = struct.pack("I", length) + win32file.WriteFile(self.pipe, length_bytes) + win32file.WriteFile(self.pipe, data) + + def read(self): + if not self.pipe: + self.connect() + length_bytes = win32file.ReadFile(self.pipe, 4)[1] + length = struct.unpack("I", length_bytes)[0] + data = win32file.ReadFile(self.pipe, length)[1] + return data + + def close(self): + if self.pipe: + win32file.CloseHandle(self.pipe) + self.pipe = None + + +if __name__ == "__main__": + client = HostClient() + client.connect() + client.write(b"Hello, World!") + print(client.read()) + client.close() diff --git a/host/install_host.bat b/botext/console/install_host.bat similarity index 68% rename from host/install_host.bat rename to botext/console/install_host.bat index 6b4de40..e995453 100644 --- a/host/install_host.bat +++ b/botext/console/install_host.bat @@ -4,4 +4,4 @@ :: Change HKCU to HKLM if you want to install globally. :: %~dp0 is the directory containing this bat script and ends with a backslash. -REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.botext" /ve /t REG_SZ /d "%~dp0com.google.chrome.botext.json" /f +REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.woni.botext" /ve /t REG_SZ /d "%~dp0com.google.chrome.botext.json" /f diff --git a/botext/install.py b/botext/install.py new file mode 100644 index 0000000..c8b2e1b --- /dev/null +++ b/botext/install.py @@ -0,0 +1,21 @@ +import os + + +# 在下载文件夹创建test目录 +# 获取windows 下载文件夹路径 +def get_download_folder(): + home_dir = os.path.expanduser("~") + download_folder = os.path.join(home_dir, "Downloads") + return download_folder + +def main(): + # download_path = get_download_folder() + # test = os.path.join(download_path, "test") + # if not os.path.exists(test): + # os.mkdir(test) + print("install_host.bat") + # os.system("install_host.bat") + + +if __name__ == '__main__': + main() diff --git a/host/botext.bat b/bridge/botext.bat similarity index 100% rename from host/botext.bat rename to bridge/botext.bat diff --git a/host/client.py b/bridge/client.py similarity index 100% rename from host/client.py rename to bridge/client.py diff --git a/bridge/com.woni.chrome.botext.json b/bridge/com.woni.chrome.botext.json new file mode 100644 index 0000000..ffd72df --- /dev/null +++ b/bridge/com.woni.chrome.botext.json @@ -0,0 +1,9 @@ +{ + "name": "com.woni.chrome.botext", + "description": "botext", + "path": "D:\\CS\\BotBridge\\bin\\Debug\\net8.0\\BotBridge.exe", + "type": "stdio", + "allowed_origins": [ + "chrome-extension://hhfenmonendjidnaibblhgkapmbncmnp/" + ] +} diff --git a/host/native-messaging-example-host b/bridge/native-messaging-example-host similarity index 100% rename from host/native-messaging-example-host rename to bridge/native-messaging-example-host diff --git a/host/native-messaging-example-host copy b/bridge/native-messaging-example-host copy similarity index 100% rename from host/native-messaging-example-host copy rename to bridge/native-messaging-example-host copy diff --git a/host/server.py b/bridge/server.py similarity index 100% rename from host/server.py rename to bridge/server.py diff --git a/host/uninstall_host.bat b/bridge/uninstall_host.bat similarity index 100% rename from host/uninstall_host.bat rename to bridge/uninstall_host.bat diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..0f27524 --- /dev/null +++ b/build.bat @@ -0,0 +1 @@ +poetry build && pip install dist\botext-0.1.0-py3-none-any.whl --force-reinstall diff --git a/app/content-script.js b/crx/content-script.js similarity index 100% rename from app/content-script.js rename to crx/content-script.js diff --git a/app/index.html b/crx/index.html similarity index 100% rename from app/index.html rename to crx/index.html diff --git a/app/index.js b/crx/index.js similarity index 100% rename from app/index.js rename to crx/index.js diff --git a/app/manifest.json b/crx/manifest.json similarity index 100% rename from app/manifest.json rename to crx/manifest.json diff --git a/crx/sw.js b/crx/sw.js new file mode 100644 index 0000000..fe468cf --- /dev/null +++ b/crx/sw.js @@ -0,0 +1,52 @@ +// function injectedFunction(color) { +// document.body.style.backgroundColor = color; +// } + + +// chrome.runtime.onMessage.addListener(async ({ name, options }) => { +// if (name === 'addRecord') { +// console.log('Hello World!'); + +// // get active tab +// // const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); +// // chrome.scripting.executeScript({ +// // target: { tabId: tab.id }, +// // func: injectedFunction, +// // args: ["orange"], +// // }); + +// const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); +// const response = await chrome.tabs.sendMessage(tab.id, { greeting: "hello" }); +// // do something with response here, not outside the function +// console.log(response); + +// } +// }); + +let port; + +function sendMessage(message) { + if (!port) { + // this will run the first time to launch the native host, and save the connection to that instance in the port + port = chrome.runtime.connectNative('com.woni.chrome.botext'); + } + // Convert the message to a Uint8Array. + var encoder = new TextEncoder(); + var messageBytes = encoder.encode(message); + // Send the message length as a 32-bit integer. + var buffer = new ArrayBuffer(4); + var view = new DataView(buffer); + view.setUint32(0, messageBytes.length, true); + port.postMessage(buffer); + + // Send the message content. + port.postMessage(messageBytes); +} + + + + + +sendMessage('message 1'); +sendMessage('message 2'); +sendMessage('message 3'); diff --git a/app/wps.js b/crx/wps.js similarity index 100% rename from app/wps.js rename to crx/wps.js diff --git a/dist/botext-0.1.0-py3-none-any.whl b/dist/botext-0.1.0-py3-none-any.whl index e86ac90..4780986 100644 Binary files a/dist/botext-0.1.0-py3-none-any.whl and b/dist/botext-0.1.0-py3-none-any.whl differ diff --git a/dist/botext-0.1.0.tar.gz b/dist/botext-0.1.0.tar.gz index e186df5..8657aa3 100644 Binary files a/dist/botext-0.1.0.tar.gz and b/dist/botext-0.1.0.tar.gz differ diff --git a/docs/botext-init.md b/docs/botext-init.md new file mode 100644 index 0000000..78922d5 --- /dev/null +++ b/docs/botext-init.md @@ -0,0 +1,6 @@ +1. 注册表 + +REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.botext" /ve /t REG_SZ /d "%~dp0com.google.chrome.botext.json" /f + +com.woni.chrome.botext + diff --git a/docs/install.md b/docs/install.md new file mode 100644 index 0000000..acf6240 --- /dev/null +++ b/docs/install.md @@ -0,0 +1,11 @@ +安装python模块自动运行脚本 + +1. 修改注册表 +2. 拷贝文件至目标目录 +3. 安装插件 + + + + + + diff --git a/host/com.google.chrome.botext.json b/host/com.google.chrome.botext.json deleted file mode 100644 index 6bb7b9e..0000000 --- a/host/com.google.chrome.botext.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "com.google.chrome.botext", - "description": "botext", - "path": "D:\\rust\\botext\\target\\debug\\botext.exe", - "type": "stdio", - "allowed_origins": [ - "chrome-extension://hbneoijjlheddhhonbkamdmgljmjcdom/" - ] -} diff --git a/pyproject.toml b/pyproject.toml index 2997438..6273d47 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,5 +17,6 @@ requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" + [tool.poetry.scripts] -# pyd = "python build-pyd.py build_ext --inplace" +install = "botext.install:main"