Skip to content

Commit 4026a39

Browse files
committed
调整hook
1 parent 721f752 commit 4026a39

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-downloader",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "markdown文章下载",
55
"main": "dist/index.js",
66
"scripts": {

src/index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ const getUrl = (prefix, link) => {
148148
}
149149
return prefix + link
150150
}
151-
152-
const convert = (options, customOptions) => {
151+
const convert = async (options, customOptions) => {
153152
const context = {}
154153
const defaultOptions = {
155154
origin: 'juejin',
@@ -170,9 +169,8 @@ const convert = (options, customOptions) => {
170169
unpack: ''
171170
}
172171
}
173-
options = options instanceof Object ? options : {}
174172
customOptions = customOptions instanceof Object ? customOptions : {}
175-
options = merge({}, defaultOptions, options, customOptions)
173+
options = merge({}, defaultOptions, options instanceof Object ? options : {}, customOptions)
176174
if (options.context) {
177175
if (typeof options.context === 'string') {
178176
const el = document.createElement('div')
@@ -183,13 +181,14 @@ const convert = (options, customOptions) => {
183181
}
184182
}
185183
const {origin, selectors} = options
186-
const markdownBody = query(selectors.body, options.context).cloneNode(true)
187184
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
192187
}))
188+
if (result instanceof Object) {
189+
return result
190+
}
191+
const markdownBody = query(selectors.body, options.context).cloneNode(true)
193192
queryAll(selectors.copyBtn, markdownBody).map(item => item.parentElement.removeChild(item))
194193
queryAll('[data-id]', markdownBody).map(item => item.removeAttribute('data-id'))
195194
if (selectors.invalid) {
@@ -246,27 +245,27 @@ const convert = (options, customOptions) => {
246245
name: realName + '/urls',
247246
content: urls.join('\n')
248247
})
249-
noop(hook.extractAfter)(Object.assign(context, { files }))
248+
noop(hook.afterExtract)(Object.assign(context, { files }))
250249
return {
251250
type: 'download',
252251
fileName,
253252
files
254253
}
255254
}
256255

257-
const extract = (options, customOptions) => {
258-
const datas = convert(options, customOptions)
256+
const extract = async (options, customOptions) => {
257+
const datas = await convert(options, customOptions)
259258
sendMessage(datas)
260259
return datas
261260
}
262261

263262
if (isBroswer) {
264263
if (isExtension) {
265-
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
264+
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
266265
if (message instanceof Object) {
267266
if (message.type === 'download') {
268267
if (typeof websites[message.website] === 'function') {
269-
websites[message.website](extract)
268+
await websites[message.website](extract)
270269
}
271270
}
272271
}

src/websites/default.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const hosts = [/./]
2+
3+
export const options = {
4+
origin: 'default'
5+
}
6+
7+
export const hook = {
8+
beforeExtract () {
9+
const fileName = document.title
10+
const name = document.title.replace(/(\/\\:\*\?\|<>")/g, '_') + '.html'
11+
const downloadUrl = location.href
12+
return {
13+
type: 'download',
14+
fileName,
15+
files: [
16+
{
17+
name,
18+
downloadUrl
19+
}
20+
]
21+
}
22+
}
23+
}
24+
25+
export const config = {
26+
hosts,
27+
options,
28+
hook
29+
}
30+
31+
export default config

src/websites/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@ export const websites = {}
22
export const hooks = {}
33
export const configs = []
44
const files = require.context('./', true, /\.js/)
5+
const assigns = {
6+
index: {},
7+
default: {}
8+
}
9+
510
files.keys().forEach(key => {
611
const website = (key.split('/').pop().split('.') || {}) [0]
712
if (![
813
'index'
914
].includes(website)) {
1015
const config = files(key)
11-
const { hook, options } = config
12-
websites[website] = (extract) => extract(options)
16+
const { hook, options, customOptions = {} } = config
17+
websites[website] = (extract) => extract(options, customOptions)
1318
hooks[website] = hook instanceof Object ? hook : {}
14-
configs.push(Object.assign({
15-
website
16-
}, config))
19+
if (assigns[website]) {
20+
assigns[website] = Object.assign({
21+
website
22+
}, config)
23+
} else {
24+
configs.push(Object.assign({
25+
website
26+
}, config))
27+
}
1728
}
1829
})
30+
configs.push(assigns.default)
1931

2032
export default {
2133
websites,

0 commit comments

Comments
 (0)