From fe73e5aa3677e8bc8cfc8019f373ed7996347d0a Mon Sep 17 00:00:00 2001 From: TorsenLi Date: Fri, 11 Oct 2024 17:52:38 +0800 Subject: [PATCH] 1111 --- douyin/background.js | 8 +++ douyin/popup.js | 164 +++++++++++++++++++++++++++---------------- 2 files changed, 113 insertions(+), 59 deletions(-) diff --git a/douyin/background.js b/douyin/background.js index 8c0dcba..39904ad 100644 --- a/douyin/background.js +++ b/douyin/background.js @@ -69,4 +69,12 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { sendResponse({ status: 'error', message: '登录请求出错,请检查网络连接或稍后重试。' }); }); } + + // 添加状态检查功能 + if (request.action === 'checkStatus') { + // 执行状态检查的逻辑 + // 这里只是一个示例,您需要根据实际需求实现检查逻辑 + let status = '正常'; + sendResponse({status: status}); + } }); \ No newline at end of file diff --git a/douyin/popup.js b/douyin/popup.js index 91cfe54..e17ec41 100644 --- a/douyin/popup.js +++ b/douyin/popup.js @@ -1,77 +1,123 @@ -var usernameInput = document.getElementById('username'); -var passwordInput = document.getElementById('password'); +// 将所有的 DOM 操作移到 DOMContentLoaded 事件监听器内部 +document.addEventListener('DOMContentLoaded', function() { + console.log('DOM 已加载完成'); + + var usernameInput = document.getElementById('username'); + var passwordInput = document.getElementById('password'); + var liveIdInput = document.getElementById('liveId'); + const openIncognitoButton = document.getElementById('openIncognito'); + const openLiveButton = document.getElementById('openLive'); + const loginButton = document.getElementById('login'); + const checkButton = document.getElementById('checkButton'); + const userinfoElement = document.getElementById('userinfo'); + + console.log('元素检查:', + 'usernameInput:', !!usernameInput, + 'passwordInput:', !!passwordInput, + 'liveIdInput:', !!liveIdInput, + 'openIncognitoButton:', !!openIncognitoButton, + 'openLiveButton:', !!openLiveButton, + 'loginButton:', !!loginButton, + 'checkButton:', !!checkButton, + 'userinfoElement:', !!userinfoElement + ); // 如果已经登录,提取local中信息 chrome.storage.local.get(['loginStatus', 'loginInfo'], function(data) { - if (data.loginStatus) { - document.getElementById('userinfo').textContent = '已登录:' + data.loginInfo.uname; + if (data.loginStatus && userinfoElement) { + userinfoElement.textContent = '已登录:' + data.loginInfo.uname; } }); - -// DOM 载入完成 -document.addEventListener('DOMContentLoaded', function() { - const openIncognitoButton = document.getElementById('openIncognito'); - const openLiveButton = document.getElementById('openLive'); - const loginButton = document.getElementById('login'); // 打开抖音 - openIncognitoButton.addEventListener('click', function() { - const username = usernameInput.value; - const password = passwordInput.value; - - // 发送消息到后台脚本以打开隐身窗口 - chrome.runtime.sendMessage({ - action: 'openIncognitoDouyin', - username: username, - password: password - }, function(response) { - if (response && response.success) { - console.log('隐身窗口已打开,准备提交登录'); - // 向新打开的窗口发送消息,执行登录操作 - chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { - chrome.tabs.sendMessage(tabs[0].id, { - action: 'submitLogin', - username: username, - password: password + if (openIncognitoButton) { + openIncognitoButton.addEventListener('click', function() { + console.log('打开隐身窗口按钮被点击'); + const username = usernameInput ? usernameInput.value : ''; + const password = passwordInput ? passwordInput.value : ''; + + chrome.runtime.sendMessage({ + action: 'openIncognitoDouyin', + username: username, + password: password + }, function(response) { + if (response && response.success) { + console.log('隐身窗口已打开,准备提交登录'); + chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + chrome.tabs.sendMessage(tabs[0].id, { + action: 'submitLogin', + username: username, + password: password + }); }); - }); - } else { - console.error('打开隐身窗口失败'); - } + } else { + console.error('打开隐身窗口失败'); + } + }); }); - }); + } else { + console.error('未找到打开隐身窗口按钮'); + } // 打开直播间 - openLiveButton.addEventListener('click', () => { - const liveId = document.getElementById('liveId').value; - if (liveId) { - const liveUrl = `https://live.douyin.com/${liveId}`; - chrome.runtime.sendMessage({ action: 'openLiveIncognito', url: liveUrl }); - } - }); + if (openLiveButton && liveIdInput) { + openLiveButton.addEventListener('click', () => { + console.log('打开直播间按钮被点击'); + const liveId = liveIdInput.value.trim(); + if (liveId) { + const liveUrl = `https://live.douyin.com/${liveId}`; + chrome.runtime.sendMessage({ action: 'openLiveIncognito', url: liveUrl }); + } else { + console.error('未输入直播ID'); + alert('请输入直播ID'); + } + }); + } else { + console.error('未找到打开直播间按钮或直播ID输入框'); + } // 登录 - loginButton.addEventListener('click', function() { - const username = usernameInput.value; - const password = passwordInput.value; - - if (!username || !password) { - alert('请输入账号和密码!'); - return; - } else { - // 发送到后台打开 - chrome.runtime.sendMessage({ action: 'login', username: username, password: password }); - } - }); + if (loginButton && usernameInput && passwordInput) { + loginButton.addEventListener('click', function() { + console.log('登录按钮被点击'); + const username = usernameInput.value.trim(); + const password = passwordInput.value.trim(); - // 监听登录结果 - chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { - if (request.action === 'loginResult') { - if (request.status === 'success') { - document.getElementById('userinfo').textContent = '已登录:' + request.uname; + if (!username || !password) { + alert('请输入账号和密码!'); + return; } else { - alert('登录失败:' + request.message); + chrome.runtime.sendMessage({ action: 'login', username: username, password: password }); } + }); + } else { + console.error('未找到登录按钮或用户名/密码输入框'); + } + + // 添加新的检查按钮功能 + if (checkButton) { + checkButton.addEventListener('click', function() { + console.log('检查状态按钮被点击'); + chrome.runtime.sendMessage({action: 'checkStatus'}, function(response) { + console.log('状态检查结果:', response); + alert('状态检查结果: ' + (response && response.status ? response.status : '未知')); + }); + }); + } else { + console.error('未找到检查状态按钮'); + } +}); + +// 监听登录结果 +chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + if (request.action === 'loginResult') { + const userinfoElement = document.getElementById('userinfo'); + if (request.status === 'success' && userinfoElement) { + userinfoElement.textContent = '已登录:' + request.uname; + } else { + alert('登录失败:' + (request.message || '未知错误')); } - }); + } }); + +console.log('popup.js 脚本已加载完成');