-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
306 lines (264 loc) · 9.26 KB
/
content.js
File metadata and controls
306 lines (264 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Substack 文章提取器
(function() {
'use strict';
// 检查是否在 Substack 文章页面
function isSubstackPostPage() {
const url = window.location.href;
const isSubstack = url.match(/substack\.com\/p\/|\/p\/[\w-]+/);
console.log('[Substack Extractor] 页面检查:', {
url,
isSubstack: !!isSubstack
});
return !!isSubstack;
}
// 从 JSON-LD 提取结构化数据
function extractJsonLdData() {
console.log('[Substack Extractor] 开始提取 JSON-LD 数据...');
const jsonLdScript = document.querySelector('script[type="application/ld+json"]');
if (!jsonLdScript) {
console.warn('[Substack Extractor] 未找到 JSON-LD script 标签');
return null;
}
try {
const data = JSON.parse(jsonLdScript.textContent);
console.log('[Substack Extractor] JSON-LD 解析成功:', {
headline: data.headline,
datePublished: data.datePublished,
authorCount: Array.isArray(data.author) ? data.author.length : 1
});
return {
title: data.headline || '',
description: data.description || '',
datePublished: data.datePublished || '',
dateModified: data.dateModified || '',
authors: Array.isArray(data.author)
? data.author.map(a => ({
name: a.name || '',
url: a.url || ''
}))
: [{ name: data.author?.name || '', url: data.author?.url || '' }],
publisher: {
name: data.publisher?.name || '',
url: data.publisher?.url || ''
},
image: data.image?.[0]?.url || data.image || '',
isAccessibleForFree: data.isAccessibleForFree ?? true,
url: data.url || window.location.href
};
} catch (e) {
console.error('[Substack Extractor] JSON-LD 解析失败:', e);
return null;
}
}
// 从 DOM 提取文章内容
function extractArticleContent() {
console.log('[Substack Extractor] 开始提取文章内容...');
// Substack 使用 <article> 元素,而不是 <main>
let container = document.querySelector('main');
if (!container) {
console.log('[Substack Extractor] 未找到 main 元素,尝试查找 article 元素');
container = document.querySelector('article');
}
if (!container) {
console.log('[Substack Extractor] 未找到 article 元素,尝试查找 #entry div');
container = document.querySelector('#entry');
}
if (!container) {
console.error('[Substack Extractor] 无法找到内容容器(main/article/#entry)');
return { sections: [], fullText: '' };
}
console.log('[Substack Extractor] 找到内容容器:', container.tagName, container.id, container.className);
// 克隆容器元素以避免修改原始 DOM
const containerClone = container.cloneNode(true);
// 移除不需要的元素(从克隆中)
const elementsToRemove = containerClone.querySelectorAll(
'button, [role="button"], iframe, .paywall, form, input'
);
console.log('[Substack Extractor] 移除了', elementsToRemove.length, '个不需要的元素');
elementsToRemove.forEach(el => el.remove());
// 获取所有内容元素
const contentElements = mainClone.querySelectorAll(
'h2, h3, h4, h5, h6, p, ul, ol, blockquote, pre, code'
);
const sections = [];
contentElements.forEach(el => {
const tag = el.tagName.toLowerCase();
const text = el.textContent?.trim();
if (!text) return;
if (tag === 'h2') {
sections.push({ type: 'h2', content: text });
} else if (tag === 'h3') {
sections.push({ type: 'h3', content: text });
} else if (tag === 'h4') {
sections.push({ type: 'h4', content: text });
} else if (tag === 'p') {
sections.push({ type: 'paragraph', content: text });
} else if (tag === 'ul' || tag === 'ol') {
const items = Array.from(el.querySelectorAll('li')).map(li => li.textContent?.trim() || '');
sections.push({ type: 'list', content: items, ordered: tag === 'ol' });
} else if (tag === 'blockquote') {
sections.push({ type: 'blockquote', content: text });
} else if (tag === 'pre') {
sections.push({ type: 'code', content: text });
}
});
// 获取完整文本(从克隆中)
const fullText = containerClone.textContent?.trim() || '';
console.log('[Substack Extractor] 提取完成:', {
sectionsCount: sections.length,
textLength: fullText.length
});
return { sections, fullText };
}
// 提取图片
function extractImages() {
const main = document.querySelector('main');
if (!main) return [];
const images = Array.from(main.querySelectorAll('img')).map(img => ({
src: img.src || '',
alt: img.alt || '',
title: img.title || ''
}));
return images.filter(img => img.src && !img.src.includes('avatar'));
}
// 提取链接
function extractLinks() {
const main = document.querySelector('main');
if (!main) return [];
const links = Array.from(main.querySelectorAll('a')).map(a => ({
href: a.href || '',
text: a.textContent?.trim() || ''
}));
return links.filter(link => link.href && link.text);
}
// 主提取函数
function extractArticleData() {
console.log('[Substack Extractor] ========== 开始提取文章数据 ==========');
console.log('[Substack Extractor] 当前 URL:', window.location.href);
const jsonLdData = extractJsonLdData();
const articleContent = extractArticleContent();
const images = extractImages();
const links = extractLinks();
const result = {
meta: jsonLdData,
content: articleContent,
images,
links,
extractedAt: new Date().toISOString(),
sourceUrl: window.location.href
};
console.log('[Substack Extractor] 提取完成:', {
hasMeta: !!result.meta,
sectionsCount: result.content.sections.length,
imagesCount: result.images.length,
linksCount: result.links.length
});
console.log('[Substack Extractor] ========== 提取完成 ==========');
return result;
}
// 转换为 Markdown
function convertToMarkdown(data) {
const { meta, content, images, links } = data;
let md = '';
// 标题
md += `# ${meta?.title || 'Untitled'}\n\n`;
// 元数据
md += '## 📋 文章信息\n\n';
if (meta?.authors?.length) {
const authorNames = meta.authors.map(a => a.name).filter(Boolean).join(', ');
md += `- **作者**: ${authorNames}\n`;
}
if (meta?.datePublished) {
const date = new Date(meta.datePublished).toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
md += `- **发布日期**: ${date}\n`;
}
if (meta?.publisher?.name) {
md += `- **出版社**: [${meta.publisher.name}](${meta.publisher.url})\n`;
}
md += `- **原文链接**: ${meta?.url || window.location.href}\n`;
md += '\n';
// 描述
if (meta?.description) {
md += `## 📝 简介\n\n${meta.description}\n\n`;
}
// 封面图
if (meta?.image) {
md += `## 🖼️ 封面\n\n\n\n`;
}
// 正文内容
md += '## 📖 正文\n\n';
content.sections.forEach(section => {
switch (section.type) {
case 'h2':
md += `## ${section.content}\n\n`;
break;
case 'h3':
md += `### ${section.content}\n\n`;
break;
case 'h4':
md += `#### ${section.content}\n\n`;
break;
case 'paragraph':
md += `${section.content}\n\n`;
break;
case 'list':
section.content.forEach(item => {
md += `${section.ordered ? '1.' : '-'} ${item}\n`;
});
md += '\n';
break;
case 'blockquote':
md += `> ${section.content}\n\n`;
break;
case 'code':
md += '```\n' + section.content + '\n```\n\n';
break;
}
});
// 图片列表
if (images.length > 0) {
md += '## 🖼️ 文章图片\n\n';
images.forEach((img, index) => {
md += `${index + 1}. ${img.alt ? img.alt : '图片'}\n`;
md += ` \n\n`;
});
}
// 相关链接
if (links.length > 0) {
md += '## 🔗 相关链接\n\n';
links.forEach(link => {
md += `- [${link.text}](${link.href})\n`;
});
md += '\n';
}
// 页脚
md += '---\n\n';
md += `*提取时间: ${new Date().toLocaleString('zh-CN')}*\n`;
md += `*由 [Substack to Markdown](https://github.com) 插件生成*\n`;
return md;
}
// 生成文件名
function generateFilename(data) {
const title = data.meta?.title || 'untitled';
const sanitizedTitle = title
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-')
.substring(0, 50);
const date = data.meta?.datePublished
? new Date(data.meta.datePublished).toISOString().split('T')[0]
: new Date().toISOString().split('T')[0];
return `${sanitizedTitle}-${date}.md`;
}
// 将数据暴露给全局,供 popup 调用
window.SubstackExtractor = {
extractArticleData,
convertToMarkdown,
generateFilename,
isSubstackPostPage
};
console.log('Substack to Markdown 插件已加载');
})();