Skip to content

Commit 9bef1dc

Browse files
committed
增加选项页
1 parent 839b80a commit 9bef1dc

11 files changed

+110
-75
lines changed

package.json

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

src/background.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@ const sendCallback = (sendResponse, responseHeaders) => {
2525
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
2626
const { type, responseHeaders } = message
2727
const { url, data, dataType, callback } = message
28-
const { fileName, files, options } = message
28+
const { fileName, files, options = {} } = message
2929
const { sendSuccess, sendError } = sendCallback(sendResponse, responseHeaders)
30-
if (/(get|post)/i.test(type)) {
31-
ajax({
32-
url,
33-
method: type,
34-
data,
35-
dataType,
36-
success (data, xhr) {
37-
if (/text|blob/i.test(dataType)) {
38-
sendSuccess(data, xhr)
39-
} else {
40-
const obj = dataType === 'text' ? data : JSON.parse(data)
41-
const result = /^json$/i.test(dataType) ? {
42-
callback: noop(callback),
43-
data: obj
44-
} : data
45-
if (typeof callback === 'string' && callback) {
46-
sendSuccess(result, xhr)
30+
if (/(get|post)/i.test(type)) {
31+
ajax({
32+
url,
33+
method: type,
34+
data,
35+
dataType,
36+
success (data, xhr) {
37+
if (/text|blob/i.test(dataType)) {
38+
sendSuccess(data, xhr)
4739
} else {
48-
sendSuccess(obj, xhr)
40+
const obj = dataType === 'text' ? data : JSON.parse(data)
41+
const result = /^json$/i.test(dataType) ? {
42+
callback: noop(callback),
43+
data: obj
44+
} : data
45+
if (typeof callback === 'string' && callback) {
46+
sendSuccess(result, xhr)
47+
} else {
48+
sendSuccess(obj, xhr)
49+
}
4950
}
51+
},
52+
error (error, xhr) {
53+
sendError(error, xhr)
5054
}
51-
},
52-
error (error, xhr) {
53-
sendError(error, xhr)
54-
}
55-
})
56-
} else if (type === 'download') {
57-
downloadZip(fileName, files, options)
58-
}
55+
})
56+
} else if (type === 'download') {
57+
downloadZip(fileName, files, options)
58+
}
5959
return true
6060
})
6161

src/download.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import md5 from 'md5'
22
import JSZip from 'jszip'
33
import FileSaver from 'jszip/vendor/FileSaver'
4+
import { getLocalOptions } from './utils'
45

56
const defaultOptions = {
67
partLimit: 1e3,
@@ -13,14 +14,16 @@ const options = Object.assign({}, defaultOptions)
1314
export const mergeOptions = (newOptions) => {
1415
return Object.assign(options, defaultOptions, newOptions instanceof Object ? newOptions : {})
1516
}
16-
17+
export const mergeLocalOptions = async () => {
18+
return Object.assign(mergeOptions(await getLocalOptions()))
19+
}
1720

1821
export const noop = (func, defaultFunc) => {
1922
return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : () => {}
2023
}
2124

22-
export const ajax = (options) => {
23-
options = Object.assign({}, defaultOptions, options)
25+
export const ajax = async (options) => {
26+
const config = await mergeLocalOptions()
2427
const core = (retry = 3) => {
2528
const xhr = new XMLHttpRequest()
2629
options.method = options.method || 'get'
@@ -53,7 +56,7 @@ export const ajax = (options) => {
5356
xhr.send()
5457
}
5558
}
56-
core(options.retry)
59+
core(config.retry)
5760
}
5861

5962
export const fetchBlobFile = (file) =>{
@@ -126,9 +129,9 @@ export const partDownload = (fileName, files, { partLimit } = options) => {
126129
}, partLimit)
127130
}
128131

129-
export const downloadZip = (fileName, files, options = {}) => {
132+
export const downloadZip = async (fileName, files, options = {}) => {
130133
fileName = fileName || md5(files.map(item => item.downloadUrl).join('|'))
131-
return partDownload(fileName, files, mergeOptions(options))
134+
return partDownload(fileName, files, await mergeLocalOptions(options))
132135
}
133136

134137
export default downloadZip

src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { websites } from './websites'
22
import {
33
isExtension,
44
sendMessage,
5+
getLocalOptions
56
} from './utils'
67
import { downloadMarkdown } from './markdown'
78

89
const extract = async (options, customOptions, hook) => {
9-
const data = await downloadMarkdown(options, customOptions, hook)
10+
const localOptions = await getLocalOptions()
11+
const data = await downloadMarkdown(options, Object.assign(customOptions, {
12+
localOptions
13+
}), hook)
1014
data && sendMessage(data)
1115
return data
1216
}

src/main.092cdea9.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.d77d57b4.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/manifest.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"browser_action": {
1717
"default_icon": "icon.png"
1818
},
19+
"options_page": "option.html",
1920
"content_scripts": [
2021
{
2122
"matches": [

src/markdown.js

+29-35
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ import md5 from 'md5'
33
import html2markdown from 'html-to-md'
44
import 'mathjax/es5/tex-svg'
55
import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, formatDate, exec } from './utils'
6-
7-
const setInfo = (data) => {
6+
const replace = (str, fn) => {
7+
fn = typeof fn === 'function' ? fn : (s) => s
8+
return str.replace(/\$\{(.*?)\}/g, (s, s1) => fn(s1.replace(/(^\s+|\s+$)/g, '')))
9+
}
10+
const setInfo = (data, tpl) => {
811
data = Object.assign({
9-
date: formatDate('yyyy-MM-dd HH:mm:ss'),
12+
now: formatDate('yyyy-MM-dd HH:mm:ss'),
1013
copyright: false,
1114
url: location.href,
1215
description: '转载',
1316
tag: []
1417
}, data instanceof Object ? data : {})
15-
return `---
16-
title: {{title}}
17-
date: {{date}}
18-
copyright: {{copyright}}
19-
author: {{author}}
20-
home: {{home}}
21-
origin: {{origin}}
22-
url: {{url}}
23-
tag: ${ data.tag && data.tag.length ? '\n - ' + data.tag.join('\n - ') : '' }
24-
categories: {{categories}}
25-
description: {{description}}
18+
tpl = typeof tpl === 'string' ? tpl : `---
19+
title: \${title}
20+
date: \${now}
21+
copyright: \${copyright}
22+
author: \${author}
23+
home: \${home}
24+
origin: \${origin}
25+
url: \${url}
26+
tag: \${tag}
27+
categories: \${categories}
28+
description: \${description}
2629
---
27-
`.replace(/\n\s+/g, '\n').replace(/\{\{(.*?)\}\}/g, (s, s1) => data[s1] === void 0 ? '' : data[s1])
30+
`
31+
return replace(tpl.replace(/\n\s+/g, '\n'), (s1) => data[s1] === void 0 ? '' : Array.isArray(data[s1]) ? '\n - ' + data[s1].join('\n - ') : data[s1])
2832
}
29-
const formatCopyRight = (fileName) => {
30-
return `> 当前文档由 [markdown文档下载插件](https://github.com/kscript/markdown-download) 下载, 原文链接: [${fileName}](${location.href}) `
33+
const formatCopyRight = (data, { retain, copyright }) => {
34+
const tpl = retain ? '> 当前文档由 [markdown文档下载插件](https://github.com/kscript/markdown-download) 下载, 原文链接: [${title}](${url}) ' : copyright
35+
return typeof tpl === 'string' ? '\n\n' + replace(tpl, (s1) => data[s1] || '') + '\n\n' : ''
3136
}
3237
const getMarkdown = (markdownBody) => {
3338
return markdownBody.innerHTML
@@ -49,23 +54,12 @@ export const tex2svg = (markdownDoc) => {
4954

5055
const formatParams = (options, customOptions, hook) => {
5156
const defaultOptions = {
52-
origin: 'juejin',
53-
// 处理链接
54-
link: true,
55-
// 处理换行
5657
br: false,
57-
// 处理代码块
5858
code: false,
59+
link: true,
5960
lazyKey: 'data-src',
60-
selectors: {
61-
title: '.article-title',
62-
body: '.markdown-body',
63-
copyBtn: '.copy-code-btn',
64-
userName: '.username .name',
65-
userLink: '.username',
66-
invalid: 'style',
67-
unpack: ''
68-
}
61+
origin: '',
62+
selectors: {}
6963
}
7064
customOptions = customOptions instanceof Object ? customOptions : {}
7165
options = merge({}, defaultOptions, options instanceof Object ? options : {}, customOptions)
@@ -124,7 +118,7 @@ export const formatMarkdownBody = (container, selectors, options, exec) => {
124118
}
125119

126120
const extract = async (markdownBody, selectors, options, exec) => {
127-
const { origin, context } = options
121+
const { origin, context, localOptions = {} } = options
128122
const fileName = getText(selectors.title) || document.title
129123
const realName = fileName.replace(/[\\\/\?<>:'\*\|]/g, '_')
130124
const files = queryAll('img', markdownBody).map(item => {
@@ -154,13 +148,13 @@ const extract = async (markdownBody, selectors, options, exec) => {
154148
home: getUrl(location.origin, getAttribute('href', selectors.userLink)),
155149
tag: context.tag,
156150
description: markdownBody.innerText.replace(/^([\n\s]+)/g, '').replace(/\n/g, ' ').slice(0, 50) + '...',
157-
})
151+
}, localOptions.tpl)
158152
const markdownDoc = html2markdown(info + getMarkdown(markdownBody), {})
159-
const copyright = formatCopyRight(fileName)
153+
const copyright = formatCopyRight({ title: fileName, url: location.href }, localOptions)
160154
const content = await exec('formatContent', { markdownBody, markdownDoc })
161155
files.push({
162156
name: realName + '.md',
163-
content: (content && typeof content === 'string' ? content : markdownDoc) + '\n\n' + copyright
157+
content: `${content && typeof content === 'string' ? content : markdownDoc}${copyright}`
164158
})
165159
return {
166160
fileName,

src/option.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!doctype html>
2+
<html lang="zh-hans">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
<meta name="theme-color" content="#000000" />
7+
<meta name="description" content="插件配置" />
8+
<title>插件配置</title>
9+
<script defer="defer" src="main.d77d57b4.js"></script>
10+
<link href="main.092cdea9.css" rel="stylesheet">
11+
<link href="main.css" rel="stylesheet">
12+
</head>
13+
14+
<body>
15+
<noscript>You need to enable JavaScript to run this app.</noscript>
16+
<div id="root"></div>
17+
</body>
18+
19+
</html>

src/utils.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path-browserify'
22

3-
export const isBroswer = typeof window !== 'undefined' && window instanceof Object
4-
export const isExtension = isBroswer && window.chrome instanceof Object && window.chrome.runtime
3+
export const isBrowser = typeof window !== 'undefined' && window instanceof Object
4+
export const isExtension = isBrowser && window.chrome instanceof Object && window.chrome.runtime
55
export const getExt = (fileName) => {
66
return path.parse(fileName).ext.slice(1)
77
}
@@ -111,9 +111,15 @@ export const exec = async (...rest) => {
111111
console.warn(err)
112112
}
113113
}
114-
114+
export const getLocalOptions = () => {
115+
return new Promise((resolve) => {
116+
chrome.storage.local.get('localOptions', ({ localOptions }) => {
117+
resolve(localOptions instanceof Object ? localOptions : {})
118+
})
119+
})
120+
}
115121
export default {
116-
isBroswer,
122+
isBrowser,
117123
isExtension,
118124
getExt,
119125
query,
@@ -127,5 +133,6 @@ export default {
127133
formatDate,
128134
insertAfter,
129135
getUrl,
130-
exec
136+
exec,
137+
getLocalOptions
131138
}

0 commit comments

Comments
 (0)