Skip to content

Commit 721f752

Browse files
committed
改造成npm模块
1 parent d9aa1b8 commit 721f752

20 files changed

+1404
-1390
lines changed

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.github
2+
/node_modules
3+
/src
4+
webpack.config.js

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# markdown-download
22
markdown文档下载 chrome插件
33

4-
## 使用方法
4+
## 作为插件使用
55
1. 在命令行中执行拉取/安装/打包
66
``` cmd
77
git clone https://github.com/kscript/download-markdown
@@ -14,6 +14,32 @@ npm run build
1414
3. 选择`加载已解压的扩展程序`, 选中项目打包后的dist文件夹
1515
4. 访问已支持的网站的文章详情页面, 等待文章内图片全部加载后, 点击插件图标
1616

17+
## 作为模块使用
18+
### 安装
19+
```
20+
npm i markdown-downloader
21+
```
22+
### 使用
23+
> 由于会操作dom元素, 所以运行时需要在浏览器环境下
24+
``` js
25+
// 方式1. 导入模块
26+
import markdownDownload, { convert, download, websiteConfigs } from 'markdown-downloader'
27+
markdownDownload(websiteConfigs.juejin, {
28+
// 包含所有信息的innerHTML文本
29+
context: ``
30+
})
31+
```
32+
```html
33+
<!-- 方式2. 直接使用脚本文件 -->
34+
<script src="./markdownDownload.js"></script>
35+
<script>
36+
// 给window对象添加一个markdownDownload函数, convert, download, websiteConfigs作为其属性
37+
markdownDownload(markdownDownload.websiteConfigs.juejin, {
38+
// 包含所有信息的innerHTML文本
39+
context: ``
40+
})
41+
</script>
42+
```
1743

1844
## 已支持的网站
1945
[掘金](https://juejin.cn/)

build/copy.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/zip.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

package-lock.json

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

package.json

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
{
2-
"name": "markdown-download",
2+
"name": "markdown-downloader",
33
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
4+
"description": "markdown文章下载",
5+
"main": "dist/index.js",
66
"scripts": {
77
"build": "npx webpack",
8-
"release": "npm run build && node build/copy.js",
9-
"zip": "node build/zip.js",
8+
"release": "cross-env BUILD_MODE=npm npm run build",
109
"test": "echo \"Error: no test specified\" && exit 1"
1110
},
1211
"repository": {
1312
"type": "git",
14-
"url": "git+https://kscript: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@github.com/kscript/markdown-download.git"
13+
"url": "git+https://github.com/kscript/markdown-download.git"
1514
},
16-
"keywords": [],
17-
"author": "",
15+
"keywords": [
16+
"markdown",
17+
"download",
18+
"chrome",
19+
"extension",
20+
"hexo",
21+
"html2markdown"
22+
],
23+
"author": "luren",
1824
"license": "MIT",
1925
"bugs": {
2026
"url": "https://github.com/kscript/markdown-download/issues"
@@ -33,15 +39,13 @@
3339
"babel-runtime": "^6.26.0",
3440
"copy-webpack-plugin": "^9.0.1",
3541
"core-js": "^2.6.12",
42+
"cross-env": "^7.0.3",
3643
"html-to-md": "^0.5.3",
3744
"jszip": "^3.7.1",
3845
"md5": "^2.3.0",
46+
"path-browserify": "^1.0.1",
3947
"webpack": "^5.63.0",
40-
"webpack-cli": "^4.9.1"
41-
},
42-
"dependencies": {
43-
"fs-extra": "^10.0.0",
44-
"path": "^0.12.7",
45-
"path-browserify": "^1.0.1"
48+
"webpack-cli": "^4.9.1",
49+
"webpack-merge": "^5.8.0"
4650
}
4751
}

src/background.js

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,5 @@
1-
import md5 from 'md5'
2-
import JSZip from 'jszip'
3-
import FileSaver from 'jszip/vendor/FileSaver'
41
import { configs } from './websites'
5-
const noop = (func, defaultFunc) => {
6-
return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : () => {}
7-
}
8-
9-
const ajax = (options) => {
10-
var xhr = new XMLHttpRequest()
11-
options.method = options.method || 'get'
12-
xhr.responseType = options.dataType || 'json';
13-
xhr.onreadystatechange = () => {
14-
if (xhr.readyState == 4) {
15-
try {
16-
noop(options.success)(xhr.response, xhr)
17-
} catch (err) {
18-
noop(options.error)(err, xhr)
19-
}
20-
}
21-
}
22-
xhr.error = (err) => {
23-
console.log(err)
24-
noop(options.error)(err, xhr)
25-
}
26-
if (/post/i.test(options.method)) {
27-
xhr.open(options.method, options.url, options.async !== false)
28-
xhr.setRequestHeader('Content-type', /json/i.test(options.dataType) ? 'application/json' : 'application/x-www-form-urlencoded')
29-
xhr.send(options.data)
30-
} else {
31-
xhr.open(options.method, options.url, options.async !== false)
32-
xhr.send()
33-
}
34-
}
35-
36-
const downloadZip = (files, fileName) => {
37-
fileName = fileName || md5(files.map(item => item.downloadUrl).join('|'))
38-
const zip = new JSZip()
39-
const fetchFile = (file) =>{
40-
return new Promise((resolve, reject) => {
41-
if (file.content) {
42-
const blob = new Blob([file.content], {type : file.type || 'text/plain'})
43-
zip.file(file.name, blob)
44-
resolve(blob)
45-
} else {
46-
ajax({
47-
url: file.downloadUrl,
48-
type: 'get',
49-
data: '',
50-
dataType: 'blob',
51-
success: (blob) => {
52-
zip.file(file.name, blob);
53-
resolve(blob)
54-
},
55-
error: reject
56-
})
57-
}
58-
})
59-
}
60-
return Promise.all(
61-
files.map(file => fetchFile(file))
62-
).then((datas) => {
63-
zip.generateAsync({
64-
type: "blob"
65-
}).then(function(content) {
66-
FileSaver(content, fileName + '.zip');
67-
})
68-
})
69-
}
2+
import downloadZip from './download'
703

714
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
725
const getHeaders = (xhr) => {
@@ -111,7 +44,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
11144
}
11245
})
11346
} else if (message.type === 'download') {
114-
downloadZip(message.files, message.fileName)
47+
downloadZip(message.fileName, message.files)
11548
}
11649
return true
11750
})

src/broswer.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import convert from './'
2+
import download from './download'
3+
import { configs as websiteConfigs } from './websites'
4+
5+
export { convert, download, websiteConfigs }
6+
7+
export const markdownDownload = (options, customOptions) => {
8+
const {fileName, files} = convert(options, customOptions)
9+
download(fileName, files)
10+
}
11+
12+
if (typeof window !== 'undefined') {
13+
markdownDownload.websiteConfigs = websiteConfigs
14+
markdownDownload.convert = convert
15+
markdownDownload.download = download
16+
window.markdownDownload = markdownDownload
17+
}
18+
19+
export default markdownDownload

src/download.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import md5 from 'md5'
2+
import JSZip from 'jszip'
3+
import FileSaver from 'jszip/vendor/FileSaver'
4+
const noop = (func, defaultFunc) => {
5+
return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : () => {}
6+
}
7+
8+
const ajax = (options) => {
9+
var xhr = new XMLHttpRequest()
10+
options.method = options.method || 'get'
11+
xhr.responseType = options.dataType || 'json';
12+
xhr.onreadystatechange = () => {
13+
if (xhr.readyState == 4) {
14+
try {
15+
noop(options.success)(xhr.response, xhr)
16+
} catch (err) {
17+
noop(options.error)(err, xhr)
18+
}
19+
}
20+
}
21+
xhr.error = (err) => {
22+
console.log(err)
23+
noop(options.error)(err, xhr)
24+
}
25+
if (/post/i.test(options.method)) {
26+
xhr.open(options.method, options.url, options.async !== false)
27+
xhr.setRequestHeader('Content-type', /json/i.test(options.dataType) ? 'application/json' : 'application/x-www-form-urlencoded')
28+
xhr.send(options.data)
29+
} else {
30+
xhr.open(options.method, options.url, options.async !== false)
31+
xhr.send()
32+
}
33+
}
34+
35+
export const downloadZip = (fileName, files) => {
36+
fileName = fileName || md5(files.map(item => item.downloadUrl).join('|'))
37+
const zip = new JSZip()
38+
const fetchFile = (file) =>{
39+
return new Promise((resolve, reject) => {
40+
if (file.content) {
41+
const blob = new Blob([file.content], {type : file.type || 'text/plain'})
42+
zip.file(file.name, blob)
43+
resolve(blob)
44+
} else {
45+
ajax({
46+
url: file.downloadUrl,
47+
type: 'get',
48+
data: '',
49+
dataType: 'blob',
50+
success: (blob) => {
51+
zip.file(file.name, blob);
52+
resolve(blob)
53+
},
54+
error: reject
55+
})
56+
}
57+
})
58+
}
59+
return Promise.all(
60+
files.map(file => fetchFile(file))
61+
).then((datas) => {
62+
zip.generateAsync({
63+
type: "blob"
64+
}).then(function(content) {
65+
FileSaver(content, fileName + '.zip');
66+
})
67+
})
68+
}
69+
70+
export default downloadZip

0 commit comments

Comments
 (0)