Skip to content

Commit

Permalink
💥 导出可执行的nodejs包
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 22, 2022
1 parent e976540 commit 7b8466f
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 119 deletions.
161 changes: 121 additions & 40 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scriptcat",
"version": "0.8.2",
"version": "0.9.0",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"scripts": {
"lint": "eslint --ext .js,.ts,.tsx tests/ src/",
Expand Down Expand Up @@ -28,6 +28,7 @@
"monaco-editor": "^0.22.3",
"reflect-metadata": "^0.1.13",
"sortablejs": "^1.14.0",
"tencentcloud-sdk-nodejs": "^4.0.278",
"uuid": "^8.3.2",
"vue": "^2.6.12",
"vue-i18n": "^8.24.1",
Expand Down Expand Up @@ -93,4 +94,4 @@
"webpack-bundle-analyzer": "^4.4.2",
"webpack-cli": "^4.8.0"
}
}
}
3 changes: 3 additions & 0 deletions src/template/cloudcat-package/index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const utils = require('./utils');

utils.run();
15 changes: 15 additions & 0 deletions src/template/cloudcat-package/package.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "cloudcat-package",
"version": "1.0.0",
"description": "scriptcat后台脚本打包项目",
"main": "index.js",
"scripts": {
"run": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "CodFrm",
"license": "ISC",
"dependencies": {
"scriptcat-nodejs": "^0.1.2"
}
}
13 changes: 13 additions & 0 deletions src/template/cloudcat-package/utils.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { ScriptCat } = require("scriptcat-nodejs/dist/src/scriptcat");
const fs = require('fs');

exports.run = function () {
const code = fs.readFileSync('userScript.js', 'utf8');
const run = new ScriptCat();
run.RunOnce(code).then((res) => {
console.log(res);
});
}

7 changes: 4 additions & 3 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ declare function GM_registerMenuCommand(name: string, listener: () => void, acce
declare function GM_unregisterMenuCommand(id: number): void;

declare interface tab {
close()
close(): void
onclose?: () => void
closed?: boolean
name?: string
Expand Down Expand Up @@ -111,7 +111,7 @@ declare function GM_getCookieStore(tabid: number, ondone: (storeId: number, erro
declare function CAT_setProxy(rule: CAT_Types.ProxyRule[] | string): void;
declare function CAT_clearProxy(): void;
declare function CAT_click(x: number, y: number): void;
declare function CAT_createFile(file: string | Blob, name: string, ondone?: (download: boolean, error?: any | undefined) => void);
declare function CAT_createFile(file: string | Blob, name: string, ondone?: (download: boolean, error?: any | undefined) => void): void;

declare namespace CAT_Types {
interface ProxyRule {
Expand Down Expand Up @@ -192,6 +192,7 @@ declare namespace GM_Types {
}

type Listener<OBJ> = (event: OBJ) => any;
type ContextType = any;

interface XHRDetails {
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
Expand Down Expand Up @@ -244,7 +245,7 @@ declare namespace GM_Types {
onerror?: Listener<DownloadError>,
ontimeout?: () => void,
onload?: Listener<object>,
onprogress?: Listener<XHRProgress<void>>
onprogress?: Listener<XHRProgress>
}

interface NotificationThis extends NotificationDetails {
Expand Down
173 changes: 99 additions & 74 deletions src/views/components/BgCloud.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ import {
import { v4 as uuidv4 } from 'uuid';
import { mdiCloudUpload, mdiClose } from '@mdi/js';
import { ExtVersion } from '@App/apps/config';
import packageTpl from '@App/template/cloudcat-package/package.tpl';
import utilsTpl from '@App/template/cloudcat-package/utils.tpl';
import indexTpl from '@App/template/cloudcat-package/index.tpl';
@Component({})
export default class BgCloud extends Vue {
icons = { mdiCloudUpload, mdiClose };
Expand Down Expand Up @@ -143,111 +148,131 @@ export default class BgCloud extends Vue {
exportCookie: exportCookie,
exportValue: exportValue,
};
this.exportModel.save(this.exportConfig);
void this.exportModel.save(this.exportConfig);
}
}
submit() {
switch (this.exportConfig.dest) {
case EXPORT_DEST_LOCAL:
this.local();
void this.local();
break;
}
}
async local() {
let zip = await this.pack();
this.exportModel.save(this.exportConfig);
zip.generateAsync({ type: 'blob' }).then((content) => {
void this.exportModel.save(this.exportConfig);
void zip.generateAsync({ type: 'blob' }).then((content) => {
saveAs(content, this.script.name + '.zip');
});
}
pack(): Promise<JSZip> {
return new Promise(async (resolve) => {
let zip = new JSZip();
zip.file('userScript.js', this.script.code);
let lines = this.exportConfig.exportCookie.split('\n');
let cookies: { [key: string]: chrome.cookies.Cookie[] } = {};
let cookie = false;
for (let i = 0; i < lines.length; i++) {
let val = lines[0];
let detail: any = {};
val.split(';').forEach((param) => {
let s = param.split('=');
if (s.length != 2) {
return;
return new Promise((resolve) => {
const handler = async () => {
let zip = new JSZip();
zip.file('userScript.js', this.script.code);
let lines = this.exportConfig.exportCookie.split('\n');
let cookies: { [key: string]: chrome.cookies.Cookie[] } = {};
let cookie = false;
for (let i = 0; i < lines.length; i++) {
let val = lines[0];
if (!val) {
continue;
}
let detail: chrome.cookies.GetAllDetails = {};
val.split(';').forEach((param) => {
let s = param.split('=');
if (s.length != 2) {
return;
}
(<AnyMap>detail)[s[0]] = s[1].trim();
});
if (!detail.url && !detail.domain) {
continue;
}
cookie = true;
if (detail.url) {
let u = new URL(detail.url);
cookies[u.host] = await this.getCookie(detail);
} else if (detail.domain) {
cookies[detail.domain] = await this.getCookie(detail);
}
detail[s[0]] = s[1].trim();
});
if (!detail.url && !detail.domain) {
continue;
}
cookie = true;
if (detail.url) {
let u = new URL(detail.url);
cookies[u.host] = await this.getCookie(detail);
} else {
cookies[detail.domain] = await this.getCookie(detail);
}
}
cookie && zip.file('cookie.json', JSON.stringify(cookies));
lines = this.exportConfig.exportValue.split('\n');
let values: Value[] = [];
for (let i = 0; i < lines.length; i++) {
let val = lines[0];
let keys = val.split(',');
for (let n = 0; n < keys.length; n++) {
let value = await this.getValue(keys[n]);
if (value) {
values.push(value);
cookie &&
zip.file('cookie.js', 'export default ' + JSON.stringify(cookies));
lines = this.exportConfig.exportValue.split('\n');
let values: Value[] = [];
for (let i = 0; i < lines.length; i++) {
let val = lines[0];
let keys = val.split(',');
for (let n = 0; n < keys.length; n++) {
const key = keys[n];
if (!key) {
continue;
}
let value = await this.getValue(key);
if (value) {
values.push(value);
}
}
}
}
zip.file('value.json', JSON.stringify(values));
zip.file(
'config.json',
JSON.stringify({
version: ExtVersion,
uuid: this.exportConfig.uuid,
overwrite: {
value: this.exportConfig.overwriteValue,
cookie: this.exportConfig.overwriteCookie,
},
})
);
resolve(zip);
zip.file('value.js', 'export default ' + JSON.stringify(values));
zip.file(
'config.js',
'export default ' +
JSON.stringify({
version: ExtVersion,
uuid: this.exportConfig.uuid,
overwrite: {
value: this.exportConfig.overwriteValue,
cookie: this.exportConfig.overwriteCookie,
},
})
);
zip.file('package.json',<string>packageTpl);
zip.file('utils.js',<string>utilsTpl);
zip.file('index.js',<string>indexTpl);
resolve(zip);
};
void handler();
});
}
getCookie(detail: any): Promise<chrome.cookies.Cookie[]> {
getCookie(
detail: chrome.cookies.GetAllDetails
): Promise<chrome.cookies.Cookie[]> {
return new Promise((resolve) => {
chrome.cookies.getAll(detail, (cookies) => {
resolve(cookies);
});
});
}
getValue(key: any): Promise<any> {
return new Promise(async (resolve) => {
let model: Value | undefined;
if (this.script.metadata['storagename']) {
model = await this.valueModel.findOne({
storageName: this.script.metadata['storagename'][0],
key: key,
});
} else {
model = await this.valueModel.findOne({
scriptId: this.script,
key: key,
});
}
if (model) {
resolve(model);
} else {
resolve(undefined);
}
getValue(key: any): Promise<Value | undefined> {
return new Promise((resolve) => {
const handler = async () => {
let model: Value | undefined;
if (this.script.metadata['storagename']) {
model = await this.valueModel.findOne({
storageName: this.script.metadata['storagename'][0],
key: key,
});
} else {
model = await this.valueModel.findOne({
scriptId: this.script,
key: key,
});
}
if (model) {
resolve(model);
} else {
resolve(undefined);
}
};
void handler();
});
}
}
Expand Down

0 comments on commit 7b8466f

Please sign in to comment.