@@ -85,6 +85,11 @@ import {
8585import { v4 as uuidv4 } from ' uuid' ;
8686import { mdiCloudUpload , mdiClose } from ' @mdi/js' ;
8787import { 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 ({})
8994export 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