@@ -148,8 +148,7 @@ const getUrl = (prefix, link) => {
148
148
}
149
149
return prefix + link
150
150
}
151
-
152
- const convert = ( options , customOptions ) => {
151
+ const convert = async ( options , customOptions ) => {
153
152
const context = { }
154
153
const defaultOptions = {
155
154
origin : 'juejin' ,
@@ -170,9 +169,8 @@ const convert = (options, customOptions) => {
170
169
unpack : ''
171
170
}
172
171
}
173
- options = options instanceof Object ? options : { }
174
172
customOptions = customOptions instanceof Object ? customOptions : { }
175
- options = merge ( { } , defaultOptions , options , customOptions )
173
+ options = merge ( { } , defaultOptions , options instanceof Object ? options : { } , customOptions )
176
174
if ( options . context ) {
177
175
if ( typeof options . context === 'string' ) {
178
176
const el = document . createElement ( 'div' )
@@ -183,13 +181,14 @@ const convert = (options, customOptions) => {
183
181
}
184
182
}
185
183
const { origin, selectors} = options
186
- const markdownBody = query ( selectors . body , options . context ) . cloneNode ( true )
187
184
const hook = hooks [ origin ] || { }
188
-
189
- noop ( hook . beforeExtract ) ( Object . assign ( context , {
190
- options,
191
- markdownBody
185
+ const result = await noop ( hook . beforeExtract ) ( Object . assign ( context , {
186
+ options
192
187
} ) )
188
+ if ( result instanceof Object ) {
189
+ return result
190
+ }
191
+ const markdownBody = query ( selectors . body , options . context ) . cloneNode ( true )
193
192
queryAll ( selectors . copyBtn , markdownBody ) . map ( item => item . parentElement . removeChild ( item ) )
194
193
queryAll ( '[data-id]' , markdownBody ) . map ( item => item . removeAttribute ( 'data-id' ) )
195
194
if ( selectors . invalid ) {
@@ -246,27 +245,27 @@ const convert = (options, customOptions) => {
246
245
name : realName + '/urls' ,
247
246
content : urls . join ( '\n' )
248
247
} )
249
- noop ( hook . extractAfter ) ( Object . assign ( context , { files } ) )
248
+ noop ( hook . afterExtract ) ( Object . assign ( context , { files } ) )
250
249
return {
251
250
type : 'download' ,
252
251
fileName,
253
252
files
254
253
}
255
254
}
256
255
257
- const extract = ( options , customOptions ) => {
258
- const datas = convert ( options , customOptions )
256
+ const extract = async ( options , customOptions ) => {
257
+ const datas = await convert ( options , customOptions )
259
258
sendMessage ( datas )
260
259
return datas
261
260
}
262
261
263
262
if ( isBroswer ) {
264
263
if ( isExtension ) {
265
- chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
264
+ chrome . runtime . onMessage . addListener ( async ( message , sender , sendResponse ) => {
266
265
if ( message instanceof Object ) {
267
266
if ( message . type === 'download' ) {
268
267
if ( typeof websites [ message . website ] === 'function' ) {
269
- websites [ message . website ] ( extract )
268
+ await websites [ message . website ] ( extract )
270
269
}
271
270
}
272
271
}
0 commit comments