// ==UserScript== // @name 网页正文转 Markdown|一键复制 / 下载干净的 Markdown // @name:zh-CN 网页正文转 Markdown|一键复制 / 下载干净的 Markdown // @name:en Page to Markdown | Copy or download the article as clean Markdown // @namespace https://github.com/yilecoding/helper-scripts // @version 2025.04.30.10 // @description 把文章页的正文一键转成干净的 Markdown,复制到剪贴板或下载为 .md 文件。 // @description:zh-CN 把文章页的正文一键转成干净的 Markdown,复制到剪贴板或下载为 .md 文件。 // @description:en Turn the main article of any web page into clean Markdown — copy it to the clipboard or download it as a .md file. // @author 壹樂(V:ita1125) // @match *://*/* // @grant GM_registerMenuCommand // @grant GM_setClipboard // @grant GM_download // @license MIT // @run-at document-idle // @noframes // @homepageURL https://github.com/yile2020/helper-scripts // @supportURL https://github.com/yile2020/helper-scripts/issues // @updateURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/page-to-markdown/page-to-markdown.user.js // @downloadURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/page-to-markdown/page-to-markdown.user.js // ==/UserScript== // 脚本持续更新维护,欢迎 star / watch 仓库获取最新版本: // https://github.com/yile2020/helper-scripts (function () { 'use strict'; const TAG = '[正文转MD]'; // ========================================================================= // 配置 // ========================================================================= const CONFIG = { bullet: '-', // 无序列表标记 fence: '```', // 代码块围栏 toastMs: 2500, // 提示条停留时间 frontMatter: true, // 顶部加 YAML front-matter(title / url / author / date / captured) titleHeading: true, // 正文里没有 h1 时在开头补一个 `# 标题` maxNameLen: 80, // 下载文件名最长字符数(不含 .md) }; // ========================================================================= // HTML → Markdown // ========================================================================= const BLOCK_TAGS = new Set([ 'ADDRESS', 'ARTICLE', 'ASIDE', 'BLOCKQUOTE', 'BODY', 'CENTER', 'DD', 'DETAILS', 'DIALOG', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE', 'FOOTER', 'FORM', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEADER', 'HGROUP', 'HR', 'LI', 'MAIN', 'MENU', 'NAV', 'OL', 'P', 'PRE', 'SECTION', 'SUMMARY', 'TABLE', 'TBODY', 'TD', 'TFOOT', 'TH', 'THEAD', 'TR', 'UL', ]); // data-p2md-block:标签是行内的,但页面 CSS 把它显示成块(见 cloneForConvert) const BLOCK_SELECTOR = [...BLOCK_TAGS].join(',').toLowerCase() + ',[data-p2md-block]'; const SKIP_TAGS = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'HEAD', 'META', 'LINK']); /** 硬换行先用占位符,等整段拼完再决定是 `\` 换行还是分段(见 finishInline)。 */ const BR = '\uE001'; function isBlock(el) { return BLOCK_TAGS.has(el.tagName) || el.hasAttribute('data-p2md-block') || !!el.querySelector(BLOCK_SELECTOR); } const isSpace = c => c !== undefined && /\s/.test(c); const isWord = c => c !== undefined && /[\p{L}\p{N}]/u.test(c); /* * 正文里的转义要克制:不转 `.`、不转单词中间的 `_`(snake_case),只转真会被误解析的。 * * ~ 两边都是空白才安全(`a * b`);`2*3*4`、`3~5` 里的会被当成强调 / 删除线 * _ 两边都是字母数字(单词中间)或都是空白时安全 * ] 后面跟 ( [ : 才可能拼出链接 / 引用定义,转它一个就够;链接文字里 [ ] 都转 * < 后面是字母、/、!、? 时可能被当成 HTML 标签 * \ 后面是 ASCII 标点或在末尾(会吃掉紧跟的 ** 闭合符) * 行首的 # - + 1. > 在 escapeLineStart 里处理。 */ function escapeText(s, ctx = {}) { s = s.replace(/[\uE000-\uE00F]/g, ''); // 本脚本内部用的占位符 return s.replace(/[\\`*_~[\]<&]/g, (c, i) => { const a = s[i - 1]; const b = s[i + 1]; switch (c) { case '\\': return /[!-/:-@[-`{-~]/.test(b || '!') ? '\\\\' : c; case '`': return '\\`'; case '*': case '~': return isSpace(a) && isSpace(b) ? c : '\\' + c; case '_': return (isWord(a) && isWord(b)) || (isSpace(a) && isSpace(b)) ? c : '\\_'; case '[': return ctx.inLink ? '\\[' : c; case ']': return ctx.inLink || /[([:]/.test(b || '') ? '\\]' : c; case '<': return /[A-Za-z/!?]/.test(b || '') ? '\\<' : c; case '&': return /^&#?\w+;/.test(s.slice(i)) ? '\\&' : c; } return c; }); } function textOf(node, ctx) { return escapeText(node.data.replace(/\s+/g, ' '), ctx); } /* * 强调符号先输出成占位符,整篇拼完再在 resolveEmphasis 里定成 ** 还是 。 * 原因是 CommonMark 的定界规则:内容以标点结尾时,闭合的 ** 后面必须是空白或标点才算数。 * `**注意:**这里` 在中文里极常见,但不会渲染成加粗 —— 这要看到外面紧挨着的字才能判断, * 而转换单个元素时还不知道后面跟的是什么。 */ const EM = { s: ['**', 'strong'], e: ['*', 'em'], d: ['~~', 'del'] }; /** 首尾空白挪到标记外面:` 粗 ` → ` **粗** `,否则 `** 粗 **` 不生效。 */ function wrap(s, kind) { const m = s.match(/^(\s*)([\s\S]*?)(\s*)$/); if (!m[2]) return s; return `${m[1]}\uE002${kind}\uE002${m[2]}\uE003${kind}\uE003${m[3]}`; } function resolveEmphasis(md) { const edge = c => c === undefined || /[\s\p{P}\p{S}\uE002\uE003]/u.test(c); const punct = c => c === undefined || /[\p{P}\p{S}\uE002\uE003]/u.test(c); const re = /\uE002([sed])\uE002([\s\S]*?)\uE003\1\uE003/g; // 外层先定、内层下一轮;内层的占位符在外层看来就是标点(最终也会变成 * 或 ) for (let guard = 0; re.test(md) && guard < 20; guard++) { re.lastIndex = 0; md = md.replace(re, (all, kind, inner, off, str) => { const [delim, tag] = EM[kind]; const before = str[off - 1]; const after = str[off + all.length]; const html = (punct(inner[0]) && !edge(before)) || (punct(inner[inner.length - 1]) && !edge(after)); return html ? `<${tag}>${inner}` : delim + inner + delim; }); } return md; } function absUrl(u) { if (!u) return ''; try { return new URL(u, document.baseURI).href; } catch (e) { return u; } } function renderInline(el, ctx) { const tag = el.tagName; if (SKIP_TAGS.has(tag)) return ''; switch (tag) { case 'BR': return BR; case 'STRONG': case 'B': return wrap(inlineChildren(el, ctx), 's'); case 'EM': case 'I': return wrap(inlineChildren(el, ctx), 'e'); case 'CODE': case 'KBD': case 'SAMP': case 'TT': return codeSpan(el.textContent); case 'DEL': case 'S': case 'STRIKE': return wrap(inlineChildren(el, ctx), 'd'); case 'SUP': case 'SUB': { const t = inlineChildren(el, ctx).trim(); return t ? `<${tag.toLowerCase()}>${t}` : ''; } case 'IMG': return renderImage(el); case 'A': return renderLink(el, ctx); default: { const inner = inlineChildren(el, ctx); // 块元素被压进行内(链接卡片里的

)时,前后补空格,别把字粘在一起 return BLOCK_TAGS.has(tag) ? ' ' + inner + ' ' : inner; } } } /** 括号和空格编码掉:维基百科那种 `Foo_(bar)` 的地址放进 `(...)` 里会提前截断。 */ function fmtUrl(u) { return u.replace(/[()\s<>]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')); } function fmtTitle(t) { t = (t || '').replace(/\s+/g, ' ').trim(); return t ? ` "${t.replace(/["\\]/g, '\\$&')}"` : ''; } function renderLink(el, ctx) { const text = inlineChildren(el, { ...ctx, inLink: true }).trim(); const href = el.getAttribute('href'); if (!href || /^(javascript|data|vbscript):/i.test(href.trim())) return text; if (!text) return ''; const url = absUrl(href); if (normText(el) === url && /^https?:/i.test(url)) return `<${url}>`; return `[${text}](${fmtUrl(url)}${fmtTitle(el.getAttribute('title'))})`; } /* * 懒加载图片:真地址在 data-src 一类属性里,src 是 1×1 占位图(往往是 data: URI)。 * 所以先看 data-*,再看 srcset(取最大的那张),最后才是 src。 */ const LAZY_ATTRS = ['data-src', 'data-original', 'data-lazy-src', 'data-actualsrc', 'data-original-src', 'data-url', 'data-lazyload']; function bestFromSrcset(srcset) { let best = null; let bestW = -1; // URL 里可能有逗号(cloudinary 的 w_100,h_100),不能按裸逗号切: // URL 贪婪吃到空白为止,只有它后面(或描述符后面)的逗号才是分隔符 const re = /\s*([^\s,]\S*)(?:\s+([\d.]+)([wx]))?\s*(?:,|$)/g; let m; while ((m = re.exec(srcset)) && m[0]) { const w = m[2] ? parseFloat(m[2]) * (m[3] === 'x' ? 1000 : 1) : 0; if (w > bestW) { best = m[1]; bestW = w; } } return best; } function imageSrc(img) { for (const a of LAZY_ATTRS) { const v = img.getAttribute(a); if (v && !/^data:/i.test(v.trim())) return absUrl(v.trim()); } const srcset = img.getAttribute('data-srcset') || img.getAttribute('srcset'); if (srcset) { const best = bestFromSrcset(srcset); if (best) return absUrl(best); } const src = (img.getAttribute('src') || '').trim(); if (!src || (/^data:/i.test(src) && src.length < 200)) return ''; // 占位小图 return /^data:/i.test(src) ? src : absUrl(src); } function renderImage(img) { const src = imageSrc(img); if (!src) return ''; const alt = (img.getAttribute('alt') || '').replace(/\s+/g, ' ').trim().replace(/[[\]\\]/g, '\\$&'); return `![${alt}](${fmtUrl(src)}${fmtTitle(img.getAttribute('title'))})`; } function inlineChildren(el, ctx) { let out = ''; for (const n of el.childNodes) { if (n.nodeType === 3) out += textOf(n, ctx); else if (n.nodeType === 1) out += renderInline(n, ctx); } return out; } /** 一段行内内容收尾:合并空白、处理换行占位符。 */ function finishInline(s, ctx = {}) { s = s.replace(/ {2,}/g, ' ') .replace(/ *\uE001 */g, BR) .replace(/^\uE001+|\uE001+$/g, ''); // 连续两个以上
当分段用(不少中文站用

代替

) s = s.replace(/\uE001{2,}/g, '\n\n').replace(/\uE001/g, '\\\n'); s = s.trim(); // 表格单元格里不会有块语法,单独一个 `-` 是常见的「无」 return ctx.inTable ? s : s.split('\n').map(escapeLineStart).join('\n'); } /* * 行首才有意义的块语法。只在一段行内内容拼完以后按行处理 —— 单个文本节点不知道自己是不是在行首, * 在文本节点里转会把句中的「1. 」「- 」也转掉。 */ function escapeLineStart(line) { return line .replace(/^(#{1,6})(?=\s|$)/, '\\$1') .replace(/^([-+*])(?=\s)/, '\\$1') .replace(/^(\d{1,9})([.)])(?=\s|$)/, '$1\\$2') .replace(/^>/, '\\>') .replace(/^(=+|-+)\s*$/, '\\$1'); } /** 渲染子节点为若干块,连续的行内节点拼成一段。 */ function renderChildren(parent, ctx) { const out = []; let buf = ''; const flush = () => { const t = finishInline(buf, ctx); if (t) out.push(t); buf = ''; }; // 两个同类列表挨着时,CommonMark 会把它们并成一个(空行也拦不住), // 后一个换标记(- ↔ *,1. ↔ 1))才能断开 let prevList = null; let alt = false; for (const n of parent.childNodes) { if (n.nodeType === 3) { buf += textOf(n, ctx); if (n.data.trim()) prevList = null; } else if (n.nodeType === 1 && !SKIP_TAGS.has(n.tagName)) { if (isBlock(n)) { flush(); const isList = n.tagName === 'UL' || n.tagName === 'OL'; alt = isList && prevList === n.tagName ? !alt : false; const b = renderBlock(n, isList ? { ...ctx, altList: alt } : ctx); if (b) { out.push(b); prevList = isList ? n.tagName : null; } } else { prevList = null; buf += renderInline(n, ctx); } } } flush(); return out; } function renderBlock(el, ctx) { const tag = el.tagName; if (/^H[1-6]$/.test(tag)) { const text = finishInline(inlineChildren(el, ctx)).replace(/\\?\n+/g, ' '); return text ? '#'.repeat(+tag[1]) + ' ' + text : ''; } switch (tag) { case 'HR': return '---'; case 'UL': case 'OL': return renderList(el, ctx); case 'LI': return renderListItem(el, CONFIG.bullet, ctx); // 选区里截出来的孤儿 li case 'PRE': return renderCodeBlock(el); case 'TABLE': return renderTable(el, ctx); case 'A': return finishInline(renderLink(el, ctx)); // 包着块元素的链接(卡片)压成一行 case 'BLOCKQUOTE': { const inner = renderChildren(el, ctx).join('\n\n'); return inner ? inner.split('\n').map(l => (l ? '> ' + l : '>')).join('\n') : ''; } } // 被 CSS 显示成块的行内元素(公众号里 display:block 的 小标题、块级 ): // 按行内渲染保住加粗 / 图片,再单独成段 if (!BLOCK_TAGS.has(tag) && !el.querySelector(BLOCK_SELECTOR)) return finishInline(renderInline(el, ctx), ctx); return renderChildren(el, ctx).join('\n\n'); } /** 内容里有反引号就用更长的围栏,且贴边的反引号要用空格隔开。 */ function codeSpan(text) { text = text.replace(/\s+/g, ' '); if (!text.trim()) return text; const longest = Math.max(0, ...(text.match(/`+/g) || []).map(r => r.length)); const fence = '`'.repeat(longest + 1); const pad = /^`|`$/.test(text) ? ' ' : ''; return fence + pad + text + pad + fence; } /* * 行号。各家高亮器把行号塞进

 的方式都不一样,textContent 会把它们和代码拼在一起:
     *   Prism line-numbers    里多一个 span.line-numbers-rows(空 span,但删掉更稳)
     *   highlightjs-line-numbers  table.hljs-ln,每行 td.hljs-ln-numbers + td.hljs-ln-code
     *   CSDN                 
 里多一个 ul.pre-numbering
     *   Hexo / Pygments      外层 table,td.gutter / td.linenos 是一整个装行号的 
     */
    const GUTTER_SELECTOR = [
        '.line-numbers-rows', '.hljs-ln-numbers', '.pre-numbering', 'td.gutter', 'td.linenos',
        '.linenodiv', 'span.linenos', 'span.lineno', '.line-number', '.code-line-number',
    ].join(',');

    /** 
 的文本:
和逐行的块元素(div/p/li/tr)都要变成换行,只取 textContent 会把行粘在一起。 */ function preText(pre) { let out = ''; const walk = node => { for (const c of node.childNodes) { if (c.nodeType === 3) { out += c.data; } else if (c.nodeType === 1) { if (c.tagName === 'BR') { out += '\n'; continue; } if (c.matches(GUTTER_SELECTOR) || SKIP_TAGS.has(c.tagName)) continue; walk(c); if (/^(DIV|P|LI|TR)$/.test(c.tagName) && out && !out.endsWith('\n')) out += '\n'; } } }; walk(pre); return out.replace(/\u00a0/g, ' ').replace(/^\n+|\s+$/g, ''); } const LANG_STOP = new Set([ 'hljs', 'highlight', 'highlighter-rouge', 'line-numbers', 'linenums', 'prettyprint', 'notranslate', 'code', 'source', 'sourcecode', 'codehilite', 'wrap', 'nowrap', 'copyable', 'block', 'pre', 'default', ]); const LANG_NONE = new Set(['none', 'nohighlight', 'plaintext', 'plain', 'text', 'txt']); function langFromEl(el) { const attr = el.getAttribute('data-lang') || el.getAttribute('data-language'); if (attr) return attr; const cls = typeof el.className === 'string' ? el.className : ''; if (!cls) return ''; const m = cls.match(/(?:^|\s)(?:language|lang|highlight-source)-([\w+#.-]+)/) || cls.match(/brush:\s*([\w+#-]+)/); if (m) return m[1]; // `hljs python`、`highlight js`、`sourceCode python`:标记类旁边那个就是语言 if (/(^|\s)(hljs|highlight|sourceCode)(\s|$)/.test(cls)) { const tok = cls.split(/\s+/).find(t => t && !LANG_STOP.has(t.toLowerCase()) && !/^hljs-/.test(t) && /^[a-z][\w+#.-]{0,19}$/i.test(t)); if (tok) return tok; } return ''; } function detectLang(pre) { const cands = [pre.querySelector('code'), pre]; // 往上找几层:Hexo 的语言在 figure.highlight 上,中间隔着 table/tbody/tr/td for (let p = pre.parentElement, i = 0; p && i < 5; p = p.parentElement, i++) cands.push(p); for (const el of cands) { if (!el) continue; const lang = langFromEl(el).toLowerCase(); if (lang) return LANG_NONE.has(lang) ? '' : lang; } return ''; } function renderCodeBlock(pre) { const code = preText(pre); if (!code.trim()) return ''; const longest = Math.max(0, ...(code.match(/`{3,}/g) || []).map(r => r.length)); const fence = longest >= CONFIG.fence.length ? '`'.repeat(longest + 1) : CONFIG.fence; return `${fence}${detectLang(pre)}\n${code}\n${fence}`; } /* * GFM 表格。单元格里的 `|` 必须转义成 `\|` —— 连行内代码里的也要,因为 GFM 是先按 `|` 切列 * 再解析行内语法的;换行只能写成
,否则整行表格断掉。 * 带
、嵌套表格或只有一列的表格多半是排版用的(Hexo 代码块、老站布局),直接展开内容。
     */
    function renderTable(table, ctx) {
        const rows = [...table.rows];
        if (!rows.length) return '';
        const isLayout = table.querySelector('pre, table') || rows.every(r => r.cells.length <= 1);
        if (isLayout) return renderChildren(table, ctx).join('\n\n');

        const grid = [];
        const align = [];
        for (const tr of rows) {
            const cells = [];
            for (const td of tr.cells) {
                if (grid.length === 0) align[cells.length] = cellAlign(td);
                cells.push(cellText(td, ctx));
                const span = Math.min(parseInt(td.getAttribute('colspan'), 10) || 1, 50);
                for (let i = 1; i < span; i++) cells.push('');
            }
            grid.push(cells);
        }
        const cols = Math.max(...grid.map(r => r.length));
        const line = cells => '| ' + Array.from({ length: cols }, (_, i) => cells[i] || '').join(' | ') + ' |';
        const sep = Array.from({ length: cols }, (_, i) => {
            const a = align[i];
            return a === 'center' ? ':---:' : a === 'right' ? '---:' : a === 'left' ? ':---' : '---';
        });
        // 没有 thead 也只能拿第一行当表头,GFM 表格必须有表头
        return [line(grid[0]), '| ' + sep.join(' | ') + ' |', ...grid.slice(1).map(line)].join('\n');
    }

    function cellAlign(td) {
        const a = (td.getAttribute('align') || td.style.textAlign || '').toLowerCase();
        return /^(left|center|right)$/.test(a) ? a : '';
    }

    function cellText(td, ctx) {
        return renderChildren(td, { ...ctx, inTable: true }).join('\n')
            .replace(/\\\n/g, '
') .replace(/\n+/g, '
') .replace(/\|/g, '\\|') .trim(); } function indent(s, n) { const pad = ' '.repeat(n); return s.split('\n').map(l => (l ? pad + l : l)).join('\n'); } function renderList(el, ctx) { const alt = ctx.altList; ctx = { ...ctx, altList: false }; const ordered = el.tagName === 'OL'; const bullet = alt ? (CONFIG.bullet === '*' ? '-' : '*') : CONFIG.bullet; let n = ordered ? parseInt(el.getAttribute('start'), 10) : 1; if (!Number.isFinite(n)) n = 1; const items = []; let lastWidth = 2; for (const child of el.children) { if (child.tagName === 'LI') { const marker = ordered ? `${n++}${alt ? ')' : '.'}` : bullet; const item = renderListItem(child, marker, ctx); if (item) items.push(item); lastWidth = marker.length + 1; } else if (child.tagName === 'UL' || child.tagName === 'OL') { // 不合规但很常见:
  • a
    • …
,子列表直接挂在 ul 下而不是 li 里 const sub = renderList(child, ctx); if (!sub) continue; if (items.length) items[items.length - 1] += '\n' + indent(sub, lastWidth); else items.push(sub); } } return items.join('\n'); } /** 续行缩进 = 标记宽度(`- ` 2 格,`10. ` 4 格),少一格子列表就会被当成同级。 */ function renderListItem(li, marker, ctx) { let task = ''; const cb = li.querySelector('input[type="checkbox"]'); if (cb && cb.closest('li') === li) { task = cb.checked || cb.hasAttribute('checked') ? '[x] ' : '[ ] '; cb.remove(); // 操作的是克隆,不影响页面 } const blocks = renderChildren(li, ctx); // 宽松列表(li 里有

)项内各块之间空一行,紧凑列表只换行 const loose = [...li.children].some(c => c.tagName === 'P'); const body = blocks.join(loose ? '\n\n' : '\n'); const width = marker.length + 1; if (!body) return task ? `${marker} ${task.trim()}` : ''; return `${marker} ${task}` + indent(body, width).slice(width); } function htmlToMarkdown(root) { const ctx = {}; return resolveEmphasis(renderChildren(root, ctx).join('\n\n')).trim() + '\n'; } // ========================================================================= // 正文提取(Readability 风格打分) // ========================================================================= /* * 思路照 Mozilla Readability:给每个「像段落的」元素打分,分数往上传给父 / 祖父, * 得分最高的祖先就是正文容器,再把分数够高的兄弟节点一并带上。 * 区别: * - 文本长度按「CJK 字符算 2」加权。一个中文字的信息量约等于两三个英文字母, * 不加权的话中文段落普遍过不了 25 字的门槛,正文容器反而输给侧栏。 * - 逗号把中文的「,、。;」也算上。 */ const RE = { unlikely: /-ad-|banner|breadcrumb|combx|comment|community|disqus|extra|footer|gdpr|header|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|pagination|pager|popup|modal|share|recommend|subscribe|cookie|toolbar|copyright|navbar|topbar/i, maybe: /article|body|column|content|main|post|entry|story|text|blog/i, positive: /article|body|content|entry|hentry|h-entry|main|page|post|text|blog|story/i, negative: /-ad-|hidden|^hid$|banner|combx|comment|com-|contact|foot|footnote|gdpr|masthead|meta|outbrain|promo|related|scroll|share|shoutbox|sidebar|skyscraper|sponsor|shopping|tags|tool|widget|recommend/i, // 广告:要求按词边界匹配,否则 header / read / shadow 里的 "ad" 全会误伤 ad: /(^|[\s_-])(ads?|adv|advert|advertisement|adsbygoogle|sponsored|guanggao)([\s_-]|$)/i, comma: /[,,、。;]/g, cjk: /[぀-ヿ㐀-鿿豈-﫿가-힯]/g, }; const NOISE_SELECTOR = [ 'script', 'style', 'noscript', 'template', 'iframe', 'object', 'embed', 'canvas', 'svg', 'nav', 'aside', 'footer', 'form', 'button', 'select', 'textarea', 'input:not([type="checkbox"])', 'label', 'dialog', '[role="navigation"]', '[role="complementary"]', '[role="dialog"]', '[hidden]', '.sr-only', '.visually-hidden', '.screen-reader-text', 'ins.adsbygoogle', '[id^="div-gpt-ad"]', '[data-ad-slot]', '[data-ad]', ].join(','); function normText(el) { return (el.textContent || '').replace(/\s+/g, ' ').trim(); } function weightedLen(s) { return s.length + (s.match(RE.cjk) || []).length; } function linkDensity(el) { const total = weightedLen(normText(el)); if (!total) return 0; let links = 0; for (const a of el.querySelectorAll('a')) links += weightedLen(normText(a)); return Math.min(1, links / total); } function classWeight(el) { let w = 0; for (const s of [el.className, el.id]) { if (typeof s !== 'string' || !s) continue; if (RE.negative.test(s)) w -= 25; if (RE.positive.test(s)) w += 25; } if (el.tagName === 'ARTICLE' || el.tagName === 'MAIN' || el.getAttribute('itemprop') === 'articleBody') w += 25; return w; } function isUnlikely(el) { if (el.tagName === 'BODY' || el.tagName === 'ARTICLE' || el.tagName === 'MAIN') return false; if (/^(NAV|ASIDE|FOOTER|FORM|DIALOG)$/.test(el.tagName)) return true; const role = el.getAttribute('role'); if (role && /^(navigation|complementary|dialog|banner|contentinfo)$/.test(role)) return true; const s = (typeof el.className === 'string' ? el.className : '') + ' ' + el.id; return (RE.unlikely.test(s) && !RE.maybe.test(s)) || RE.ad.test(s); } /** 元素本身或任一祖先是噪声区(侧栏 / 评论 / 导航…)。带缓存,段落多时不重复爬树。 */ function makeUnlikelyChecker() { const cache = new Map(); const check = el => { if (!el || el === document.body || el === document.documentElement) return false; if (cache.has(el)) return cache.get(el); const v = isUnlikely(el) || check(el.parentElement); cache.set(el, v); return v; }; return check; } function baseScore(el) { switch (el.tagName) { case 'DIV': case 'SECTION': case 'ARTICLE': case 'MAIN': return 5; case 'PRE': case 'TD': case 'BLOCKQUOTE': return 3; case 'ADDRESS': case 'OL': case 'UL': case 'DL': case 'DD': case 'DT': case 'LI': case 'FORM': return -3; case 'H1': case 'H2': case 'H3': case 'H4': case 'H5': case 'H6': case 'TH': return -5; default: return 0; } } /** 返回正文元素数组(最高分容器 + 够格的兄弟节点),找不到时退回 article / main / body。 */ function findContent(doc = document) { const unlikely = makeUnlikelyChecker(); const scores = new Map(); const addScore = (el, v) => { if (!el || el === doc.documentElement) return; if (!scores.has(el)) scores.set(el, baseScore(el) + classWeight(el)); scores.set(el, scores.get(el) + v); }; // 「段落」:p / pre / td,外加没有块级子元素的 div(很多中文站一段一个 div) const paras = [...doc.body.querySelectorAll('p, pre, td, div, section')].filter(el => /^(P|PRE|TD)$/.test(el.tagName) || ![...el.children].some(c => BLOCK_TAGS.has(c.tagName))); for (const p of paras) { if (unlikely(p) || !p.getClientRects().length) continue; const text = normText(p); const len = weightedLen(text); if (len < 25) continue; const score = 1 + (text.match(RE.comma) || []).length + Math.min(Math.floor(len / 100), 3); let anc = p.parentElement; for (let level = 0; anc && level < 3; level++, anc = anc.parentElement) { if (anc === doc.body && level > 0) break; addScore(anc, level === 0 ? score : score / (level === 1 ? 2 : level * 3)); } } let top = null; let topScore = -Infinity; for (const [el, s] of scores) { const final = s * (1 - linkDensity(el)); scores.set(el, final); if (final > topScore) { top = el; topScore = final; } } if (!top) { const fallback = doc.querySelector('article, [itemprop="articleBody"], main') || doc.body; return [fallback]; } // 只有一个孩子的包装层往上提,保证兄弟合并时能看到真正的兄弟 while (top.parentElement && top.parentElement !== doc.body && top.parentElement.children.length === 1) { top = top.parentElement; } // 页面作者已经用唯一的

标出了正文时信它:正文短、表格多的页面, // 最高分常常落在 article 里的某个单元格上,兄弟合并又捞不回分数低的表格和列表 const art = top.closest('article, [itemprop="articleBody"]'); if (art && art !== top && doc.querySelectorAll('article').length <= 1 && !unlikely(art) && linkDensity(art) < 0.5) { top = art; } const parent = top.parentElement; if (!parent) return [top]; const threshold = Math.max(10, topScore * 0.2); const picked = []; for (const sib of parent.children) { if (sib === top) { picked.push(sib); continue; } if (unlikely(sib)) continue; if ((scores.get(sib) || 0) >= threshold) { picked.push(sib); continue; } if (sib.tagName === 'P') { const len = weightedLen(normText(sib)); const ld = linkDensity(sib); if ((len > 80 && ld < 0.25) || (len > 0 && ld === 0 && /[.。!!??]$/.test(normText(sib)))) picked.push(sib); } } return picked; } /* * 隐藏元素只能在活的 DOM 上用 getComputedStyle 判断,克隆出来的游离节点拿不到样式。 * cloneNode(true) 保持结构一致,所以两边 querySelectorAll('*') 的顺序一一对应, * 按下标把「隐藏」标记抄到克隆上,再在克隆上做删除。 */ /** * manual:用户自己点选 / 框选的内容。只清硬噪声(脚本、隐藏、表单…), * 不按 class 关键词删 —— 否则用户特意点了评论区,`.comment` 会被当噪声删光,结果是空的。 */ function cloneForConvert(els, manual = false) { const wrapEl = document.createElement('div'); for (const live of els) { const clone = live.cloneNode(true); const L = [live, ...live.querySelectorAll('*')]; const C = [clone, ...clone.querySelectorAll('*')]; if (L.length === C.length) { for (let i = 0; i < L.length; i++) { const cs = getComputedStyle(L[i]); if (cs.display === 'none' || cs.visibility === 'hidden') C[i].setAttribute('data-p2md-hidden', ''); else if (/^(block|flex|grid|list-item|table|flow-root)$/.test(cs.display) && !BLOCK_TAGS.has(L[i].tagName) && L[i].tagName !== 'BR') C[i].setAttribute('data-p2md-block', ''); } } wrapEl.appendChild(clone); } cleanTree(wrapEl, !manual); return wrapEl; } /** root 的直接子节点是选中的内容本身,永远不删(用户点的就是一个