popup.js 6.2 KB

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