改进: - 页面加载等待时间上限提高到 60秒 - 添加等待 networkidle 状态逻辑 - 前端界面增加等待设置说明 - 提示用户针对验证网站设置 10-30秒等待 使用场景: - Cloudflare 等验证网站 - 复杂加载过程网站 - 需要用户确认的页面
488 lines
16 KiB
HTML
488 lines
16 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Web Capture - 网页截图与代码提取</title>
|
||
<style>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
body {
|
||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
min-height: 100vh;
|
||
padding: 20px;
|
||
}
|
||
|
||
.container {
|
||
max-width: 1200px;
|
||
margin: 0 auto;
|
||
}
|
||
|
||
.header {
|
||
text-align: center;
|
||
color: white;
|
||
margin-bottom: 30px;
|
||
}
|
||
|
||
.header h1 {
|
||
font-size: 2.5em;
|
||
margin-bottom: 10px;
|
||
}
|
||
|
||
.header p {
|
||
opacity: 0.9;
|
||
}
|
||
|
||
.card {
|
||
background: white;
|
||
border-radius: 16px;
|
||
padding: 30px;
|
||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.form-group {
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
label {
|
||
display: block;
|
||
font-weight: 600;
|
||
margin-bottom: 8px;
|
||
color: #333;
|
||
}
|
||
|
||
input[type="text"],
|
||
input[type="number"],
|
||
select {
|
||
width: 100%;
|
||
padding: 12px;
|
||
border: 2px solid #e0e0e0;
|
||
border-radius: 8px;
|
||
font-size: 16px;
|
||
transition: border-color 0.3s;
|
||
}
|
||
|
||
input:focus,
|
||
select:focus {
|
||
outline: none;
|
||
border-color: #667eea;
|
||
}
|
||
|
||
.radio-group {
|
||
display: flex;
|
||
gap: 20px;
|
||
}
|
||
|
||
.radio-label {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.radio-label input[type="radio"] {
|
||
width: 20px;
|
||
height: 20px;
|
||
}
|
||
|
||
.checkbox-group {
|
||
display: flex;
|
||
gap: 20px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.checkbox-label {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.checkbox-label input[type="checkbox"] {
|
||
width: 20px;
|
||
height: 20px;
|
||
}
|
||
|
||
.row {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||
gap: 15px;
|
||
}
|
||
|
||
.btn {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
border: none;
|
||
padding: 15px 40px;
|
||
font-size: 18px;
|
||
font-weight: 600;
|
||
border-radius: 8px;
|
||
cursor: pointer;
|
||
transition: transform 0.2s, box-shadow 0.2s;
|
||
width: 100%;
|
||
margin-top: 10px;
|
||
}
|
||
|
||
.btn:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
|
||
}
|
||
|
||
.btn:active {
|
||
transform: translateY(0);
|
||
}
|
||
|
||
.btn:disabled {
|
||
opacity: 0.6;
|
||
cursor: not-allowed;
|
||
transform: none;
|
||
}
|
||
|
||
.loading {
|
||
display: none;
|
||
text-align: center;
|
||
padding: 20px;
|
||
}
|
||
|
||
.loading.active {
|
||
display: block;
|
||
}
|
||
|
||
.spinner {
|
||
border: 4px solid #f3f3f3;
|
||
border-top: 4px solid #667eea;
|
||
border-radius: 50%;
|
||
width: 40px;
|
||
height: 40px;
|
||
animation: spin 1s linear infinite;
|
||
margin: 0 auto 15px;
|
||
}
|
||
|
||
@keyframes spin {
|
||
0% { transform: rotate(0deg); }
|
||
100% { transform: rotate(360deg); }
|
||
}
|
||
|
||
.result {
|
||
display: none;
|
||
}
|
||
|
||
.result.active {
|
||
display: block;
|
||
}
|
||
|
||
.result-image {
|
||
width: 100%;
|
||
border-radius: 8px;
|
||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||
}
|
||
|
||
.result-code {
|
||
background: #1e1e1e;
|
||
color: #d4d4d4;
|
||
padding: 20px;
|
||
border-radius: 8px;
|
||
overflow-x: auto;
|
||
max-height: 600px;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 14px;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.result-code pre {
|
||
margin: 0;
|
||
white-space: pre-wrap;
|
||
word-wrap: break-word;
|
||
}
|
||
|
||
.actions {
|
||
margin-top: 20px;
|
||
display: flex;
|
||
gap: 10px;
|
||
}
|
||
|
||
.btn-secondary {
|
||
background: #6c757d;
|
||
padding: 10px 20px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.error {
|
||
background: #fee;
|
||
border: 1px solid #fcc;
|
||
color: #c00;
|
||
padding: 15px;
|
||
border-radius: 8px;
|
||
margin-top: 20px;
|
||
display: none;
|
||
}
|
||
|
||
.error.active {
|
||
display: block;
|
||
}
|
||
|
||
.api-docs {
|
||
margin-top: 30px;
|
||
padding: 20px;
|
||
background: #f8f9fa;
|
||
border-radius: 8px;
|
||
}
|
||
|
||
.api-docs h3 {
|
||
margin-bottom: 15px;
|
||
color: #333;
|
||
}
|
||
|
||
.api-docs code {
|
||
background: #e9ecef;
|
||
padding: 2px 6px;
|
||
border-radius: 4px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.api-docs pre {
|
||
background: #1e1e1e;
|
||
color: #d4d4d4;
|
||
padding: 15px;
|
||
border-radius: 8px;
|
||
overflow-x: auto;
|
||
margin-top: 10px;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<div class="header">
|
||
<h1>🌐 Web Capture</h1>
|
||
<p>网页截图与代码提取服务</p>
|
||
</div>
|
||
|
||
<div class="card">
|
||
<form id="captureForm">
|
||
<div class="form-group">
|
||
<label for="url">目标网址</label>
|
||
<input type="text" id="url" placeholder="https://example.com" required>
|
||
</div>
|
||
|
||
<div class="form-group">
|
||
<label>操作类型</label>
|
||
<div class="radio-group">
|
||
<label class="radio-label">
|
||
<input type="radio" name="action" value="screenshot" checked>
|
||
📸 截图
|
||
</label>
|
||
<label class="radio-label">
|
||
<input type="radio" name="action" value="html">
|
||
📄 提取HTML
|
||
</label>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="form-group">
|
||
<label>高级选项</label>
|
||
<div class="checkbox-group">
|
||
<label class="checkbox-label">
|
||
<input type="checkbox" id="fullPage">
|
||
全页截图
|
||
</label>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="form-group">
|
||
<label>等待设置(重要!)</label>
|
||
<div class="row">
|
||
<div>
|
||
<label for="waitTime">页面加载等待(ms) ⏱️</label>
|
||
<input type="number" id="waitTime" value="2000" min="0" max="60000" step="500">
|
||
<small style="color: #666; display: block; margin-top: 4px;">某些网站需要较长验证过程,可设置为 10000-30000</small>
|
||
</div>
|
||
<div>
|
||
<label for="scrollTimes">滚动次数(可选)</label>
|
||
<input type="number" id="scrollTimes" value="0" min="0" max="20">
|
||
</div>
|
||
<div>
|
||
<label for="scrollDelay">滚动间隔(ms)</label>
|
||
<input type="number" id="scrollDelay" value="1000" min="500" max="5000" step="100">
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="form-group">
|
||
<label>视口大小</label>
|
||
<div class="row">
|
||
<div>
|
||
<label for="viewportWidth">宽度</label>
|
||
<input type="number" id="viewportWidth" value="1920" min="320" max="3840">
|
||
</div>
|
||
<div>
|
||
<label for="viewportHeight">高度</label>
|
||
<input type="number" id="viewportHeight" value="1080" min="240" max="2160">
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<button type="submit" class="btn" id="submitBtn">
|
||
🚀 开始捕获
|
||
</button>
|
||
</form>
|
||
|
||
<div class="loading" id="loading">
|
||
<div class="spinner"></div>
|
||
<p>正在处理,请稍候...</p>
|
||
</div>
|
||
|
||
<div class="error" id="error"></div>
|
||
|
||
<div class="result" id="result">
|
||
<h3 style="margin-bottom: 15px;">捕获结果</h3>
|
||
<div id="resultContent"></div>
|
||
<div class="actions">
|
||
<button class="btn btn-secondary" onclick="downloadResult()">💾 下载</button>
|
||
<button class="btn btn-secondary" onclick="copyResult()">📋 复制</button>
|
||
<button class="btn btn-secondary" onclick="resetForm()">🔄 重新捕获</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="card api-docs">
|
||
<h3>📚 使用说明</h3>
|
||
<p><strong>重要提示:</strong></p>
|
||
<ul style="margin: 10px 0; line-height: 1.8;">
|
||
<li>⏱️ <strong>页面加载等待</strong>:某些网站需要验证过程(如 Cloudflare),请设置较长时间(10000-30000ms)</li>
|
||
<li>🔄 <strong>滚动次数</strong>:用于加载动态内容(如微博、推特等),建议 3-5 次</li>
|
||
<li>🎯 <strong>全页截图</strong>:滚动加载后建议开启此选项</li>
|
||
</ul>
|
||
<p style="margin-top: 15px;"><strong>API 调用:</strong></p>
|
||
<pre><code>POST /api/capture
|
||
{
|
||
"url": "https://example.com",
|
||
"action": "screenshot", // 或 "html"
|
||
"wait_time": 15000, // 重要!验证网站需设置较长等待
|
||
"scroll_times": 3, // 可选:加载动态内容
|
||
"scroll_delay": 1000, // 可选:滚动间隔
|
||
"full_page": true, // 可选:全页截图
|
||
"backend": "playwright" // 可选:playwright 或 agent-browser
|
||
}</code></pre>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
const form = document.getElementById('captureForm');
|
||
const loading = document.getElementById('loading');
|
||
const result = document.getElementById('result');
|
||
const error = document.getElementById('error');
|
||
const submitBtn = document.getElementById('submitBtn');
|
||
let currentData = null;
|
||
let currentBlob = null;
|
||
|
||
form.addEventListener('submit', async (e) => {
|
||
e.preventDefault();
|
||
|
||
const url = document.getElementById('url').value;
|
||
const action = document.querySelector('input[name="action"]:checked').value;
|
||
const fullPage = document.getElementById('fullPage').checked;
|
||
const scrollTimes = parseInt(document.getElementById('scrollTimes').value);
|
||
const scrollDelay = parseInt(document.getElementById('scrollDelay').value);
|
||
const waitTime = parseInt(document.getElementById('waitTime').value);
|
||
const viewportWidth = parseInt(document.getElementById('viewportWidth').value);
|
||
const viewportHeight = parseInt(document.getElementById('viewportHeight').value);
|
||
|
||
currentData = {
|
||
url,
|
||
action,
|
||
scroll_times: scrollTimes,
|
||
scroll_delay: scrollDelay,
|
||
full_page: fullPage,
|
||
wait_time: waitTime,
|
||
viewport: {
|
||
width: viewportWidth,
|
||
height: viewportHeight
|
||
}
|
||
};
|
||
|
||
// 显示加载
|
||
loading.classList.add('active');
|
||
result.classList.remove('active');
|
||
error.classList.remove('active');
|
||
submitBtn.disabled = true;
|
||
|
||
try {
|
||
const response = await fetch('/api/capture', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify(currentData)
|
||
});
|
||
|
||
if (!response.ok) {
|
||
const errData = await response.json();
|
||
throw new Error(errData.error || '请求失败');
|
||
}
|
||
|
||
if (action === 'screenshot') {
|
||
currentBlob = await response.blob();
|
||
const imageUrl = URL.createObjectURL(currentBlob);
|
||
document.getElementById('resultContent').innerHTML =
|
||
`<img src="${imageUrl}" class="result-image" alt="截图结果">`;
|
||
} else {
|
||
const data = await response.json();
|
||
currentBlob = new Blob([data.html], { type: 'text/html' });
|
||
document.getElementById('resultContent').innerHTML =
|
||
`<div class="result-code"><pre>${escapeHtml(data.html)}</pre></div>`;
|
||
}
|
||
|
||
result.classList.add('active');
|
||
} catch (err) {
|
||
error.textContent = '❌ ' + err.message;
|
||
error.classList.add('active');
|
||
} finally {
|
||
loading.classList.remove('active');
|
||
submitBtn.disabled = false;
|
||
}
|
||
});
|
||
|
||
function escapeHtml(text) {
|
||
const div = document.createElement('div');
|
||
div.textContent = text;
|
||
return div.innerHTML;
|
||
}
|
||
|
||
function downloadResult() {
|
||
if (!currentBlob) return;
|
||
|
||
const url = URL.createObjectURL(currentBlob);
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.download = currentData.action === 'screenshot' ? 'screenshot.png' : 'page.html';
|
||
a.click();
|
||
URL.revokeObjectURL(url);
|
||
}
|
||
|
||
function copyResult() {
|
||
if (!currentBlob) return;
|
||
|
||
const reader = new FileReader();
|
||
reader.onload = () => {
|
||
navigator.clipboard.writeText(reader.result).then(() => {
|
||
alert('已复制到剪贴板');
|
||
});
|
||
};
|
||
reader.readAsText(currentBlob);
|
||
}
|
||
|
||
function resetForm() {
|
||
result.classList.remove('active');
|
||
error.classList.remove('active');
|
||
currentData = null;
|
||
currentBlob = null;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html> |