:feature: 日常更新

This commit is contained in:
muwoo 2023-03-22 11:19:39 +08:00
parent dc74251bd0
commit 433e7c9850
5 changed files with 142 additions and 120 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "rubick", "name": "rubick",
"version": "2.0.8", "version": "2.0.9",
"author": "muwoo <2424880409@qq.com>", "author": "muwoo <2424880409@qq.com>",
"private": true, "private": true,
"scripts": { "scripts": {

View File

@ -1,19 +1,19 @@
const { ipcRenderer, shell } = require("electron"); const { ipcRenderer, shell } = require('electron');
const os = require("os"); const os = require('os');
const ipcSendSync = (type, data) => { const ipcSendSync = (type, data) => {
const returnValue = ipcRenderer.sendSync("msg-trigger", { const returnValue = ipcRenderer.sendSync('msg-trigger', {
type, type,
data data,
}); });
if (returnValue instanceof Error) throw returnValue; if (returnValue instanceof Error) throw returnValue;
return returnValue; return returnValue;
}; };
const ipcSend = (type, data) => { const ipcSend = (type, data) => {
ipcRenderer.send("msg-trigger", { ipcRenderer.send('msg-trigger', {
type, type,
data data,
}); });
}; };
@ -22,99 +22,99 @@ window.rubick = {
// 事件 // 事件
onPluginEnter(cb) { onPluginEnter(cb) {
console.log(window.rubick.hooks); console.log(window.rubick.hooks);
typeof cb === "function" && (window.rubick.hooks.onPluginEnter = cb); typeof cb === 'function' && (window.rubick.hooks.onPluginEnter = cb);
}, },
onPluginReady(cb) { onPluginReady(cb) {
typeof cb === "function" && (window.rubick.hooks.onPluginReady = cb); typeof cb === 'function' && (window.rubick.hooks.onPluginReady = cb);
}, },
onPluginOut(cb) { onPluginOut(cb) {
typeof cb === "function" && (window.rubick.hooks.onPluginOut = cb); typeof cb === 'function' && (window.rubick.hooks.onPluginOut = cb);
}, },
openPlugin(plugin) { openPlugin(plugin) {
ipcSendSync("loadPlugin", plugin); ipcSendSync('loadPlugin', plugin);
}, },
// 窗口交互 // 窗口交互
hideMainWindow() { hideMainWindow() {
ipcSendSync("hideMainWindow"); ipcSendSync('hideMainWindow');
}, },
showMainWindow() { showMainWindow() {
ipcSendSync("showMainWindow"); ipcSendSync('showMainWindow');
}, },
showOpenDialog(options) { showOpenDialog(options) {
ipcSendSync("showOpenDialog", options); ipcSendSync('showOpenDialog', options);
}, },
setExpendHeight(height) { setExpendHeight(height) {
ipcSendSync("setExpendHeight", height); ipcSendSync('setExpendHeight', height);
}, },
setSubInput(onChange, placeholder = "", isFocus) { setSubInput(onChange, placeholder = '', isFocus) {
typeof onChange === "function" && typeof onChange === 'function' &&
(window.rubick.hooks.onSubInputChange = onChange); (window.rubick.hooks.onSubInputChange = onChange);
ipcSendSync("setSubInput", { ipcSendSync('setSubInput', {
placeholder, placeholder,
isFocus isFocus,
}); });
}, },
removeSubInput() { removeSubInput() {
delete window.rubick.hooks.onSubInputChange; delete window.rubick.hooks.onSubInputChange;
ipcSendSync("removeSubInput"); ipcSendSync('removeSubInput');
}, },
setSubInputValue(text) { setSubInputValue(text) {
ipcSendSync("setSubInputValue", { text }); ipcSendSync('setSubInputValue', { text });
}, },
subInputBlur() { subInputBlur() {
ipcSendSync("subInputBlur"); ipcSendSync('subInputBlur');
}, },
getPath(name) { getPath(name) {
return ipcSendSync("getPath", { name }); return ipcSendSync('getPath', { name });
}, },
showNotification(body, clickFeatureCode) { showNotification(body, clickFeatureCode) {
ipcSend("showNotification", { body, clickFeatureCode }); ipcSend('showNotification', { body, clickFeatureCode });
}, },
copyImage(img) { copyImage(img) {
return ipcSendSync("copyImage", { img }); return ipcSendSync('copyImage', { img });
}, },
copyText(text) { copyText(text) {
return ipcSendSync("copyText", { text }); return ipcSendSync('copyText', { text });
}, },
copyFile: file => { copyFile: (file) => {
return ipcSendSync("copyFile", { file }); return ipcSendSync('copyFile', { file });
}, },
db: { db: {
put: data => ipcSendSync("dbPut", { data }), put: (data) => ipcSendSync('dbPut', { data }),
get: id => ipcSendSync("dbGet", { id }), get: (id) => ipcSendSync('dbGet', { id }),
remove: doc => ipcSendSync("dbRemove", { doc }), remove: (doc) => ipcSendSync('dbRemove', { doc }),
bulkDocs: docs => ipcSendSync("dbBulkDocs", { docs }), bulkDocs: (docs) => ipcSendSync('dbBulkDocs', { docs }),
allDocs: key => ipcSendSync("dbAllDocs", { key }) allDocs: (key) => ipcSendSync('dbAllDocs', { key }),
}, },
dbStorage: { dbStorage: {
setItem: (key, value) => { setItem: (key, value) => {
const target = { _id: String(key) }; const target = { _id: String(key) };
const result = ipcSendSync("dbGet", { id: target._id }); const result = ipcSendSync('dbGet', { id: target._id });
result && (target._rev = result._rev); result && (target._rev = result._rev);
target.value = value; target.value = value;
const res = ipcSendSync("dbPut", { data: target }); const res = ipcSendSync('dbPut', { data: target });
if (res.error) throw new Error(res.message); if (res.error) throw new Error(res.message);
}, },
getItem: key => { getItem: (key) => {
const res = ipcSendSync("dbGet", { id: key }); const res = ipcSendSync('dbGet', { id: key });
return res && "value" in res ? res.value : null; return res && 'value' in res ? res.value : null;
},
removeItem: (key) => {
const res = ipcSendSync('dbGet', { id: key });
res && ipcSendSync('dbRemove', { doc: res });
}, },
removeItem: key => {
const res = ipcSendSync("dbGet", { id: key });
res && ipcSendSync("dbRemove", { doc: res });
}
}, },
isDarkColors() { isDarkColors() {
return false; return false;
}, },
getFeatures() { getFeatures() {
return ipcSendSync("getFeatures"); return ipcSendSync('getFeatures');
}, },
setFeature(feature) { setFeature(feature) {
return ipcSendSync("setFeature", { feature }); return ipcSendSync('setFeature', { feature });
}, },
removeFeature(code) { removeFeature(code) {
return ipcSendSync("removeFeature", { code }); return ipcSendSync('removeFeature', { code });
}, },
// 系统 // 系统
@ -123,29 +123,29 @@ window.rubick = {
}, },
isMacOs() { isMacOs() {
return os.type() === "Darwin"; return os.type() === 'Darwin';
}, },
isWindows() { isWindows() {
return os.type() === "Windows_NT"; return os.type() === 'Windows_NT';
}, },
isLinux() { isLinux() {
return os.type() === "Linux"; return os.type() === 'Linux';
}, },
shellOpenPath(path) { shellOpenPath(path) {
shell.openPath(path); shell.openPath(path);
}, },
getLocalId: () => ipcSendSync("getLocalId"), getLocalId: () => ipcSendSync('getLocalId'),
removePlugin() { removePlugin() {
ipcSend("removePlugin"); ipcSend('removePlugin');
}, },
shellShowItemInFolder: path => { shellShowItemInFolder: (path) => {
ipcSend("shellShowItemInFolder", { path }); ipcSend('shellShowItemInFolder', { path });
}, },
redirect: (label, payload) => { redirect: (label, payload) => {
@ -153,6 +153,6 @@ window.rubick = {
}, },
shellBeep: () => { shellBeep: () => {
ipcSend("shellBeep") ipcSend('shellBeep');
}, },
}; };

View File

@ -1,5 +1,5 @@
import { app, BrowserWindow, protocol } from "electron"; import { app, BrowserWindow, protocol } from 'electron';
import path from "path"; import path from 'path';
export default () => { export default () => {
let win: any; let win: any;
@ -12,7 +12,7 @@ export default () => {
height: viewInfo.height, height: viewInfo.height,
width: viewInfo.width, width: viewInfo.width,
autoHideMenuBar: true, autoHideMenuBar: true,
titleBarStyle: "hidden", titleBarStyle: 'hidden',
trafficLightPosition: { x: 12, y: 21 }, trafficLightPosition: { x: 12, y: 21 },
title: pluginInfo.pluginName, title: pluginInfo.pluginName,
resizable: true, resizable: true,
@ -32,22 +32,34 @@ export default () => {
}); });
if (process.env.WEBPACK_DEV_SERVER_URL) { if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode // Load the url of the dev server if in development mode
win.loadURL("http://localhost:8082"); win.loadURL('http://localhost:8082');
} else { } else {
win.loadURL(`file://${path.join(__static, "./detach/index.html")}`); win.loadURL(`file://${path.join(__static, './detach/index.html')}`);
} }
win.on('close', () => {
win.on("closed", () => { executeHooks('PluginOut', null);
});
win.on('closed', () => {
win = undefined; win = undefined;
}); });
win.once("ready-to-show", () => { win.once('ready-to-show', () => {
win.setBrowserView(view); win.setBrowserView(view);
win.webContents.executeJavaScript( win.webContents.executeJavaScript(
`window.initDetach(${JSON.stringify(pluginInfo)})` `window.initDetach(${JSON.stringify(pluginInfo)})`
); );
win.show(); win.show();
}); });
const executeHooks = (hook, data) => {
if (!view) return;
const evalJs = `console.log(window.rubick);if(window.rubick && window.rubick.hooks && typeof window.rubick.hooks.on${hook} === 'function' ) {
try {
window.rubick.hooks.on${hook}(${data ? JSON.stringify(data) : ''});
} catch(e) {console.log(e)}
}
`;
view.webContents.executeJavaScript(evalJs);
};
}; };
const getWindow = () => win; const getWindow = () => win;

View File

@ -1,19 +1,19 @@
import { BrowserView, BrowserWindow, session } from "electron"; import { BrowserView, BrowserWindow, session } from 'electron';
import path from "path"; import path from 'path';
import commonConst from "../../common/utils/commonConst"; import commonConst from '../../common/utils/commonConst';
import { PLUGIN_INSTALL_DIR as baseDir } from "@/common/constans/main"; import { PLUGIN_INSTALL_DIR as baseDir } from '@/common/constans/main';
const getRelativePath = (indexPath) => { const getRelativePath = (indexPath) => {
return commonConst.windows() return commonConst.windows()
? indexPath.replace("file://", "") ? indexPath.replace('file://', '')
: indexPath.replace("file:", ""); : indexPath.replace('file:', '');
}; };
const getPreloadPath = (plugin, pluginIndexPath) => { const getPreloadPath = (plugin, pluginIndexPath) => {
const { name, preload, tplPath, indexPath } = plugin; const { name, preload, tplPath, indexPath } = plugin;
if (!preload) return; if (!preload) return;
if (commonConst.dev()) { if (commonConst.dev()) {
if (name === "rubick-system-feature") { if (name === 'rubick-system-feature') {
return path.resolve(__static, `../feature/public/preload.js`); return path.resolve(__static, `../feature/public/preload.js`);
} }
if (tplPath) { if (tplPath) {
@ -37,20 +37,29 @@ export default () => {
}; };
const createView = (plugin, window: BrowserWindow) => { const createView = (plugin, window: BrowserWindow) => {
let pluginIndexPath = plugin.tplPath || plugin.indexPath; const { tplPath, indexPath, development, name, main, pluginSetting, ext } =
plugin;
let pluginIndexPath = tplPath || indexPath;
let preloadPath;
// 开发环境
if (commonConst.dev() && development) {
pluginIndexPath = development;
const pluginPath = path.resolve(baseDir, 'node_modules', name);
preloadPath = `file://${path.join(pluginPath, './', main)}`;
}
// 再尝试去找 // 再尝试去找
if (plugin.name === "rubick-system-feature" && !pluginIndexPath) { if (plugin.name === 'rubick-system-feature' && !pluginIndexPath) {
pluginIndexPath = commonConst.dev() pluginIndexPath = commonConst.dev()
? "http://localhost:8081/#/" ? 'http://localhost:8081/#/'
: `file://${__static}/feature/index.html`; : `file://${__static}/feature/index.html`;
} }
if (!pluginIndexPath) { if (!pluginIndexPath) {
const pluginPath = path.resolve(baseDir, "node_modules", plugin.name); const pluginPath = path.resolve(baseDir, 'node_modules', name);
pluginIndexPath = `file://${path.join(pluginPath, "./", plugin.main)}`; pluginIndexPath = `file://${path.join(pluginPath, './', main)}`;
} }
const preload = getPreloadPath(plugin, pluginIndexPath); const preload = getPreloadPath(plugin, preloadPath || pluginIndexPath);
const ses = session.fromPartition("<" + plugin.name + ">"); const ses = session.fromPartition('<' + name + '>');
ses.setPreloads([`${__static}/preload.js`]); ses.setPreloads([`${__static}/preload.js`]);
view = new BrowserView({ view = new BrowserView({
@ -67,19 +76,20 @@ export default () => {
}); });
window.setBrowserView(view); window.setBrowserView(view);
view.webContents.loadURL(pluginIndexPath); view.webContents.loadURL(pluginIndexPath);
view.webContents.once("dom-ready", () => { view.webContents.once('dom-ready', () => {
window.setSize(800, 660); const height = pluginSetting && pluginSetting.height;
view.setBounds({ x: 0, y: 60, width: 800, height: 600 }); window.setSize(800, height || 660);
view.setBounds({ x: 0, y: 60, width: 800, height: height || 660 });
view.setAutoResize({ width: true }); view.setAutoResize({ width: true });
executeHooks("PluginEnter", plugin.ext); executeHooks('PluginEnter', ext);
executeHooks("PluginReady", plugin.ext); executeHooks('PluginReady', ext);
window.webContents.executeJavaScript(`window.pluginLoaded()`); window.webContents.executeJavaScript(`window.pluginLoaded()`);
}); });
// 修复请求跨域问题 // 修复请求跨域问题
view.webContents.session.webRequest.onBeforeSendHeaders( view.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => { (details, callback) => {
callback({ callback({
requestHeaders: { referer: "*", ...details.requestHeaders }, requestHeaders: { referer: '*', ...details.requestHeaders },
}); });
} }
); );
@ -88,7 +98,7 @@ export default () => {
(details, callback) => { (details, callback) => {
callback({ callback({
responseHeaders: { responseHeaders: {
"Access-Control-Allow-Origin": ["*"], 'Access-Control-Allow-Origin': ['*'],
...details.responseHeaders, ...details.responseHeaders,
}, },
}); });
@ -100,7 +110,7 @@ export default () => {
if (!view) return; if (!view) return;
window.removeBrowserView(view); window.removeBrowserView(view);
window.setSize(800, 60); window.setSize(800, 60);
executeHooks("PluginOut", null); executeHooks('PluginOut', null);
window.webContents.executeJavaScript(`window.initRubick()`); window.webContents.executeJavaScript(`window.initRubick()`);
view = undefined; view = undefined;
}; };
@ -111,7 +121,7 @@ export default () => {
if (!view) return; if (!view) return;
const evalJs = `if(window.rubick && window.rubick.hooks && typeof window.rubick.hooks.on${hook} === 'function' ) { const evalJs = `if(window.rubick && window.rubick.hooks && typeof window.rubick.hooks.on${hook} === 'function' ) {
try { try {
window.rubick.hooks.on${hook}(${data ? JSON.stringify(data) : ""}); window.rubick.hooks.on${hook}(${data ? JSON.stringify(data) : ''});
} catch(e) {} } catch(e) {}
} }
`; `;

View File

@ -6,27 +6,27 @@ import {
Notification, Notification,
nativeImage, nativeImage,
clipboard, clipboard,
shell shell,
} from "electron"; } from 'electron';
import { runner, detach } from "../browsers"; import { runner, detach } from '../browsers';
import fs from "fs"; import fs from 'fs';
import { LocalDb } from "@/core"; import { LocalDb } from '@/core';
import plist from "plist"; import plist from 'plist';
import { DECODE_KEY } from "@/common/constans/main"; import { DECODE_KEY } from '@/common/constans/main';
import mainInstance from "../index"; import mainInstance from '../index';
const runnerInstance = runner(); const runnerInstance = runner();
const detachInstance = detach(); const detachInstance = detach();
const dbInstance = new LocalDb(app.getPath("userData")); const dbInstance = new LocalDb(app.getPath('userData'));
dbInstance.init(); dbInstance.init();
class API { class API {
public currentPlugin: null | any = null; public currentPlugin: null | any = null;
private DBKEY = "RUBICK_DB_DEFAULT"; private DBKEY = 'RUBICK_DB_DEFAULT';
init(mainWindow: BrowserWindow) { init(mainWindow: BrowserWindow) {
// 响应 preload.js 事件 // 响应 preload.js 事件
ipcMain.on("msg-trigger", async (event, arg) => { ipcMain.on('msg-trigger', async (event, arg) => {
const window = arg.winId ? BrowserWindow.fromId(arg.winId) : mainWindow; const window = arg.winId ? BrowserWindow.fromId(arg.winId) : mainWindow;
const data = await this[arg.type](arg, window, event); const data = await this[arg.type](arg, window, event);
event.returnValue = data; event.returnValue = data;
@ -41,9 +41,9 @@ class API {
}; };
public __EscapeKeyDown = (event, input, window) => { public __EscapeKeyDown = (event, input, window) => {
if (input.type !== "keyDown") return; if (input.type !== 'keyDown') return;
if (!(input.meta || input.control || input.shift || input.alt)) { if (!(input.meta || input.control || input.shift || input.alt)) {
if (input.key === "Escape") { if (input.key === 'Escape') {
if (this.currentPlugin) { if (this.currentPlugin) {
this.removePlugin(null, window); this.removePlugin(null, window);
} else { } else {
@ -70,17 +70,17 @@ class API {
this.currentPlugin = plugin; this.currentPlugin = plugin;
window.webContents.executeJavaScript( window.webContents.executeJavaScript(
`window.setCurrentPlugin(${JSON.stringify({ `window.setCurrentPlugin(${JSON.stringify({
currentPlugin: this.currentPlugin currentPlugin: this.currentPlugin,
})})` })})`
); );
window.show(); window.show();
// 按 ESC 退出插件 // 按 ESC 退出插件
window.webContents.on("before-input-event", (event, input) => window.webContents.on('before-input-event', (event, input) =>
this.__EscapeKeyDown(event, input, window) this.__EscapeKeyDown(event, input, window)
); );
runnerInstance runnerInstance
.getView() .getView()
.webContents.on("before-input-event", (event, input) => .webContents.on('before-input-event', (event, input) =>
this.__EscapeKeyDown(event, input, window) this.__EscapeKeyDown(event, input, window)
); );
} }
@ -91,7 +91,7 @@ class API {
} }
public openPluginDevTools() { public openPluginDevTools() {
runnerInstance.getView().webContents.openDevTools({ mode: "detach" }); runnerInstance.getView().webContents.openDevTools({ mode: 'detach' });
} }
public hideMainWindow(arg, window) { public hideMainWindow(arg, window) {
@ -118,7 +118,7 @@ class API {
if (!originWindow) return; if (!originWindow) return;
originWindow.webContents.executeJavaScript( originWindow.webContents.executeJavaScript(
`window.setSubInput(${JSON.stringify({ `window.setSubInput(${JSON.stringify({
placeholder: data.placeholder placeholder: data.placeholder,
})})` })})`
); );
} }
@ -128,7 +128,7 @@ class API {
} }
public sendSubInputChangeEvent({ data }) { public sendSubInputChangeEvent({ data }) {
runnerInstance.executeHooks("SubInputChange", data); runnerInstance.executeHooks('SubInputChange', data);
} }
public removeSubInput(data, window, e) { public removeSubInput(data, window, e) {
@ -142,7 +142,7 @@ class API {
if (!originWindow) return; if (!originWindow) return;
originWindow.webContents.executeJavaScript( originWindow.webContents.executeJavaScript(
`window.setSubInputValue(${JSON.stringify({ `window.setSubInputValue(${JSON.stringify({
value: data.text value: data.text,
})})` })})`
); );
} }
@ -153,13 +153,13 @@ class API {
public showNotification({ data: { body } }) { public showNotification({ data: { body } }) {
if (!Notification.isSupported()) return; if (!Notification.isSupported()) return;
"string" != typeof body && (body = String(body)); 'string' != typeof body && (body = String(body));
const plugin = this.currentPlugin; const plugin = this.currentPlugin;
if (!plugin) return; if (!plugin) return;
const notify = new Notification({ const notify = new Notification({
title: plugin.pluginName, title: plugin.pluginName,
body, body,
icon: plugin.logo icon: plugin.logo,
}); });
notify.show(); notify.show();
} }
@ -177,7 +177,7 @@ class API {
public copyFile({ data }) { public copyFile({ data }) {
if (data.file && fs.existsSync(data.file)) { if (data.file && fs.existsSync(data.file)) {
clipboard.writeBuffer( clipboard.writeBuffer(
"NSFilenamesPboardType", 'NSFilenamesPboardType',
Buffer.from(plist.build([data.file])) Buffer.from(plist.build([data.file]))
); );
return true; return true;
@ -214,7 +214,7 @@ class API {
...this.currentPlugin, ...this.currentPlugin,
features: (() => { features: (() => {
let has = false; let has = false;
this.currentPlugin.features.some(feature => { this.currentPlugin.features.some((feature) => {
has = feature.code === data.feature.code; has = feature.code === data.feature.code;
return has; return has;
}); });
@ -222,11 +222,11 @@ class API {
return [...this.currentPlugin.features, data.feature]; return [...this.currentPlugin.features, data.feature];
} }
return this.currentPlugin.features; return this.currentPlugin.features;
})() })(),
}; };
window.webContents.executeJavaScript( window.webContents.executeJavaScript(
`window.updatePlugin(${JSON.stringify({ `window.updatePlugin(${JSON.stringify({
currentPlugin: this.currentPlugin currentPlugin: this.currentPlugin,
})})` })})`
); );
return true; return true;
@ -235,16 +235,16 @@ class API {
public removeFeature({ data }, window) { public removeFeature({ data }, window) {
this.currentPlugin = { this.currentPlugin = {
...this.currentPlugin, ...this.currentPlugin,
features: this.currentPlugin.features.filter(feature => { features: this.currentPlugin.features.filter((feature) => {
if (data.code.type) { if (data.code.type) {
return feature.code.type !== data.code.type; return feature.code.type !== data.code.type;
} }
return feature.code !== data.code; return feature.code !== data.code;
}) }),
}; };
window.webContents.executeJavaScript( window.webContents.executeJavaScript(
`window.updatePlugin(${JSON.stringify({ `window.updatePlugin(${JSON.stringify({
currentPlugin: this.currentPlugin currentPlugin: this.currentPlugin,
})})` })})`
); );
return true; return true;
@ -255,14 +255,14 @@ class API {
if (!code || !runnerInstance.getView()) return; if (!code || !runnerInstance.getView()) return;
if (modifiers.length > 0) { if (modifiers.length > 0) {
runnerInstance.getView().webContents.sendInputEvent({ runnerInstance.getView().webContents.sendInputEvent({
type: "keyDown", type: 'keyDown',
modifiers, modifiers,
keyCode: code keyCode: code,
}); });
} else { } else {
runnerInstance.getView().webContents.sendInputEvent({ runnerInstance.getView().webContents.sendInputEvent({
type: "keyDown", type: 'keyDown',
keyCode: code keyCode: code,
}); });
} }
} }
@ -273,11 +273,11 @@ class API {
window.setBrowserView(null); window.setBrowserView(null);
window.webContents window.webContents
.executeJavaScript(`window.getMainInputInfo()`) .executeJavaScript(`window.getMainInputInfo()`)
.then(res => { .then((res) => {
detachInstance.init( detachInstance.init(
{ {
...this.currentPlugin, ...this.currentPlugin,
subInput: res subInput: res,
}, },
window.getBounds(), window.getBounds(),
view view
@ -293,7 +293,7 @@ class API {
} }
public getLocalId() { public getLocalId() {
return encodeURIComponent(app.getPath("home")); return encodeURIComponent(app.getPath('home'));
} }
public shellShowItemInFolder({ data }) { public shellShowItemInFolder({ data }) {