(function() { const container = document.createElement('div'); container.id = 'api-monitor-tool'; container.style = ` position: fixed; top: 10px; right: 10px; width: 380px; background: rgba(30, 30, 46, 0.95); border: 2px solid #4a9ff9; border-radius: 10px; padding: 15px; z-index: 9999; box-shadow: 0 5px 25px rgba(0, 0, 0, 0.5); color: #fff; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-height: 95vh; overflow: auto; `; const title = document.createElement('h3'); title.textContent = 'API响应捕获工具 ak0.cn'; title.style = ` color: #4a9ff9; margin-bottom: 15px; text-align: center; padding-bottom: 10px; border-bottom: 1px solid #4a9ff9; `; container.appendChild(title); const status = document.createElement('div'); status.id = 'monitor-status'; status.textContent = '🟢 正在监控API请求...'; status.style = ` margin-bottom: 15px; padding: 8px; background: rgba(0, 0, 0, 0.3); border-radius: 5px; font-size: 14px; `; container.appendChild(status); const apiListTitle = document.createElement('h4'); apiListTitle.textContent = '捕获的API请求:'; apiListTitle.style = 'margin-bottom: 8px; color: #ccc;'; container.appendChild(apiListTitle); const apiList = document.createElement('div'); apiList.id = 'api-requests'; apiList.style = ` max-height: 150px; overflow-y: auto; margin-bottom: 15px; background: rgba(0, 0, 0, 0.3); border-radius: 5px; padding: 10px; `; const placeholder = document.createElement('div'); placeholder.id = 'api-placeholder'; placeholder.textContent = '等待API请求...'; placeholder.style = 'color: #888; text-align: center; padding: 10px;'; apiList.appendChild(placeholder); container.appendChild(apiList); const responseTitle = document.createElement('h4'); responseTitle.textContent = '目标API响应:'; responseTitle.style = 'margin-bottom: 8px; color: #ccc;'; container.appendChild(responseTitle); const responseContent = document.createElement('div'); responseContent.id = 'api-response'; responseContent.style = ` height: 150px; overflow-y: auto; background: rgba(0, 0, 0, 0.3); border-radius: 5px; padding: 10px; margin-bottom: 15px; white-space: pre-wrap; font-family: monospace; font-size: 13px; `; responseContent.textContent = '选择上方的API请求查看响应 ak0.cn'; container.appendChild(responseContent); const buttonContainer = document.createElement('div'); buttonContainer.style = 'display: flex; gap: 10px; margin-bottom: 15px;'; const copyResponseBtn = document.createElement('button'); copyResponseBtn.textContent = '复制响应'; copyResponseBtn.style = ` flex: 1; background: linear-gradient(135deg, #4a9ff9, #2a75c9); color: white; border: none; padding: 8px; border-radius: 5px; cursor: pointer; `; buttonContainer.appendChild(copyResponseBtn); const copyImgBtn = document.createElement('button'); copyImgBtn.textContent = '复制imgSource'; copyImgBtn.style = ` flex: 1; background: linear-gradient(135deg, #9f4af9, #752ac9); color: white; border: none; padding: 8px; border-radius: 5px; cursor: pointer; `; buttonContainer.appendChild(copyImgBtn); const clearBtn = document.createElement('button'); clearBtn.textContent = '清除记录'; clearBtn.style = ` background: linear-gradient(135deg, #f94a4a, #c92a2a); color: white; border: none; padding: 8px; border-radius: 5px; cursor: pointer; `; buttonContainer.appendChild(clearBtn); container.appendChild(buttonContainer); const previewTitle = document.createElement('h4'); previewTitle.textContent = 'Logo预览,右键图片保存:'; previewTitle.style = 'margin-bottom: 8px; color: #ccc;'; container.appendChild(previewTitle); const imgPreview = document.createElement('img'); imgPreview.id = 'img-preview'; imgPreview.style = ` max-width: 100%; max-height: 200px; display: block; margin: 0 auto; border-radius: 5px; border: 1px solid #4a9ff9; `; imgPreview.alt = 'Logo预览'; container.appendChild(imgPreview); document.body.appendChild(container); const apiRequests = []; const targetApiUrl = 'https://www.logosc.cn/api/generateLogoImg'; const originalFetch = window.fetch; window.fetch = async function(url, options) { const response = await originalFetch.apply(this, arguments); const clonedResponse = response.clone(); try { const data = await clonedResponse.json(); const requestInfo = { url: url, method: options?.method || 'GET', status: response.status, response: data, timestamp: new Date().toLocaleTimeString() }; apiRequests.push(requestInfo); updateRequestsList(); if (url.includes(targetApiUrl)) { updateResponseDisplay(requestInfo); } } catch (error) { console.error('解析响应失败:', error); } return response; }; const originalXHR = window.XMLHttpRequest; window.XMLHttpRequest = function() { const xhr = new originalXHR(); const originalOpen = xhr.open; const originalSend = xhr.send; let requestUrl = ''; xhr.open = function(method, url) { requestUrl = url; return originalOpen.apply(this, arguments); }; xhr.send = function(body) { const originalOnload = xhr.onload; xhr.onload = function() { if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) { try { const data = JSON.parse(xhr.responseText); const requestInfo = { url: requestUrl, method: 'GET', status: xhr.status, response: data, timestamp: new Date().toLocaleTimeString() }; apiRequests.push(requestInfo); updateRequestsList(); if (requestUrl.includes(targetApiUrl)) { updateResponseDisplay(requestInfo); } } catch (error) { console.error('解析响应失败:', error); } } if (originalOnload) { originalOnload.apply(this, arguments); } }; return originalSend.apply(this, arguments); }; return xhr; }; function updateRequestsList() { const apiListElement = document.getElementById('api-requests'); const placeholder = document.getElementById('api-placeholder'); if (placeholder && apiRequests.length > 0) { placeholder.remove(); } apiListElement.innerHTML = ''; apiRequests.forEach((req, index) => { const isTarget = req.url.includes(targetApiUrl); const requestItem = document.createElement('div'); requestItem.className = 'request-item'; requestItem.style = ` padding: 8px; margin-bottom: 5px; border-radius: 4px; background: ${isTarget ? 'rgba(74, 159, 249, 0.2)' : 'rgba(255, 255, 255, 0.05)'}; border-left: 3px solid ${isTarget ? '#4a9ff9' : '#666'}; cursor: pointer; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; `; requestItem.title = `${req.method} ${req.url}`; requestItem.textContent = `${req.method} ${req.url} [${req.status}] - ${req.timestamp}`; requestItem.addEventListener('click', () => { updateResponseDisplay(req); }); apiListElement.appendChild(requestItem); }); } function updateResponseDisplay(requestInfo) { const responseElement = document.getElementById('api-response'); const previewImg = document.getElementById('img-preview'); try { const formattedResponse = JSON.stringify(requestInfo.response, null, 2); responseElement.textContent = formattedResponse; if (requestInfo.response.imgSource) { previewImg.src = requestInfo.response.imgSource; previewImg.style.display = 'block'; } else { previewImg.style.display = 'none'; } document.getElementById('monitor-status').textContent = `🟢 已捕获目标API响应 (${requestInfo.timestamp})`; } catch (error) { responseElement.textContent = '无法解析响应数据'; previewImg.style.display = 'none'; } } copyResponseBtn.addEventListener('click', () => { const responseContent = document.getElementById('api-response').textContent; navigator.clipboard.writeText(responseContent).then(() => { showNotification('响应内容已复制到剪贴板!'); }); }); copyImgBtn.addEventListener('click', () => { const responseContent = document.getElementById('api-response').textContent; try { const responseObj = JSON.parse(responseContent); if (responseObj.imgSource) { navigator.clipboard.writeText(responseObj.imgSource).then(() => { showNotification('imgSource已复制到剪贴板!'); }); } else { showNotification('未找到imgSource内容'); } } catch (e) { showNotification('无法解析响应数据'); } }); clearBtn.addEventListener('click', () => { apiRequests.length = 0; document.getElementById('api-requests').innerHTML = '<div id="api-placeholder" style="color: #888; text-align: center; padding: 10px;">等待API请求...</div>'; document.getElementById('api-response').textContent = '选择上方的API请求查看响应'; document.getElementById('img-preview').style.display = 'none'; document.getElementById('monitor-status').textContent = '🟢 正在监控API请求...'; }); function showNotification(message) { const notification = document.createElement('div'); notification.textContent = message; notification.style = ` position: fixed; top: 20px; right: 20px; background: rgba(40, 167, 69, 0.9); color: white; padding: 12px 20px; border-radius: 5px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); z-index: 10000; `; document.body.appendChild(notification); setTimeout(() => { notification.style.opacity = '0'; notification.style.transition = 'opacity 0.5s'; setTimeout(() => { notification.remove(); }, 500); }, 2000); } console.log('API监控工具已加载!ak0.cn'); })();
|