options.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //在popup.js 中调用 backgourd.js 中的变量和方法,很重要
  2. var bg = chrome.extension.getBackgroundPage();
  3. function login (username, password) {
  4. fetch(`http://user.lamp.run/login`, {
  5. method: 'POST',
  6. headers: {
  7. "Content-Type": "application/json"
  8. },
  9. body: JSON.stringify({
  10. type: "assist",
  11. username: username,
  12. password: password
  13. })
  14. }).then((response) => {return response.json();}).then((res) => {
  15. if (res.err === 0) {
  16. bg.userInfo = res.data
  17. chrome.storage.sync.set({userInfo: {username, password, session: bg.userInfo.session}})
  18. chrome.runtime.sendMessage({name:"reloadUser"},function() {})
  19. document.querySelector('.login-box').style.display = 'none'
  20. fetch(`https://assist.lamp.run/getList?username=${username}&session=${bg.userInfo.session}`).then((response) => {return response.json();}).then((res) => {
  21. document.querySelector('#selectTable').style.display = 'block'
  22. setTimeout(() => {
  23. document.querySelectorAll('table input').forEach(element => {
  24. element.onchange = function (e) {
  25. const itemId = e.target.getAttribute("key")
  26. bg.openList[itemId] = !bg.openList[itemId]
  27. bg.saveOpenList()
  28. }
  29. });
  30. }, 0);
  31. })
  32. } else {
  33. alert(res.message)
  34. }
  35. })
  36. }
  37. chrome.storage.sync.get('userInfo', function(data) {
  38. console.log(data)
  39. const userInfo = data.userInfo
  40. if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
  41. login(userInfo.username, userInfo.password)
  42. }
  43. })
  44. // 登录按钮点击
  45. document.getElementsByClassName('login-button')[0].onclick = function () {
  46. const username = document.getElementById('username').value
  47. const password = document.getElementById('password').value
  48. if (!username || !password) {
  49. chrome.notifications.create(null, {
  50. type: 'basic',
  51. iconUrl: 'img/48.png',
  52. title: '运行方案',
  53. message: '用户名或密码不能为空!'
  54. })
  55. return
  56. }
  57. login(username, password)
  58. }