Skip to content

Commit 7b8466f

Browse files
committed
💥 导出可执行的nodejs包
1 parent e976540 commit 7b8466f

7 files changed

Lines changed: 258 additions & 119 deletions

File tree

package-lock.json

Lines changed: 121 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptcat",
3-
"version": "0.8.2",
3+
"version": "0.9.0",
44
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
55
"scripts": {
66
"lint": "eslint --ext .js,.ts,.tsx tests/ src/",
@@ -28,6 +28,7 @@
2828
"monaco-editor": "^0.22.3",
2929
"reflect-metadata": "^0.1.13",
3030
"sortablejs": "^1.14.0",
31+
"tencentcloud-sdk-nodejs": "^4.0.278",
3132
"uuid": "^8.3.2",
3233
"vue": "^2.6.12",
3334
"vue-i18n": "^8.24.1",
@@ -93,4 +94,4 @@
9394
"webpack-bundle-analyzer": "^4.4.2",
9495
"webpack-cli": "^4.8.0"
9596
}
96-
}
97+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const utils = require('./utils');
2+
3+
utils.run();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "cloudcat-package",
3+
"version": "1.0.0",
4+
"description": "scriptcat后台脚本打包项目",
5+
"main": "index.js",
6+
"scripts": {
7+
"run": "node index.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "CodFrm",
11+
"license": "ISC",
12+
"dependencies": {
13+
"scriptcat-nodejs": "^0.1.2"
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { ScriptCat } = require("scriptcat-nodejs/dist/src/scriptcat");
2+
const fs = require('fs');
3+
4+
exports.run = function () {
5+
const code = fs.readFileSync('userScript.js', 'utf8');
6+
7+
const run = new ScriptCat();
8+
9+
run.RunOnce(code).then((res) => {
10+
console.log(res);
11+
});
12+
}
13+

src/types/scriptcat.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ declare function GM_registerMenuCommand(name: string, listener: () => void, acce
7777
declare function GM_unregisterMenuCommand(id: number): void;
7878

7979
declare interface tab {
80-
close()
80+
close(): void
8181
onclose?: () => void
8282
closed?: boolean
8383
name?: string
@@ -111,7 +111,7 @@ declare function GM_getCookieStore(tabid: number, ondone: (storeId: number, erro
111111
declare function CAT_setProxy(rule: CAT_Types.ProxyRule[] | string): void;
112112
declare function CAT_clearProxy(): void;
113113
declare function CAT_click(x: number, y: number): void;
114-
declare function CAT_createFile(file: string | Blob, name: string, ondone?: (download: boolean, error?: any | undefined) => void);
114+
declare function CAT_createFile(file: string | Blob, name: string, ondone?: (download: boolean, error?: any | undefined) => void): void;
115115

116116
declare namespace CAT_Types {
117117
interface ProxyRule {
@@ -192,6 +192,7 @@ declare namespace GM_Types {
192192
}
193193

194194
type Listener<OBJ> = (event: OBJ) => any;
195+
type ContextType = any;
195196

196197
interface XHRDetails {
197198
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
@@ -244,7 +245,7 @@ declare namespace GM_Types {
244245
onerror?: Listener<DownloadError>,
245246
ontimeout?: () => void,
246247
onload?: Listener<object>,
247-
onprogress?: Listener<XHRProgress<void>>
248+
onprogress?: Listener<XHRProgress>
248249
}
249250

250251
interface NotificationThis extends NotificationDetails {

src/views/components/BgCloud.vue

Lines changed: 99 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ import {
8585
import { v4 as uuidv4 } from 'uuid';
8686
import { mdiCloudUpload, mdiClose } from '@mdi/js';
8787
import { ExtVersion } from '@App/apps/config';
88+
import packageTpl from '@App/template/cloudcat-package/package.tpl';
89+
import utilsTpl from '@App/template/cloudcat-package/utils.tpl';
90+
import indexTpl from '@App/template/cloudcat-package/index.tpl';
91+
92+
8893
@Component({})
8994
export default class BgCloud extends Vue {
9095
icons = { mdiCloudUpload, mdiClose };
@@ -143,111 +148,131 @@ export default class BgCloud extends Vue {
143148
exportCookie: exportCookie,
144149
exportValue: exportValue,
145150
};
146-
this.exportModel.save(this.exportConfig);
151+
void this.exportModel.save(this.exportConfig);
147152
}
148153
}
149154
150155
submit() {
151156
switch (this.exportConfig.dest) {
152157
case EXPORT_DEST_LOCAL:
153-
this.local();
158+
void this.local();
154159
break;
155160
}
156161
}
157162
158163
async local() {
159164
let zip = await this.pack();
160-
this.exportModel.save(this.exportConfig);
161-
zip.generateAsync({ type: 'blob' }).then((content) => {
165+
void this.exportModel.save(this.exportConfig);
166+
void zip.generateAsync({ type: 'blob' }).then((content) => {
162167
saveAs(content, this.script.name + '.zip');
163168
});
164169
}
165170
166171
pack(): Promise<JSZip> {
167-
return new Promise(async (resolve) => {
168-
let zip = new JSZip();
169-
zip.file('userScript.js', this.script.code);
170-
let lines = this.exportConfig.exportCookie.split('\n');
171-
let cookies: { [key: string]: chrome.cookies.Cookie[] } = {};
172-
let cookie = false;
173-
for (let i = 0; i < lines.length; i++) {
174-
let val = lines[0];
175-
let detail: any = {};
176-
val.split(';').forEach((param) => {
177-
let s = param.split('=');
178-
if (s.length != 2) {
179-
return;
172+
return new Promise((resolve) => {
173+
const handler = async () => {
174+
let zip = new JSZip();
175+
zip.file('userScript.js', this.script.code);
176+
let lines = this.exportConfig.exportCookie.split('\n');
177+
let cookies: { [key: string]: chrome.cookies.Cookie[] } = {};
178+
let cookie = false;
179+
for (let i = 0; i < lines.length; i++) {
180+
let val = lines[0];
181+
if (!val) {
182+
continue;
183+
}
184+
let detail: chrome.cookies.GetAllDetails = {};
185+
val.split(';').forEach((param) => {
186+
let s = param.split('=');
187+
if (s.length != 2) {
188+
return;
189+
}
190+
(<AnyMap>detail)[s[0]] = s[1].trim();
191+
});
192+
if (!detail.url && !detail.domain) {
193+
continue;
194+
}
195+
cookie = true;
196+
if (detail.url) {
197+
let u = new URL(detail.url);
198+
cookies[u.host] = await this.getCookie(detail);
199+
} else if (detail.domain) {
200+
cookies[detail.domain] = await this.getCookie(detail);
180201
}
181-
detail[s[0]] = s[1].trim();
182-
});
183-
if (!detail.url && !detail.domain) {
184-
continue;
185-
}
186-
cookie = true;
187-
if (detail.url) {
188-
let u = new URL(detail.url);
189-
cookies[u.host] = await this.getCookie(detail);
190-
} else {
191-
cookies[detail.domain] = await this.getCookie(detail);
192202
}
193-
}
194-
cookie && zip.file('cookie.json', JSON.stringify(cookies));
195-
196-
lines = this.exportConfig.exportValue.split('\n');
197-
let values: Value[] = [];
198-
for (let i = 0; i < lines.length; i++) {
199-
let val = lines[0];
200-
let keys = val.split(',');
201-
for (let n = 0; n < keys.length; n++) {
202-
let value = await this.getValue(keys[n]);
203-
if (value) {
204-
values.push(value);
203+
cookie &&
204+
zip.file('cookie.js', 'export default ' + JSON.stringify(cookies));
205+
206+
lines = this.exportConfig.exportValue.split('\n');
207+
let values: Value[] = [];
208+
for (let i = 0; i < lines.length; i++) {
209+
let val = lines[0];
210+
let keys = val.split(',');
211+
for (let n = 0; n < keys.length; n++) {
212+
const key = keys[n];
213+
if (!key) {
214+
continue;
215+
}
216+
let value = await this.getValue(key);
217+
if (value) {
218+
values.push(value);
219+
}
205220
}
206221
}
207-
}
208-
zip.file('value.json', JSON.stringify(values));
209-
zip.file(
210-
'config.json',
211-
JSON.stringify({
212-
version: ExtVersion,
213-
uuid: this.exportConfig.uuid,
214-
overwrite: {
215-
value: this.exportConfig.overwriteValue,
216-
cookie: this.exportConfig.overwriteCookie,
217-
},
218-
})
219-
);
220-
resolve(zip);
222+
zip.file('value.js', 'export default ' + JSON.stringify(values));
223+
zip.file(
224+
'config.js',
225+
'export default ' +
226+
JSON.stringify({
227+
version: ExtVersion,
228+
uuid: this.exportConfig.uuid,
229+
overwrite: {
230+
value: this.exportConfig.overwriteValue,
231+
cookie: this.exportConfig.overwriteCookie,
232+
},
233+
})
234+
);
235+
zip.file('package.json',<string>packageTpl);
236+
zip.file('utils.js',<string>utilsTpl);
237+
zip.file('index.js',<string>indexTpl);
238+
resolve(zip);
239+
};
240+
void handler();
221241
});
222242
}
223243
224-
getCookie(detail: any): Promise<chrome.cookies.Cookie[]> {
244+
getCookie(
245+
detail: chrome.cookies.GetAllDetails
246+
): Promise<chrome.cookies.Cookie[]> {
225247
return new Promise((resolve) => {
226248
chrome.cookies.getAll(detail, (cookies) => {
227249
resolve(cookies);
228250
});
229251
});
230252
}
231253
232-
getValue(key: any): Promise<any> {
233-
return new Promise(async (resolve) => {
234-
let model: Value | undefined;
235-
if (this.script.metadata['storagename']) {
236-
model = await this.valueModel.findOne({
237-
storageName: this.script.metadata['storagename'][0],
238-
key: key,
239-
});
240-
} else {
241-
model = await this.valueModel.findOne({
242-
scriptId: this.script,
243-
key: key,
244-
});
245-
}
246-
if (model) {
247-
resolve(model);
248-
} else {
249-
resolve(undefined);
250-
}
254+
getValue(key: any): Promise<Value | undefined> {
255+
return new Promise((resolve) => {
256+
const handler = async () => {
257+
let model: Value | undefined;
258+
if (this.script.metadata['storagename']) {
259+
model = await this.valueModel.findOne({
260+
storageName: this.script.metadata['storagename'][0],
261+
key: key,
262+
});
263+
} else {
264+
model = await this.valueModel.findOne({
265+
scriptId: this.script,
266+
key: key,
267+
});
268+
}
269+
if (model) {
270+
resolve(model);
271+
} else {
272+
resolve(undefined);
273+
}
274+
};
275+
void handler();
251276
});
252277
}
253278
}

0 commit comments

Comments
 (0)