-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathuserscript.js
More file actions
372 lines (320 loc) · 11.2 KB
/
userscript.js
File metadata and controls
372 lines (320 loc) · 11.2 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
361
362
363
364
365
366
367
368
369
370
371
372
// ==UserScript==
// @name HTML2PDF 网页元素转PDF
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 在任意网站添加按钮,选择页面元素并生成PDF
// @author You
// @match *://*/*
// @grant none
// @require file:///D:/project-code/html2pdf/dist/html2pdf.js
// @require file:///D:/project-code/html2pdf/examples/SourceHanSansSC-Normal-Min-normal.js
// ==/UserScript==
(function () {
'use strict';
// 全局变量
let isSelecting = false;
let selectedElement = null;
let overlay = null;
let controlPanel = null;
// 创建控制面板
function createControlPanel() {
const panel = document.createElement('div');
panel.id = 'html2pdf-control-panel';
panel.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 10000;
background: #fff;
border: 2px solid #007cba;
border-radius: 8px;
padding: 15px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
font-family: Arial, sans-serif;
min-width: 200px;
`;
panel.innerHTML = `
<div style="margin-bottom: 10px; font-weight: bold; color: #333;">HTML2PDF 工具</div>
<button id="select-element-btn" style="
width: 100%;
padding: 8px 12px;
margin-bottom: 8px;
background: #007cba;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
">选择元素</button>
<button id="generate-pdf-btn" style="
width: 100%;
padding: 8px 12px;
margin-bottom: 8px;
background: #28a745;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
opacity: 0.5;
margin-left:0
" disabled>生成PDF</button>
<button id="close-panel-btn" style="
width: 100%;
padding: 6px 12px;
background: #dc3545;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
margin - left: 0
">关闭</button>
<div id="selected-info" style="
margin-top: 10px;
padding: 8px;
background: #f8f9fa;
border-radius: 4px;
font-size: 12px;
color: #666;
display: none;
"></div>
`;
document.body.appendChild(panel);
return panel;
}
// 创建选择覆盖层
function createOverlay() {
const overlay = document.createElement('div');
overlay.id = 'html2pdf-overlay';
overlay.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 123, 186, 0.1);
z-index: 9999;
cursor: crosshair;
pointer-events: none;
`;
document.body.appendChild(overlay);
return overlay;
}
// 高亮元素
function highlightElement(element) {
// 移除之前的高亮
const prevHighlight = document.querySelector('.html2pdf-highlight');
if (prevHighlight) {
prevHighlight.classList.remove('html2pdf-highlight');
}
// 添加高亮样式
if (element && element !== document.body) {
element.classList.add('html2pdf-highlight');
// 添加高亮CSS
if (!document.querySelector('#html2pdf-highlight-style')) {
const style = document.createElement('style');
style.id = 'html2pdf-highlight-style';
style.textContent = `
.html2pdf-highlight {
outline: 3px solid #007cba !important;
outline-offset: 2px !important;
}
`;
document.head.appendChild(style);
}
}
}
// 开始选择模式
function startSelection() {
isSelecting = true;
overlay = createOverlay();
document.body.style.cursor = 'crosshair';
// 更新按钮状态
const selectBtn = document.getElementById('select-element-btn');
selectBtn.textContent = '取消选择';
selectBtn.style.background = '#dc3545';
// 添加事件监听
document.addEventListener('mouseover', onMouseOver);
document.addEventListener('click', onElementClick);
document.addEventListener('keydown', onKeyDown);
}
// 停止选择模式
function stopSelection() {
isSelecting = false;
document.body.style.cursor = '';
if (overlay) {
overlay.remove();
overlay = null;
}
// 更新按钮状态
const selectBtn = document.getElementById('select-element-btn');
selectBtn.textContent = '选择元素';
selectBtn.style.background = '#007cba';
// 移除事件监听
document.removeEventListener('mouseover', onMouseOver);
document.removeEventListener('click', onElementClick);
document.removeEventListener('keydown', onKeyDown);
}
// 鼠标悬停事件
function onMouseOver(e) {
if (!isSelecting) return;
e.preventDefault();
const element = e.target;
if (element !== controlPanel && !controlPanel.contains(element)) {
highlightElement(element);
}
}
// 元素点击事件
function onElementClick(e) {
if (!isSelecting) return;
e.preventDefault();
e.stopPropagation();
const element = e.target;
if (element !== controlPanel && !controlPanel.contains(element)) {
selectedElement = element;
stopSelection();
updateSelectedInfo(element);
enablePdfGeneration();
}
}
// 键盘事件(ESC取消选择)
function onKeyDown(e) {
if (e.key === 'Escape' && isSelecting) {
stopSelection();
}
}
// 更新选中元素信息
function updateSelectedInfo(element) {
const info = document.getElementById('selected-info');
const tagName = element.tagName.toLowerCase();
const className = element.className ? `.${element.className.split(' ').join('.')}` : '';
const id = element.id ? `#${element.id}` : '';
info.innerHTML = `
<strong>已选择元素:</strong><br>
标签: ${tagName}${id}${className}<br>
尺寸: ${element.offsetWidth} × ${element.offsetHeight}px
`;
info.style.display = 'block';
}
// 启用PDF生成按钮
function enablePdfGeneration() {
const pdfBtn = document.getElementById('generate-pdf-btn');
pdfBtn.disabled = false;
pdfBtn.style.opacity = '1';
}
// 生成PDF
async function generatePDF() {
if (!selectedElement) {
alert('请先选择要转换的元素');
return;
}
try {
// 显示加载状态
const pdfBtn = document.getElementById('generate-pdf-btn');
const originalText = pdfBtn.textContent;
pdfBtn.textContent = '生成中...';
pdfBtn.disabled = true;
// 使用html2canvas截图
html2pdf(selectedElement, {
useCORS: true,
scale: 1,
fontConfig: {
fontFamily: 'SourceHanSansCN-Medium',
fontBase64: window.fontBase64, //
fontUrl: '',
fontWeight: 400,
fontStyle: 'normal'
},
}).then(function (canvas) {
const link = document.createElement('a');
link.download = 'output.png';
link.href = canvas.toDataURL('image/png');
link.click();
// 恢复按钮状态
pdfBtn.textContent = originalText;
pdfBtn.disabled = false;
alert('PDF生成成功!');
}).catch(function (err) {
console.log('creacterr', err);
});;
} catch (error) {
console.error('PDF生成失败:', error);
alert('PDF生成失败,请检查控制台错误信息');
// 恢复按钮状态
const pdfBtn = document.getElementById('generate-pdf-btn');
pdfBtn.textContent = '生成PDF';
pdfBtn.disabled = false;
}
}
// 关闭面板
function closePanel() {
if (isSelecting) {
stopSelection();
}
// 移除高亮
const highlight = document.querySelector('.html2pdf-highlight');
if (highlight) {
highlight.classList.remove('html2pdf-highlight');
}
// 移除样式
const style = document.querySelector('#html2pdf-highlight-style');
if (style) {
style.remove();
}
// 移除面板
if (controlPanel) {
controlPanel.remove();
controlPanel = null;
}
selectedElement = null;
}
// 初始化
function init() {
// 等待页面加载完成
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
return;
}
// 创建控制面板
controlPanel = createControlPanel();
// 绑定事件
document.getElementById('select-element-btn').addEventListener('click', () => {
if (isSelecting) {
stopSelection();
} else {
startSelection();
}
});
document.getElementById('generate-pdf-btn').addEventListener('click', generatePDF);
document.getElementById('close-panel-btn').addEventListener('click', closePanel);
// 让面板可拖拽
let isDragging = false;
let dragOffset = {
x: 0,
y: 0
};
controlPanel.addEventListener('mousedown', (e) => {
if (e.target.tagName !== 'BUTTON') {
isDragging = true;
dragOffset.x = e.clientX - controlPanel.offsetLeft;
dragOffset.y = e.clientY - controlPanel.offsetTop;
controlPanel.style.cursor = 'move';
}
});
document.addEventListener('mousemove', (e) => {
if (isDragging) {
controlPanel.style.left = (e.clientX - dragOffset.x) + 'px';
controlPanel.style.top = (e.clientY - dragOffset.y) + 'px';
controlPanel.style.right = 'auto';
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
controlPanel.style.cursor = '';
});
console.log('HTML2PDF 用户脚本已加载');
}
// 启动脚本
init();
})();