新增通用的浏览器自动化扩展插件
This commit is contained in:
153
expand/general-auto/popup.js
Normal file
153
expand/general-auto/popup.js
Normal file
@@ -0,0 +1,153 @@
|
||||
// Popup Script
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const statusEl = document.getElementById('status');
|
||||
const taskCountEl = document.getElementById('taskCount');
|
||||
const requestLogCountEl = document.getElementById('requestLogCount');
|
||||
const logContentEl = document.getElementById('logContent');
|
||||
|
||||
let logs = [];
|
||||
|
||||
// 添加日志
|
||||
function addLog(message, type = 'info') {
|
||||
const now = new Date();
|
||||
const timeStr = now.toLocaleTimeString('zh-CN', {
|
||||
hour12: false,
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
});
|
||||
|
||||
logs.push({
|
||||
time: timeStr,
|
||||
message: message,
|
||||
type: type
|
||||
});
|
||||
|
||||
// 限制日志数量
|
||||
if (logs.length > 50) {
|
||||
logs.shift();
|
||||
}
|
||||
|
||||
renderLogs();
|
||||
}
|
||||
|
||||
// 渲染日志
|
||||
function renderLogs() {
|
||||
if (logs.length === 0) {
|
||||
logContentEl.innerHTML = '<div class="empty-log">暂无日志</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
logContentEl.innerHTML = logs.map(log => `
|
||||
<div class="log-entry">
|
||||
<span class="log-time">[${log.time}]</span>
|
||||
<span class="log-message">${log.message}</span>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
logContentEl.scrollTop = logContentEl.scrollHeight;
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
function updateStatus() {
|
||||
chrome.runtime.sendMessage({ action: 'getStatus' }, response => {
|
||||
if (response && response.success) {
|
||||
const status = response.status;
|
||||
statusEl.textContent = status.isProcessing ? '运行中' : '就绪';
|
||||
statusEl.className = 'status-value ' + (status.isProcessing ? 'active' : 'inactive');
|
||||
taskCountEl.textContent = status.taskCount;
|
||||
requestLogCountEl.textContent = status.requestLogCount;
|
||||
addLog('状态已更新');
|
||||
} else {
|
||||
addLog('获取状态失败', 'error');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 刷新状态按钮
|
||||
document.getElementById('refreshStatus').addEventListener('click', function() {
|
||||
updateStatus();
|
||||
});
|
||||
|
||||
// 获取 Cookies 按钮
|
||||
document.getElementById('getCookies').addEventListener('click', function() {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
|
||||
if (tabs[0]) {
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'getCookies',
|
||||
url: tabs[0].url
|
||||
}, response => {
|
||||
if (response && response.success) {
|
||||
const cookieCount = response.cookies.length;
|
||||
addLog(`获取到 ${cookieCount} 个 Cookie`);
|
||||
console.log('Cookies:', response.cookies);
|
||||
|
||||
// 复制到剪贴板
|
||||
const cookieText = JSON.stringify(response.cookies, null, 2);
|
||||
navigator.clipboard.writeText(cookieText).then(() => {
|
||||
addLog('Cookie 已复制到剪贴板');
|
||||
}).catch(err => {
|
||||
console.error('复制失败:', err);
|
||||
});
|
||||
} else {
|
||||
addLog('获取 Cookie 失败', 'error');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 清除 Cookies 按钮
|
||||
document.getElementById('clearCookies').addEventListener('click', function() {
|
||||
if (confirm('确定要清除当前页面的所有 Cookies 吗?')) {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
|
||||
if (tabs[0]) {
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'clearCookies',
|
||||
url: tabs[0].url
|
||||
}, response => {
|
||||
if (response && response.success) {
|
||||
addLog('Cookies 已清除');
|
||||
} else {
|
||||
addLog('清除 Cookies 失败', 'error');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 查看日志按钮
|
||||
document.getElementById('viewLogs').addEventListener('click', function() {
|
||||
chrome.runtime.sendMessage({ action: 'getRequestLogs' }, response => {
|
||||
if (response && response.success) {
|
||||
const logCount = response.logs.length;
|
||||
addLog(`获取到 ${logCount} 条请求日志`);
|
||||
console.log('请求日志:', response.logs);
|
||||
|
||||
if (logCount > 0) {
|
||||
// 显示最近 5 条
|
||||
response.logs.slice(-5).forEach(log => {
|
||||
addLog(`${log.method} ${log.url} - ${log.status}`);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
addLog('获取请求日志失败', 'error');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 清空日志按钮
|
||||
document.getElementById('clearLog').addEventListener('click', function() {
|
||||
logs = [];
|
||||
renderLogs();
|
||||
addLog('日志已清空');
|
||||
});
|
||||
|
||||
// 初始化
|
||||
addLog('扩展已就绪');
|
||||
updateStatus();
|
||||
|
||||
// 定时更新状态
|
||||
setInterval(updateStatus, 5000);
|
||||
});
|
||||
Reference in New Issue
Block a user