popup.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = {}
  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('userInfo')[0].onclick = function () {
  31. if (!userInfo || !userInfo.username || !userInfo.password || !userInfo.session) {
  32. window.open(chrome.extension.getURL('options.html'), "_blank")
  33. } else {
  34. chrome.storage.sync.set({userInfo: {}})
  35. location.reload();
  36. }
  37. }
  38. let dataCopy = null
  39. chrome.storage.sync.get('userInfo', function(data) {
  40. console.log(data)
  41. userInfo = data.userInfo
  42. if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
  43. document.querySelector('.userInfo').innerHTML = `${userInfo.username}`
  44. }
  45. // alert("页面加载完成!");
  46. // 获取是否有脚本
  47. getSchemeData.then((dataTemp) => {
  48. console.log(dataTemp)
  49. const scriptBox = document.getElementsByClassName('script-box')[0]
  50. if (dataTemp.edition > 1) {
  51. chrome.notifications.create(null, {
  52. type: 'basic',
  53. iconUrl: 'img/48.png',
  54. title: '版本过低',
  55. message: '有新版本请在弹出页面下载最新插件!'
  56. })
  57. // console.log(unescape(dataTemp.data))
  58. chrome.tabs.create({url: dataTemp.url})
  59. return
  60. }
  61. const data = dataTemp['data']
  62. if (dataTemp.err !== 0 || data.length == 0) {
  63. scriptBox.classList.add('no-scheme')
  64. scriptBox.classList.remove('scheme')
  65. } else {
  66. dataCopy = data
  67. let buttonHtml = ''
  68. let ind = 0
  69. data.forEach(element => {
  70. buttonHtml += `<button data-ind="${ind}">${element.name}</button>`
  71. ind++
  72. });
  73. document.querySelector('.button-box').innerHTML = buttonHtml
  74. scriptBox.classList.add('scheme')
  75. setTimeout(() => {
  76. const buttonList = document.getElementsByTagName('button')
  77. for (const key in buttonList) {
  78. if (Object.hasOwnProperty.call(buttonList, key)) {
  79. const element = buttonList[key];
  80. element.onclick = function () {
  81. let index = this.getAttribute("data-ind")
  82. index = parseInt(index)
  83. let dataTempCopy = dataCopy[index]
  84. switch (dataTempCopy.type) {
  85. case 'run': {
  86. chrome.notifications.create(null, {
  87. type: 'basic',
  88. iconUrl: 'img/48.png',
  89. title: dataTempCopy.name,
  90. message: '远程方案已载入并运行!'
  91. })
  92. // console.log(unescape(dataTemp.data))
  93. if (dataTempCopy.data) {
  94. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(dataTempCopy.data)})
  95. }
  96. if (dataTempCopy.style || dataTempCopy.script) {
  97. dataTempCopy.style = dataTempCopy.style || '[]'
  98. dataTempCopy.script = dataTempCopy.script || '[]'
  99. let temp = `
  100. function loadScript(url, callback) {
  101. var script = document.createElement("script")
  102. script.type = "text/javascript";
  103. if (script.readyState) { //IE
  104. script.onreadystatechange = function () {
  105. if (script.readyState == "loaded" || script.readyState == "complete") {
  106. script.onreadystatechange = null;
  107. if (callback) callback();
  108. }
  109. };
  110. } else { //Others
  111. script.onload = function () {
  112. if (callback) callback();
  113. };
  114. }
  115. script.src = url;
  116. var head = document.head || document.getElementsByTagName('head')[0];
  117. head.appendChild(script);
  118. }
  119. function loadCSS (url) {
  120. var link = document.createElement("link");
  121. link.rel = "stylesheet";
  122. link.type = "text/css";
  123. link.href = url;
  124. document.getElementsByTagName("head")[0].appendChild(link);
  125. }
  126. ${dataTempCopy.style}.forEach(element => {
  127. loadCSS(element)
  128. });
  129. ${dataTempCopy.script}.forEach(element => {
  130. loadScript(element)
  131. });
  132. `
  133. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(temp)})
  134. }
  135. break
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }, 0);
  142. }
  143. })
  144. })