From 45709602a2c8fdfeaea2684b59113777b39d9bfa Mon Sep 17 00:00:00 2001 From: Flames Date: Sun, 13 Oct 2024 04:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B7=B2=E7=9F=A5BUG?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 28 ++++-- src/main.js | 18 +++- src/preload.js | 21 ++++- src/renderer.js | 228 ++++++++++++++++++++++++++++++++--------------- src/setting.html | 24 +++++ 5 files changed, 237 insertions(+), 82 deletions(-) create mode 100644 src/setting.html diff --git a/manifest.json b/manifest.json index 35e6abe..f58549b 100644 --- a/manifest.json +++ b/manifest.json @@ -3,13 +3,27 @@ "type": "extension", "name": "OpenCar", "slug": "xq_opencar", - "description": "OpenCar", - "version": "0.0.1", - "authors": [{ - "name": "Flames", - "link": "https://blog.mnorg.cn" - }], - "platform": ["win32", "linux", "darwin"], + "description": "来不及解释了,快上车!", + "version": "0.1.0", + "authors": [ + { + "name": "Flames", + "link": "https://blog.mnorg.cn" + } + ], + "repository": { + "repo": "flamegreat/LiteLoaderQQNT-OpenCar", + "branch": "master", + "release": { + "tag": "0.1.0", + "file": "opencar.zip" + } + }, + "platform": [ + "win32", + "linux", + "darwin" + ], "injects": { "renderer": "./src/renderer.js", "main": "./src/main.js", diff --git a/src/main.js b/src/main.js index dc1bf67..f89c25f 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,5 @@ // 运行在 Electron 主进程 下的插件入口 -const { app, ipcMain, dialog } = require("electron"); +const { app, ipcMain, dialog, shell } = require("electron"); const fs = require('fs'); // 创建窗口时触发 @@ -10,7 +10,7 @@ module.exports.onBrowserWindowCreated = window => { } - +// 打开文件 ipcMain.on('read-file-request', async (event, filePath) => { fs.readFile(filePath, (error, data) => { if (error) { @@ -19,4 +19,18 @@ ipcMain.on('read-file-request', async (event, filePath) => { event.reply('read-file-reply', data); } }); +}); + +// 获取下载路径 +ipcMain.on('get-download-path', (event) => { + // 获取下载路径 + const downloadPath = app.getPath('downloads'); + + // 返回下载路径 + event.reply('get-download-path-reply', downloadPath); +}); + +// 打开浏览器 +ipcMain.on('open-browser', (event, url) => { + shell.openExternal(url); }); \ No newline at end of file diff --git a/src/preload.js b/src/preload.js index 4c49169..b494953 100644 --- a/src/preload.js +++ b/src/preload.js @@ -37,5 +37,24 @@ contextBridge.exposeInMainWorld("xq", { } }); }); - } + }, + // 获取下载目录路径 + getDownloadPath: () => { + return new Promise((resolve, reject) => { + ipcRenderer.send('get-download-path'); + + ipcRenderer.once('get-download-path-reply', (event, path) => { + if (path instanceof Error) { + reject(path); + } else { + resolve(path); + } + }); + }); + }, + openBrowser: (url) => { + return new Promise((resolve, reject) => { + ipcRenderer.send('open-browser', url); + }); + }, }); diff --git a/src/renderer.js b/src/renderer.js index 8ce8f62..6350154 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -4,87 +4,171 @@ export const onSettingWindowCreated = view => { // view 为 Element 对象,修改将同步到插件设置界面 console.log("view 为 Element 对象,修改将同步到插件设置界面"); + + const new_navbar_item = ` +
+ + + + +
+ 开车 + 来不及解释了,快上车! +
+
+ +
+ 网页版OpenCar + https://opencar.mnorg.com +
+ +
+
+
+
+
+`; + + const parser = new DOMParser(); + const doc2 = parser.parseFromString(new_navbar_item, "text/html"); + const node2 = doc2.querySelector("div"); // 获取 div 元素 + + if (node2) { // 确保 node2 不为 null + view.appendChild(node2); // 将 div 添加到 view 中 + } else { + console.error("未能找到 div 元素"); + } + + node2.querySelector("#opencar-open-web").onclick = async () => { + window.xq.openBrowser('https://opencar.mnorg.com'); + }; } onLoad(); async function onLoad() { setInterval(() => { + // 定时执行 OpenCar(); - }, 500); + }, 1000); } +const btn_text_opencar = "OpenCar"; +const btn_text_close = "Close"; + async function OpenCar() { - var elements = document.querySelector(".chat-msg-area__vlist")?.querySelectorAll(".ml-item"); - - elements.forEach(element => { - const FileEl = element.querySelector(".file-element"); - - if (FileEl) { - const filename = FileEl?.title; - const isdown = FileEl.querySelector(".file-info")?.textContent?.includes("已下载"); - const button = document.getElementById("opencar"); - if (isdown && !button) { - const button = document.createElement('button'); - button.id = "opencar"; - button.textContent = 'OpenCar'; - button.addEventListener('click', async function (event) { - // 阻止事件冒泡 - event.stopPropagation(); - - const video = document.getElementById("opencarvideo"); - - if (video) { - video.remove(); - button.textContent = 'OpenCar'; - } - else { - const filepath = 'C:/Users/Administrator/Downloads/' + filename; - console.log('文件路径:', filepath); - - button.textContent = 'loading...'; - window.xq.readFile(filepath) - .then(data => { - console.log(data); - - const uint8Array = new Uint8Array(data); - const reversedArray = uint8Array.reverse(); - const blob = new Blob([reversedArray], { type: 'application/octet-stream' }); - const url = URL.createObjectURL(blob); - - // 创建 video 元素 - const video = document.createElement('video'); - FileEl.appendChild(video); - - video.controls = true; // 显示播放控件 - video.id = "opencarvideo"; - video.width = FileEl.offsetWidth; // 设置视频宽度 - // video.height = 240; // 设置视频高度 - - // // 设置视频源 - // const source = document.createElement('source'); - // source.src = url; // 替换为你的视频文件路径 - // source.type = 'video/mp4'; // 视频格式 - // video.appendChild(source); - video.src = url; - // 将 video 元素添加到指定的容器 - - video.onerror = function (event) { - console.log(event, "播放报错啦"); - }; - // 播放视频 - video.play(); - - button.textContent = 'Close'; - }) - .catch(error => console.error(error)); - - } - }); - - FileEl.appendChild(button); - } - } + // 获取全部文件消息列表元素 + var elements = document.querySelector(".chat-msg-area__vlist")?.querySelectorAll(".file-element"); + + elements.forEach(FileEl => { + // 获取文件信息元素 + const FileInfoEl = FileEl.querySelector(".file-info"); + + // 判断文件是否已下载 + const isdown = FileInfoEl?.textContent?.includes("已下载"); + const issend = FileInfoEl?.textContent?.includes("已发送"); + + // 不是已下载文件则跳过 + if (!isdown && !issend) return; + + // 按钮 id + const btn_id = "opencar_" + FileEl.getAttribute('element-id'); + + // 判断是否已经创建了按钮 + const hasbutton = document.getElementById(btn_id); + + // 已经创建了按钮则跳过 + if (hasbutton) return; + + // 没有按钮则创建按钮 + const button = document.createElement('button'); + button.id = btn_id; + button.textContent = btn_text_opencar; + button.addEventListener('click', async function (event) { + // 阻止事件冒泡 + event.stopPropagation(); + + // 点击按钮时播放或关闭视频 + OpenCarPlayOrClose(FileEl, FileEl.title).then((isplay) => { + if (isplay) { + button.textContent = btn_text_close; + } + else { + button.textContent = btn_text_opencar; + } + }); + }); + + FileInfoEl.appendChild(button); }); -} \ No newline at end of file +} + +// 打开或关闭视频播放 +async function OpenCarPlayOrClose(FileEl, filename) { + // 获取 video 元素 + const video_id = "opencar_video_" + FileEl.getAttribute('element-id'); + const video = document.getElementById(video_id); + + // 获取 img 元素 + const img_id = "opencar_img_id" + FileEl.getAttribute('element-id'); + const img = document.getElementById(img_id); + + if (video) { + // 移除 video 元素 + video.remove(); + return false; + } + else if (img) { + // 移除 img 元素 + img.remove(); + return false; + } + else { + // 获取下载目录 + const downloadPath = await window.xq.getDownloadPath(); + const filepath = downloadPath + '/' + filename; + + // button.textContent = 'loading...'; + + // 读取文件内容 + const fileData = await window.xq.readFile(filepath); + + const uint8Array = new Uint8Array(fileData); + const reversedArray = uint8Array.reverse(); + const blob = new Blob([reversedArray], { type: 'application/octet-stream' }); + const url = URL.createObjectURL(blob); + + // 创建 video 元素 + const video = document.createElement('video'); + FileEl.appendChild(video); + + video.controls = true; // 显示播放控件 + video.id = video_id; + video.width = FileEl.offsetWidth; // 设置视频宽度 + video.src = url; + + video.onerror = function (event) { + OpenCarLog("播放视频出错啦,尝试显示为图片。"); + + video.remove(); + + const img = document.createElement('img'); + img.id = img_id; + img.src = URL.createObjectURL(blob); + img.width = FileEl.offsetWidth; + FileEl.appendChild(img); + }; + // 播放视频 + video.play(); + + return true; + } +} + +async function OpenCarLog(msg) { + console.log('[OpenCar] ' + msg); +}; \ No newline at end of file diff --git a/src/setting.html b/src/setting.html new file mode 100644 index 0000000..e557595 --- /dev/null +++ b/src/setting.html @@ -0,0 +1,24 @@ +
+ + + + +
+ 开车 + 来不及解释了,快上车! +
+
+ +
+ 网页版OpenCar + https://opencar.mnorg.com +
+ +
+
+
+
+
\ No newline at end of file