popup.js 3.2 KB

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