Files
electron-egg/electron/apis/example.js
gaoshuaixing 312fcaa31f 1.12.0
2021-07-27 15:06:46 +08:00

112 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const path = require('path');
const fs = require('fs');
const {exec} = require('child_process');
const {app, webContents, shell} = require('electron');
const AutoLaunchManager = require('../lib/autoLaunch');
const shortcut = require('../lib/shortcut');
const eLogger = require('../lib/eLogger').get();
/**
* app根目录
*/
exports.getPath = function () {
const dir = app.getAppPath();
return dir;
}
/**
* 打开目录
*/
exports.openDir = function (dir = '') {
if (!dir) {
return false;
}
dir = getElectronPath(dir);
shell.openPath(dir);
return true;
}
/**
* 执行js
*/
exports.executeJS = function (str) {
let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`;
console.log(jscode);
return webContents.fromId(1).executeJavaScript(jscode);
}
/**
* 快捷键-注册
*/
exports.setShortcut = function (shortcutObj) {
shortcut.register(shortcutObj, true, function (){
MAIN_WINDOW.hide()
});
return true;
}
/**
* 开机启动-开启
*/
exports.autoLaunchEnable = function () {
const autoLaunchManager = new AutoLaunchManager()
const enable = autoLaunchManager.enable()
return enable
}
/**
* 开机启动-关闭
*/
exports.autoLaunchDisable = function () {
const autoLaunchManager = new AutoLaunchManager()
const disable = autoLaunchManager.disable()
return disable
}
/**
* 开机启动-是否开启
*/
exports.autoLaunchIsEnabled = function () {
const autoLaunchManager = new AutoLaunchManager()
const isEnable = autoLaunchManager.isEnabled()
return isEnable
}
/**
* 调用其它程序exe、bash等可执行程序
*/
exports.openSoftware = function (softName = '') {
if (!softName) {
return false;
}
// 资源路径不同
let softwarePath = '';
if (app.isPackaged) {
// 打包后
softwarePath = path.join(app.getAppPath(), "..", "extraResources", softName);
} else {
// 打包前
softwarePath = path.join(app.getAppPath(), "build", "extraResources", softName);
}
// 检查程序是否存在
if (!fs.existsSync(softwarePath)) {
return false;
}
// 命令行字符串 并 执行
let cmdStr = 'start ' + softwarePath;
exec(cmdStr);
return true;
}
function getElectronPath(filepath) {
//filepath = path.resolve(filepath);
filepath = filepath.replace("resources", "");
filepath = filepath.replace("app.asar", "");
filepath = path.normalize(filepath);
return filepath;
};