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