options.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //在popup.js 中调用 backgourd.js 中的变量和方法,很重要
  2. var bg = chrome.extension.getBackgroundPage();
  3. function login (username, password) {
  4. fetch(`https://user.hanshu.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. let newHtml = `<table border="0"><thead><tr><th>编号</th><th>脚本名称</th><th>开启状态</th></tr></thead><tbody>`
  21. fetch(`https://hanshu.run/assist?route=getList&username=${username}&session=${bg.userInfo.session}`).then((response) => {return response.json();}).then((res) => {
  22. console.log(bg.openList)
  23. res.data.forEach(element => {
  24. 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>`
  25. });
  26. newHtml += `</tbody></table>`
  27. document.querySelector('#selectTable').innerHTML = newHtml
  28. document.querySelector('#selectTable').style.display = 'block'
  29. setTimeout(() => {
  30. document.querySelectorAll('table input').forEach(element => {
  31. element.onchange = function (e) {
  32. const itemId = e.target.getAttribute("key")
  33. bg.openList[itemId] = !bg.openList[itemId]
  34. bg.saveOpenList()
  35. }
  36. });
  37. }, 0);
  38. })
  39. }
  40. })
  41. }
  42. chrome.storage.sync.get('userInfo', function(data) {
  43. console.log(data)
  44. const userInfo = data.userInfo
  45. if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
  46. login(userInfo.username, userInfo.password)
  47. }
  48. })
  49. // 登录按钮点击
  50. document.getElementsByClassName('login-button')[0].onclick = function () {
  51. const username = document.getElementById('username').value
  52. const password = document.getElementById('password').value
  53. if (!username || !password) {
  54. chrome.notifications.create(null, {
  55. type: 'basic',
  56. iconUrl: 'img/48.png',
  57. title: '运行方案',
  58. message: '用户名或密码不能为空!'
  59. })
  60. return
  61. }
  62. login(username, password)
  63. }
  64. document.getElementsByClassName('login-button')[1].onclick = function () {
  65. window.open('https://demos.run/debuger/index.html?view-loginContent=register#login')
  66. }