2 changed files with 113 additions and 59 deletions
@ -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 脚本已加载完成'); |
|||
|
|||
Loading…
Reference in new issue