From 5d39549e69fdd14685abfc55ee9c0dabe6f915b2 Mon Sep 17 00:00:00 2001 From: gaoshuaixing <530353222@qq.com> Date: Wed, 13 Oct 2021 17:21:13 +0800 Subject: [PATCH] =?UTF-8?q?crx=E6=8F=92=E4=BB=B6demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/v1/example.js | 35 +++++---- app/router/example.js | 3 +- app/service/example.js | 6 ++ app/service/storage.js | 8 +- config/config.default.js | 2 +- electron/apis/example.js | 23 ++++++ frontend/src/config/router.config.js | 10 +-- frontend/src/layouts/DemoMenu.vue | 20 ++--- frontend/src/views/demo/extend/Index.vue | 77 ------------------- frontend/src/views/demo/extension/Index.vue | 82 +++++++++++++++++++++ package.json | 5 +- 11 files changed, 158 insertions(+), 113 deletions(-) delete mode 100644 frontend/src/views/demo/extend/Index.vue create mode 100644 frontend/src/views/demo/extension/Index.vue diff --git a/app/controller/v1/example.js b/app/controller/v1/example.js index 8f45cb6..7d9f479 100644 --- a/app/controller/v1/example.js +++ b/app/controller/v1/example.js @@ -61,20 +61,7 @@ class ExampleController extends BaseController { const self = this; const { ctx, service } = this; let tmpDir = service.storage.getStorageDir(); - // for (const file of ctx.request.files) { - // this.app.logger.info('file:', file); - - // try { - // let tmpFile = fs.readFileSync(file.filepath) - // fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile) - // } finally { - // await fs.unlink(file.filepath, function(){}); - // } - // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) - // const uploadRes = await service.example.uploadFileToSMMS(fileStream); - // } const file = ctx.request.files[0]; - //this.app.logger.info('file:', file); try { let tmpFile = fs.readFileSync(file.filepath) @@ -88,6 +75,28 @@ class ExampleController extends BaseController { self.sendData(uploadRes); } + async uploadExtension() { + const self = this; + const { ctx, service } = this; + const data = {}; + let tmpDir = service.storage.getStorageDir(); + + const file = ctx.request.files[0]; + this.app.logger.info('file:', file); + + try { + let tmpFile = fs.readFileSync(file.filepath) + fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile) + } finally { + await fs.unlink(file.filepath, function(){}); + } + + const filePath = path.join(tmpDir, file.filename); + await service.example.loadExtension(filePath); + + self.sendData(data); + } + async setShortcut() { const self = this; const { ctx, service } = this; diff --git a/app/router/example.js b/app/router/example.js index 62b22e1..d3cfa6b 100644 --- a/app/router/example.js +++ b/app/router/example.js @@ -39,5 +39,6 @@ module.exports = app => { router.post('/api/v1/example/messageShow', controller.v1.example.messageShow); // message show confirm router.post('/api/v1/example/messageShowConfirm', controller.v1.example.messageShowConfirm); - + // upload chrome extension + router.post('/api/v1/example/uploadExtension', controller.v1.example.uploadExtension); }; \ No newline at end of file diff --git a/app/service/example.js b/app/service/example.js index 01e8133..cecc66d 100644 --- a/app/service/example.js +++ b/app/service/example.js @@ -111,7 +111,13 @@ class ExampleService extends BaseService { return true; } + async loadExtension(filePath) { + const self = this; + await self.ipcCall('example.loadExtension', filePath); + + return true; + } } module.exports = ExampleService; \ No newline at end of file diff --git a/app/service/storage.js b/app/service/storage.js index 0c2a432..40ca522 100644 --- a/app/service/storage.js +++ b/app/service/storage.js @@ -59,7 +59,7 @@ class StorageService extends BaseService { } /* - * add Test data + * 增 Test data */ async addTestData(user) { const key = storageKey.TEST_DATA; @@ -76,7 +76,7 @@ class StorageService extends BaseService { } /* - * del Test data + * 删 Test data */ async delTestData(name = '') { const key = storageKey.TEST_DATA; @@ -89,7 +89,7 @@ class StorageService extends BaseService { } /* - * update Test data + * 改 Test data */ async updateTestData(name= '', age = 0) { const key = storageKey.TEST_DATA; @@ -103,7 +103,7 @@ class StorageService extends BaseService { } /* - * get Test data + * 查 Test data */ async getTestData(name = '') { const key = storageKey.TEST_DATA; diff --git a/config/config.default.js b/config/config.default.js index e679a04..26c62e9 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -94,7 +94,7 @@ module.exports = appInfo => { config.multipart = { mode: 'file', - fileExtensions: [ '.xlsx' ] // 增加你需要的文件扩展名 + fileExtensions: [ '.xlsx', '.crx' ] // 增加你需要的文件扩展名 }; return { diff --git a/electron/apis/example.js b/electron/apis/example.js index b931212..88848be 100644 --- a/electron/apis/example.js +++ b/electron/apis/example.js @@ -11,6 +11,8 @@ const {exec} = require('child_process'); const {app, webContents, shell, dialog} = require('electron'); const AutoLaunchManager = require('../lib/autoLaunch'); const shortcut = require('../lib/shortcut'); +const chromeExtension = require('../lib/chromeExtension'); +const unzip = require("unzip-crx-3"); /** * app根目录 @@ -166,6 +168,27 @@ exports.messageShowConfirm = function () { return true; } +/** + * 加载扩展程序 + */ +exports.loadExtension = async function (crxFile) { + if (_.isEmpty(crxFile)) { + return false; + } + + const extensionId = path.basename(crxFile, '.crx'); + const chromeExtensionDir = chromeExtension.getDirectory(); + const extensionDir = path.join(chromeExtensionDir, extensionId); + + console.log("[api] [example] [loadExtension] extension id:", extensionId); + unzip(crxFile, extensionDir).then(() => { + console.log("[api] [example] [loadExtension] unzip success!"); + chromeExtension.load(extensionId); + }); + + return true; +} + function getElectronPath(filepath) { //filepath = path.resolve(filepath); filepath = filepath.replace("resources", ""); diff --git a/frontend/src/config/router.config.js b/frontend/src/config/router.config.js index 6623b9b..10a75ad 100644 --- a/frontend/src/config/router.config.js +++ b/frontend/src/config/router.config.js @@ -28,11 +28,6 @@ export const constantRouterMap = [ name: 'DemoSocketIndex', component: () => import('@/views/demo/socket/Index') }, - { - path: '/demo/extend/index', - name: 'DemoExtendIndex', - component: () => import('@/views/demo/extend/Index') - }, { path: '/demo/windowview/index', name: 'DemoWindowViewIndex', @@ -43,6 +38,11 @@ export const constantRouterMap = [ name: 'DemoWindowIndex', component: () => import('@/views/demo/window/Index') }, + { + path: '/demo/extension/index', + name: 'DemoExtensionIndex', + component: () => import('@/views/demo/extension/Index') + }, { path: '/demo/notification/index', name: 'DemoNotificationIndex', diff --git a/frontend/src/layouts/DemoMenu.vue b/frontend/src/layouts/DemoMenu.vue index 4ef196f..cb72353 100644 --- a/frontend/src/layouts/DemoMenu.vue +++ b/frontend/src/layouts/DemoMenu.vue @@ -36,12 +36,6 @@ export default { pageName: 'DemoSocketIndex', params: {} }, - 'menu_301' : { - icon: 'profile', - title: '扩展程序', - pageName: 'DemoExtendIndex', - params: {} - }, 'menu_400' : { icon: 'profile', title: '视图', @@ -54,25 +48,31 @@ export default { pageName: 'DemoWindowIndex', params: {} }, - 'menu_402' : { + 'menu_403' : { + icon: 'profile', + title: '扩展程序', + pageName: 'DemoExtensionIndex', + params: {} + }, + 'menu_405' : { icon: 'profile', title: '桌面通知', pageName: 'DemoNotificationIndex', params: {} }, - 'menu_403' : { + 'menu_407' : { icon: 'profile', title: '电源监控', pageName: 'DemoPowerMonitorIndex', params: {} }, - 'menu_404' : { + 'menu_409' : { icon: 'profile', title: '屏幕信息', pageName: 'DemoScreenIndex', params: {} }, - 'menu_405' : { + 'menu_411' : { icon: 'profile', title: '系统主题', pageName: 'DemoThemeIndex', diff --git a/frontend/src/views/demo/extend/Index.vue b/frontend/src/views/demo/extend/Index.vue deleted file mode 100644 index cf037a5..0000000 --- a/frontend/src/views/demo/extend/Index.vue +++ /dev/null @@ -1,77 +0,0 @@ - - - - - 1. - - - - - 获取模式 - - - 结果:{{ currentThemeMode }} - - - - 2. 设置主题模式(请自行实现前端UI效果) - - - - - - - - diff --git a/frontend/src/views/demo/extension/Index.vue b/frontend/src/views/demo/extension/Index.vue new file mode 100644 index 0000000..185d3e7 --- /dev/null +++ b/frontend/src/views/demo/extension/Index.vue @@ -0,0 +1,82 @@ + + + + + 1. 上传扩展程序(crx文件格式) + + + + + + + + + Click or drag file to this area to upload + + + + + + + 2. chrome扩展商店(crx下载) + + + + 极简插件:https://chrome.zzzmh.cn/ + + + + + + diff --git a/package.json b/package.json index c75870b..6bf0601 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron-egg", - "version": "1.14.0", + "version": "1.15.0", "description": "A fast, desktop software development framework", "main": "main.js", "scripts": { @@ -117,6 +117,7 @@ "lowdb": "^1.0.0", "semver": "^5.4.1", "socket.io": "^3.0.5", - "socket.io-client": "^3.0.5" + "socket.io-client": "^3.0.5", + "unzip-crx-3": "^0.2.0" } }
+ +
+ Click or drag file to this area to upload +
+