popup.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const serverUrl = 'https://going.run/assist'
  2. const getSchemeData = new Promise((resolve, reject) => {
  3. getCurrentTabId((tabInfo) => {
  4. fetch(`${serverUrl}?route=search`, {
  5. method: 'POST',
  6. body: JSON.stringify({
  7. "edition": 2,
  8. "url": tabInfo.url
  9. }),
  10. redirect: 'follow'
  11. }).then(data => data.json()).then(dataTemp => {
  12. dataTemp.tabInfo = tabInfo
  13. resolve(dataTemp)
  14. })
  15. })
  16. })
  17. let userInfo = localStorage.getItem('userInfo')
  18. if (userInfo) {
  19. userInfo = JSON.parse(userInfo)
  20. // document.getElementsByClassName('user')[0].innerHTML = `金币: ${userInfo.gold}`
  21. document.getElementsByClassName('login')[0].getElementsByClassName.display = 'none'
  22. }
  23. // 获取当前选项卡ID
  24. function getCurrentTabId(callback) {
  25. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  26. if(callback) callback(tabs.length ? tabs[0]: null);
  27. })
  28. }
  29. // 登录按钮登录
  30. document.getElementsByClassName('login')[0].onclick = function () {
  31. window.open(chrome.extension.getURL('options.html'))
  32. }
  33. let dataCopy = null
  34. window.onload = function() {
  35. // alert("页面加载完成!");
  36. // 获取是否有脚本
  37. getSchemeData.then((dataTemp) => {
  38. console.log(dataTemp)
  39. const scriptBox = document.getElementsByClassName('script-box')[0]
  40. if (dataTemp.edition > 1) {
  41. chrome.notifications.create(null, {
  42. type: 'basic',
  43. iconUrl: 'img/48.png',
  44. title: '版本过低',
  45. message: '有新版本请在弹出页面下载最新插件!'
  46. })
  47. // console.log(unescape(dataTemp.data))
  48. chrome.tabs.create({url: dataTemp.url})
  49. return
  50. }
  51. if (dataTemp.err !== 0) {
  52. scriptBox.classList.add('no-scheme')
  53. } else {
  54. const data = dataTemp['data']
  55. dataCopy = data
  56. let buttonHtml = ''
  57. let ind = 0
  58. data.forEach(element => {
  59. ind++
  60. buttonHtml += `<button data-ind="${ind}">${element.name}</button>`
  61. });
  62. document.querySelector('.button-box').innerHTML = buttonHtml
  63. scriptBox.classList.add('scheme')
  64. setTimeout(() => {
  65. const buttonList = document.getElementsByTagName('button')
  66. for (const key in buttonList) {
  67. if (Object.hasOwnProperty.call(buttonList, key)) {
  68. const element = buttonList[key];
  69. element.onclick = function () {
  70. let index = this.getAttribute("data-ind")
  71. index = parseInt(index)
  72. let dataTempCopy = dataCopy[index]
  73. switch (dataTempCopy.type) {
  74. case 'run': {
  75. chrome.notifications.create(null, {
  76. type: 'basic',
  77. iconUrl: 'img/48.png',
  78. title: '运行方案',
  79. message: '远程方案以载入并运行!'
  80. })
  81. // console.log(unescape(dataTemp.data))
  82. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(dataTempCopy.data)})
  83. break
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }, 0);
  90. }
  91. })
  92. }