options.js 2.7 KB

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