options.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //在popup.js 中调用 backgourd.js 中的变量和方法,很重要
  2. var bg = chrome.extension.getBackgroundPage();
  3. const serverUrl = 'https://going.run/assist'
  4. // 登录按钮点击
  5. document.getElementsByClassName('login-button')[0].onclick = function () {
  6. const username = document.getElementById('username').value
  7. const password = document.getElementById('password').value
  8. if (!username || !password) {
  9. chrome.notifications.create(null, {
  10. type: 'basic',
  11. iconUrl: 'img/48.png',
  12. title: '运行方案',
  13. message: '用户名或密码不能为空!'
  14. })
  15. return
  16. }
  17. fetch(`http://going.run/userServer?route=login`, {
  18. method: 'POST',
  19. headers: {
  20. "Content-Type": "application/json"
  21. },
  22. body: JSON.stringify({
  23. type: "assist",
  24. username: username,
  25. password: password
  26. })
  27. }).then((response) => {return response.json();}).then((res) => {
  28. if (res.err === 0) {
  29. bg.userInfo = res.data
  30. bg.saveUser(username, password)
  31. // do something
  32. }
  33. })
  34. }
  35. if (bg.userInfo) {
  36. document.querySelector('.login-box').style.display = 'none'
  37. let newHtml = `<table border="0"><thead><tr><th>编号</th><th>脚本名称</th><th>开启状态</th></tr></thead><tbody>`
  38. fetch('https://going.run/assist?route=getList').then((response) => {return response.json();}).then((res) => {
  39. console.log(bg.openList)
  40. res.data.forEach(element => {
  41. newHtml += `<tr><td>${element.id}</td><td>${element.name}</td><td><input type="checkbox" key="${element.id}" id="check${element.id}" name="check${element.id}" ${bg.openList[element.id] ? 'checked' : ''}></td></tr>`
  42. });
  43. newHtml += `</tbody></table>`
  44. document.querySelector('#selectTable').innerHTML = newHtml
  45. document.querySelector('#selectTable').style.display = 'block'
  46. setTimeout(() => {
  47. document.querySelectorAll('table input').forEach(element => {
  48. element.onchange = function (e) {
  49. const itemId = e.target.getAttribute("key")
  50. bg.openList[itemId] = !bg.openList[itemId]
  51. }
  52. });
  53. }, 0);
  54. })
  55. }