//在popup.js 中调用 backgourd.js 中的变量和方法,很重要 var bg = chrome.extension.getBackgroundPage(); function login (username, password) { fetch(`http://user.lamp.run/login`, { method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: "assist", username: username, password: password }) }).then((response) => {return response.json();}).then((res) => { if (res.err === 0) { bg.userInfo = res.data chrome.storage.sync.set({userInfo: {username, password, session: bg.userInfo.session}}) chrome.runtime.sendMessage({name:"reloadUser"},function() {}) document.querySelector('.login-box').style.display = 'none' fetch(`https://assist.lamp.run/getList?username=${username}&session=${bg.userInfo.session}`).then((response) => {return response.json();}).then((res) => { document.querySelector('#selectTable').style.display = 'block' setTimeout(() => { document.querySelectorAll('table input').forEach(element => { element.onchange = function (e) { const itemId = e.target.getAttribute("key") bg.openList[itemId] = !bg.openList[itemId] bg.saveOpenList() } }); }, 0); }) } else { alert(res.message) } }) } chrome.storage.sync.get('userInfo', function(data) { console.log(data) const userInfo = data.userInfo if (userInfo && userInfo.username && userInfo.password && userInfo.session) { login(userInfo.username, userInfo.password) } }) // 登录按钮点击 document.getElementsByClassName('login-button')[0].onclick = function () { const username = document.getElementById('username').value const password = document.getElementById('password').value if (!username || !password) { chrome.notifications.create(null, { type: 'basic', iconUrl: 'img/48.png', title: '运行方案', message: '用户名或密码不能为空!' }) return } login(username, password) }