Skip to content

Commit

Permalink
增加选项页
Browse files Browse the repository at this point in the history
  • Loading branch information
kscript committed Mar 10, 2024
1 parent 839b80a commit 9bef1dc
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 75 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-downloader",
"version": "1.0.6",
"version": "1.0.7",
"description": "markdown文章下载",
"main": "dist/index.js",
"scripts": {
Expand Down
54 changes: 27 additions & 27 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ const sendCallback = (sendResponse, responseHeaders) => {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const { type, responseHeaders } = message
const { url, data, dataType, callback } = message
const { fileName, files, options } = message
const { fileName, files, options = {} } = message
const { sendSuccess, sendError } = sendCallback(sendResponse, responseHeaders)
if (/(get|post)/i.test(type)) {
ajax({
url,
method: type,
data,
dataType,
success (data, xhr) {
if (/text|blob/i.test(dataType)) {
sendSuccess(data, xhr)
} else {
const obj = dataType === 'text' ? data : JSON.parse(data)
const result = /^json$/i.test(dataType) ? {
callback: noop(callback),
data: obj
} : data
if (typeof callback === 'string' && callback) {
sendSuccess(result, xhr)
if (/(get|post)/i.test(type)) {
ajax({
url,
method: type,
data,
dataType,
success (data, xhr) {
if (/text|blob/i.test(dataType)) {
sendSuccess(data, xhr)
} else {
sendSuccess(obj, xhr)
const obj = dataType === 'text' ? data : JSON.parse(data)
const result = /^json$/i.test(dataType) ? {
callback: noop(callback),
data: obj
} : data
if (typeof callback === 'string' && callback) {
sendSuccess(result, xhr)
} else {
sendSuccess(obj, xhr)
}
}
},
error (error, xhr) {
sendError(error, xhr)
}
},
error (error, xhr) {
sendError(error, xhr)
}
})
} else if (type === 'download') {
downloadZip(fileName, files, options)
}
})
} else if (type === 'download') {
downloadZip(fileName, files, options)
}
return true
})

Expand Down
15 changes: 9 additions & 6 deletions src/download.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import md5 from 'md5'
import JSZip from 'jszip'
import FileSaver from 'jszip/vendor/FileSaver'
import { getLocalOptions } from './utils'

const defaultOptions = {
partLimit: 1e3,
Expand All @@ -13,14 +14,16 @@ const options = Object.assign({}, defaultOptions)
export const mergeOptions = (newOptions) => {
return Object.assign(options, defaultOptions, newOptions instanceof Object ? newOptions : {})
}

export const mergeLocalOptions = async () => {
return Object.assign(mergeOptions(await getLocalOptions()))
}

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

export const ajax = (options) => {
options = Object.assign({}, defaultOptions, options)
export const ajax = async (options) => {
const config = await mergeLocalOptions()
const core = (retry = 3) => {
const xhr = new XMLHttpRequest()
options.method = options.method || 'get'
Expand Down Expand Up @@ -53,7 +56,7 @@ export const ajax = (options) => {
xhr.send()
}
}
core(options.retry)
core(config.retry)
}

export const fetchBlobFile = (file) =>{
Expand Down Expand Up @@ -126,9 +129,9 @@ export const partDownload = (fileName, files, { partLimit } = options) => {
}, partLimit)
}

export const downloadZip = (fileName, files, options = {}) => {
export const downloadZip = async (fileName, files, options = {}) => {
fileName = fileName || md5(files.map(item => item.downloadUrl).join('|'))
return partDownload(fileName, files, mergeOptions(options))
return partDownload(fileName, files, await mergeLocalOptions(options))
}

export default downloadZip
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { websites } from './websites'
import {
isExtension,
sendMessage,
getLocalOptions
} from './utils'
import { downloadMarkdown } from './markdown'

const extract = async (options, customOptions, hook) => {
const data = await downloadMarkdown(options, customOptions, hook)
const localOptions = await getLocalOptions()
const data = await downloadMarkdown(options, Object.assign(customOptions, {
localOptions
}), hook)
data && sendMessage(data)
return data
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.092cdea9.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/main.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/main.d77d57b4.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"browser_action": {
"default_icon": "icon.png"
},
"options_page": "option.html",
"content_scripts": [
{
"matches": [
Expand Down
64 changes: 29 additions & 35 deletions src/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ import md5 from 'md5'
import html2markdown from 'html-to-md'
import 'mathjax/es5/tex-svg'
import { query, getExt, getText, getUrl, queryAll, insertAfter, getAttribute, formatDate, exec } from './utils'

const setInfo = (data) => {
const replace = (str, fn) => {
fn = typeof fn === 'function' ? fn : (s) => s
return str.replace(/\$\{(.*?)\}/g, (s, s1) => fn(s1.replace(/(^\s+|\s+$)/g, '')))
}
const setInfo = (data, tpl) => {
data = Object.assign({
date: formatDate('yyyy-MM-dd HH:mm:ss'),
now: formatDate('yyyy-MM-dd HH:mm:ss'),
copyright: false,
url: location.href,
description: '转载',
tag: []
}, data instanceof Object ? data : {})
return `---
title: {{title}}
date: {{date}}
copyright: {{copyright}}
author: {{author}}
home: {{home}}
origin: {{origin}}
url: {{url}}
tag: ${ data.tag && data.tag.length ? '\n - ' + data.tag.join('\n - ') : '' }
categories: {{categories}}
description: {{description}}
tpl = typeof tpl === 'string' ? tpl : `---
title: \${title}
date: \${now}
copyright: \${copyright}
author: \${author}
home: \${home}
origin: \${origin}
url: \${url}
tag: \${tag}
categories: \${categories}
description: \${description}
---
`.replace(/\n\s+/g, '\n').replace(/\{\{(.*?)\}\}/g, (s, s1) => data[s1] === void 0 ? '' : data[s1])
`
return replace(tpl.replace(/\n\s+/g, '\n'), (s1) => data[s1] === void 0 ? '' : Array.isArray(data[s1]) ? '\n - ' + data[s1].join('\n - ') : data[s1])
}
const formatCopyRight = (fileName) => {
return `> 当前文档由 [markdown文档下载插件](https://github.com/kscript/markdown-download) 下载, 原文链接: [${fileName}](${location.href}) `
const formatCopyRight = (data, { retain, copyright }) => {
const tpl = retain ? '> 当前文档由 [markdown文档下载插件](https://github.com/kscript/markdown-download) 下载, 原文链接: [${title}](${url}) ' : copyright
return typeof tpl === 'string' ? '\n\n' + replace(tpl, (s1) => data[s1] || '') + '\n\n' : ''
}
const getMarkdown = (markdownBody) => {
return markdownBody.innerHTML
Expand All @@ -49,23 +54,12 @@ export const tex2svg = (markdownDoc) => {

const formatParams = (options, customOptions, hook) => {
const defaultOptions = {
origin: 'juejin',
// 处理链接
link: true,
// 处理换行
br: false,
// 处理代码块
code: false,
link: true,
lazyKey: 'data-src',
selectors: {
title: '.article-title',
body: '.markdown-body',
copyBtn: '.copy-code-btn',
userName: '.username .name',
userLink: '.username',
invalid: 'style',
unpack: ''
}
origin: '',
selectors: {}
}
customOptions = customOptions instanceof Object ? customOptions : {}
options = merge({}, defaultOptions, options instanceof Object ? options : {}, customOptions)
Expand Down Expand Up @@ -124,7 +118,7 @@ export const formatMarkdownBody = (container, selectors, options, exec) => {
}

const extract = async (markdownBody, selectors, options, exec) => {
const { origin, context } = options
const { origin, context, localOptions = {} } = options
const fileName = getText(selectors.title) || document.title
const realName = fileName.replace(/[\\\/\?<>:'\*\|]/g, '_')
const files = queryAll('img', markdownBody).map(item => {
Expand Down Expand Up @@ -154,13 +148,13 @@ const extract = async (markdownBody, selectors, options, exec) => {
home: getUrl(location.origin, getAttribute('href', selectors.userLink)),
tag: context.tag,
description: markdownBody.innerText.replace(/^([\n\s]+)/g, '').replace(/\n/g, ' ').slice(0, 50) + '...',
})
}, localOptions.tpl)
const markdownDoc = html2markdown(info + getMarkdown(markdownBody), {})
const copyright = formatCopyRight(fileName)
const copyright = formatCopyRight({ title: fileName, url: location.href }, localOptions)
const content = await exec('formatContent', { markdownBody, markdownDoc })
files.push({
name: realName + '.md',
content: (content && typeof content === 'string' ? content : markdownDoc) + '\n\n' + copyright
content: `${content && typeof content === 'string' ? content : markdownDoc}${copyright}`
})
return {
fileName,
Expand Down
19 changes: 19 additions & 0 deletions src/option.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="zh-hans">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="插件配置" />
<title>插件配置</title>
<script defer="defer" src="main.d77d57b4.js"></script>
<link href="main.092cdea9.css" rel="stylesheet">
<link href="main.css" rel="stylesheet">
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>

</html>
17 changes: 12 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path-browserify'

export const isBroswer = typeof window !== 'undefined' && window instanceof Object
export const isExtension = isBroswer && window.chrome instanceof Object && window.chrome.runtime
export const isBrowser = typeof window !== 'undefined' && window instanceof Object
export const isExtension = isBrowser && window.chrome instanceof Object && window.chrome.runtime
export const getExt = (fileName) => {
return path.parse(fileName).ext.slice(1)
}
Expand Down Expand Up @@ -111,9 +111,15 @@ export const exec = async (...rest) => {
console.warn(err)
}
}

export const getLocalOptions = () => {
return new Promise((resolve) => {
chrome.storage.local.get('localOptions', ({ localOptions }) => {
resolve(localOptions instanceof Object ? localOptions : {})
})
})
}
export default {
isBroswer,
isBrowser,
isExtension,
getExt,
query,
Expand All @@ -127,5 +133,6 @@ export default {
formatDate,
insertAfter,
getUrl,
exec
exec,
getLocalOptions
}

0 comments on commit 9bef1dc

Please sign in to comment.