Skip to content

Commit

Permalink
初始化仓库
Browse files Browse the repository at this point in the history
  • Loading branch information
flamegreat committed Jun 18, 2024
1 parent 5554fc6 commit 54ceb6a
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
18 changes: 18 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"manifest_version": 4,
"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"],
"injects": {
"renderer": "./src/renderer.js",
"main": "./src/main.js",
"preload": "./src/preload.js"
}
}
22 changes: 22 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 运行在 Electron 主进程 下的插件入口
const { app, ipcMain, dialog } = require("electron");
const fs = require('fs');

// 创建窗口时触发
module.exports.onBrowserWindowCreated = window => {
// window 为 Electron 的 BrowserWindow 实例

console.log("Flames");

}


ipcMain.on('read-file-request', async (event, filePath) => {
fs.readFile(filePath, (error, data) => {
if (error) {
event.reply('read-file-reply', error);
} else {
event.reply('read-file-reply', data);
}
});
});
41 changes: 41 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Electron 主进程 与 渲染进程 交互的桥梁
const { contextBridge, ipcRenderer } = require('electron');

// 在window对象下导出只读对象
contextBridge.exposeInMainWorld("xq", {
readFileStream: (filePath) => {
return new Promise((resolve, reject) => {

console.log('渲染进程', filePath);

ipcRenderer.send('read-file-stream', filePath);

ipcRenderer.once('file-stream-data', (event, data) => {

console.log('渲染进程', data);

resolve(data);
});

ipcRenderer.once('file-stream-end', () => {
// Do something on end of stream
});

ipcRenderer.once('file-stream-error', (event, error) => {
reject(error);
});
});
},
readFile: (filePath) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('read-file-request', filePath);
ipcRenderer.on('read-file-reply', (event, data) => {
if (data instanceof Error) {
reject(data);
} else {
resolve(data);
}
});
});
}
});
90 changes: 90 additions & 0 deletions src/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 运行在 Electron 渲染进程 下的页面脚本

// 打开设置界面时触发
export const onSettingWindowCreated = view => {
// view 为 Element 对象,修改将同步到插件设置界面
console.log("view 为 Element 对象,修改将同步到插件设置界面");
}

onLoad();

async function onLoad() {
setInterval(() => {
OpenCar();
}, 500);
}

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);
}
}
});
}

0 comments on commit 54ceb6a

Please sign in to comment.