-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
146 lines (119 loc) · 4.07 KB
/
content.js
File metadata and controls
146 lines (119 loc) · 4.07 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
// content.js - 注入微信公众号页面的内容脚本
// 处理 HTML 插入到微信编辑器
(function() {
'use strict';
console.log('[WeixLayout] Content script 已加载');
// 微信编辑器选择器列表(按优先级)
const EDITOR_SELECTORS = [
'.ProseMirror',
'.edui-editor-body',
'[contenteditable="true"]'
];
// 监听来自 sidepanel 的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('[WeixLayout] 收到消息:', request.type);
switch (request.type) {
case 'INSERT_HTML':
handleInsertHTML(request.payload, sendResponse);
return true;
case 'CHECK_EDITOR':
handleCheckEditor(sendResponse);
return true;
default:
sendResponse({ success: false, error: '未知的消息类型' });
}
});
// 处理 HTML 插入
function handleInsertHTML(payload, sendResponse) {
const { html, isInlined } = payload;
try {
const editor = findEditor();
if (!editor) {
sendResponse({ success: false, error: '未找到微信编辑器' });
return;
}
const finalHtml = isInlined ? html : inlineStyles(html);
const insertResult = insertToEditor(editor, finalHtml);
sendResponse(insertResult);
} catch (error) {
console.error('[WeixLayout] 插入失败:', error);
sendResponse({ success: false, error: error.message });
}
}
// 检查编辑器状态
function handleCheckEditor(sendResponse) {
const editor = findEditor();
sendResponse({
success: true,
data: {
hasEditor: !!editor,
editorType: editor?.className || null
}
});
}
// 查找微信编辑器
function findEditor() {
for (const selector of EDITOR_SELECTORS) {
const editor = document.querySelector(selector);
if (editor) {
console.log('[WeixLayout] 找到编辑器:', selector);
return editor;
}
}
return null;
}
// 将 HTML 插入到编辑器
function insertToEditor(editor, html) {
console.log('[WeixLayout] 准备插入 HTML,长度:', html.length);
// 微信 ProseMirror 编辑器需要使用剪贴板方式
// 创建临时元素用于复制富文本
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
tempDiv.style.position = 'fixed';
tempDiv.style.left = '-9999px';
tempDiv.style.top = '0';
tempDiv.setAttribute('contenteditable', 'true');
document.body.appendChild(tempDiv);
// 选中临时元素内容
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(tempDiv);
selection.removeAllRanges();
selection.addRange(range);
// 复制到剪贴板
const copySuccess = document.execCommand('copy');
console.log('[WeixLayout] 复制到剪贴板:', copySuccess);
// 移除临时元素
document.body.removeChild(tempDiv);
if (copySuccess) {
// 聚焦编辑器并粘贴
editor.focus();
// 选中编辑器所有内容(用于替换)
const editorRange = document.createRange();
editorRange.selectNodeContents(editor);
selection.removeAllRanges();
selection.addRange(editorRange);
// 执行粘贴
const pasteSuccess = document.execCommand('paste');
if (!pasteSuccess) {
// 如果粘贴失败,提示用户手动粘贴
console.log('[WeixLayout] 自动粘贴失败,需手动操作');
alert('✅ 内容已复制到剪贴板!\n\n请按 Ctrl+V (Mac: Cmd+V) 粘贴到编辑器。');
return {
success: true,
manualPasteRequired: true,
message: '内容已复制到剪贴板,请手动粘贴到编辑器'
};
}
return { success: true };
} else {
console.error('[WeixLayout] 复制失败');
return { success: false, error: '无法复制内容到剪贴板' };
}
console.log('[WeixLayout] 操作完成');
}
// CSS 内联处理
function inlineStyles(html) {
return globalThis.WeixLayoutThemeInline.inlineThemeStyles(html);
}
})();