-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcontent.js
More file actions
360 lines (312 loc) · 12.4 KB
/
Copy pathcontent.js
File metadata and controls
360 lines (312 loc) · 12.4 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// AiMaster Content Script
// Injected into web pages for platform detection, article crawling and publishing
console.log('AiMaster content script loaded');
// ============================================
// Platform Configuration
// ============================================
const PLATFORMS = {
baidu: {
name: 'Baidu News',
match: (url) => url.includes('baijiahao.baidu.com') || url.includes('baidu.com/s?'),
crawler: null,
publisher: null
},
pengpai: {
name: 'Pengpai News',
match: (url) => url.includes('thepaper.cn'),
crawler: null,
publisher: null
},
sohu: {
name: 'Sohu News',
match: (url) => url.includes('sohu.com'),
crawler: null,
publisher: null
},
tencent: {
name: 'Tencent News',
match: (url) => url.includes('qq.com/') && (url.includes('/omn/') || url.includes('/rain/')),
crawler: null,
publisher: null
},
netease: {
name: 'NetEase News',
match: (url) => url.includes('163.com'),
crawler: null,
publisher: null
},
chinadaily: {
name: 'China Daily',
match: (url) => url.includes('chinadaily.com'),
crawler: null,
publisher: null
},
toutiao: {
name: 'Toutiao',
match: (url) => url.includes('toutiao.com'),
crawler: null,
publisher: null
},
weixin: {
name: 'WeChat Official Account',
match: (url) => url.includes('mp.weixin.qq.com'),
crawler: null,
publisher: null
}
};
// Initialize crawlers from modules
if (typeof window.BaiduCrawler !== 'undefined') {
PLATFORMS.baidu.crawler = () => window.BaiduCrawler.crawl();
}
if (typeof window.PengpaiCrawler !== 'undefined') {
PLATFORMS.pengpai.crawler = () => window.PengpaiCrawler.crawl();
}
if (typeof window.SohuCrawler !== 'undefined') {
PLATFORMS.sohu.crawler = () => window.SohuCrawler.crawl();
}
if (typeof window.TencentCrawler !== 'undefined') {
PLATFORMS.tencent.crawler = () => window.TencentCrawler.crawl();
}
if (typeof window.NeteaseCrawler !== 'undefined') {
PLATFORMS.netease.crawler = () => window.NeteaseCrawler.crawl();
}
if (typeof window.ChinaDailyCrawler !== 'undefined') {
PLATFORMS.chinadaily.crawler = () => window.ChinaDailyCrawler.crawl();
}
if (typeof window.ToutiaoCrawler !== 'undefined') {
PLATFORMS.toutiao.crawler = () => window.ToutiaoCrawler.crawl();
}
// ============================================
// Platform Detection
// ============================================
function detectPlatform() {
const url = window.location.href;
for (const [key, platform] of Object.entries(PLATFORMS)) {
if (platform.match(url)) {
return {
platform: key,
platformName: platform.name
};
}
}
return {
platform: 'unknown',
platformName: 'Unknown Platform'
};
}
// ============================================
// NetEase News Crawler
// Moved to crawlers/netease.js module
// ============================================
// ============================================
// Toutiao Crawler
// Moved to crawlers/toutiao.js module
// ============================================
// ============================================
// Tencent News Crawler
// Moved to crawlers/tencent.js module
// ============================================
// ============================================
// WeChat Official Account Crawler
// ============================================
PLATFORMS.weixin.crawler = function() {
try {
const article = {
title: '',
content: '',
author: '',
publishTime: '',
images: []
};
article.title = document.querySelector('#activity-name')?.textContent?.trim() ||
document.querySelector('.rich_media_title')?.textContent?.trim() ||
'';
const contentEl = document.querySelector('#js_content') ||
document.querySelector('.rich_media_content');
if (contentEl) {
const sections = Array.from(contentEl.querySelectorAll('section, p'));
article.content = sections
.map(el => el.textContent.trim())
.filter(text => text.length > 0)
.join('\n\n');
const images = Array.from(contentEl.querySelectorAll('img'));
article.images = images.map(img => ({
src: img.getAttribute('data-src') || img.src,
alt: img.alt || ''
}));
}
article.author = document.querySelector('#js_name')?.textContent?.trim() ||
document.querySelector('.rich_media_meta_text')?.textContent?.trim() ||
'Unknown';
article.publishTime = document.querySelector('#publish_time')?.textContent?.trim() || '';
if (!article.title || !article.content) {
throw new Error('Failed to extract complete article information');
}
return article;
} catch (error) {
console.error('WeChat crawler failed:', error);
throw error;
}
};
// ============================================
// Generic Publisher for Editors
// ============================================
function publishToEditor(article, platform) {
try {
console.log('Publishing article to editor:', platform);
let titleInput, contentEditor;
// Select different selectors based on platform
switch (platform) {
case 'weixin':
// WeChat Official Account Editor
titleInput = document.querySelector('#title') ||
document.querySelector('.title-input') ||
document.querySelector('input[placeholder*="标题"]');
contentEditor = document.querySelector('#ueditor_0') ||
document.querySelector('.edui-body-container') ||
document.querySelector('.editor-content');
break;
case 'toutiao':
// Toutiao Editor
titleInput = document.querySelector('.title-input') ||
document.querySelector('input[placeholder*="标题"]');
contentEditor = document.querySelector('.ql-editor') ||
document.querySelector('.editor-container');
break;
default:
// Generic editor detection
titleInput = document.querySelector('input[type="text"]') ||
document.querySelector('.title-input');
contentEditor = document.querySelector('.editor') ||
document.querySelector('[contenteditable="true"]');
}
if (!titleInput && !contentEditor) {
throw new Error('Editor not found, please ensure you are on an editor page');
}
// Fill title
if (titleInput) {
titleInput.value = article.title;
titleInput.dispatchEvent(new Event('input', { bubbles: true }));
titleInput.dispatchEvent(new Event('change', { bubbles: true }));
}
// Fill content
if (contentEditor) {
if (contentEditor.contentEditable === 'true') {
contentEditor.innerHTML = article.content.split('\n\n')
.map(p => `<p>${p}</p>`)
.join('');
} else {
contentEditor.textContent = article.content;
}
contentEditor.dispatchEvent(new Event('input', { bubbles: true }));
contentEditor.dispatchEvent(new Event('change', { bubbles: true }));
contentEditor.focus();
}
console.log('Article successfully inserted into editor');
return true;
} catch (error) {
console.error('Publishing to editor failed:', error);
throw error;
}
}
// Set publishers for all platforms
PLATFORMS.weixin.publisher = (article) => publishToEditor(article, 'weixin');
PLATFORMS.toutiao.publisher = (article) => publishToEditor(article, 'toutiao');
// ============================================
// Message Listener
// ============================================
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('=== Content script 收到消息 ===');
console.log('消息类型:', request.action);
console.log('完整消息:', request);
switch (request.action) {
case 'detectPlatform':
const platformInfo = detectPlatform();
console.log('检测到平台:', platformInfo);
sendResponse(platformInfo);
break;
case 'crawlArticle':
console.log('=== 开始爬取文章 ===');
try {
const platform = detectPlatform();
console.log('当前平台:', platform);
if (platform.platform === 'unknown') {
console.error('不支持的平台');
sendResponse({ success: false, error: 'Unsupported platform' });
return;
}
const platformConfig = PLATFORMS[platform.platform];
console.log('平台配置:', {
name: platformConfig.name,
hasCrawler: !!platformConfig.crawler
});
if (!platformConfig.crawler) {
console.error('该平台暂未实现爬虫功能');
sendResponse({ success: false, error: 'Crawler not implemented for this platform' });
return;
}
console.log('调用爬虫函数...');
const article = platformConfig.crawler();
console.log('爬取成功,数据:', {
title: article.title,
author: article.author,
contentLength: article.contents?.length || article.content?.length || 0,
imagesCount: article.images?.length || 0,
commentsCount: article.commentCount || 0
});
console.log('完整文章数据:', article);
sendResponse({
success: true,
platform: platform.platform,
platformName: platform.platformName,
article: article
});
} catch (error) {
console.error('=== 爬取失败 ===');
console.error('错误类型:', error.name);
console.error('错误消息:', error.message);
console.error('错误堆栈:', error.stack);
sendResponse({
success: false,
error: error.message
});
}
break;
case 'publishArticle':
console.log('=== 开始发布文章 ===');
try {
const targetPlatform = request.platform === 'auto'
? detectPlatform().platform
: request.platform;
console.log('目标平台:', targetPlatform);
const platformConfig = PLATFORMS[targetPlatform];
if (!platformConfig || !platformConfig.publisher) {
console.error('当前平台不支持自动发布');
sendResponse({
success: false,
error: 'Auto-publish not supported for this platform, please copy manually'
});
return;
}
console.log('调用发布函数...');
platformConfig.publisher(request.article);
console.log('发布成功');
sendResponse({ success: true });
} catch (error) {
console.error('=== 发布失败 ===');
console.error('错误类型:', error.name);
console.error('错误消息:', error.message);
console.error('错误堆栈:', error.stack);
sendResponse({
success: false,
error: error.message
});
}
break;
default:
console.warn('未知的消息类型:', request.action);
sendResponse({ success: false, error: 'Unknown action' });
}
return true;
});
console.log('AiMaster content script ready');