popup.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. function owoReplaceAll(str, s1, s2) {
  7. while (str.indexOf(s1) >= 0) {
  8. str = str.replace(s1, s2)
  9. }
  10. return str
  11. }
  12. const getSchemeData = new Promise((resolve, reject) => {
  13. getCurrentTabId((tabInfo) => {
  14. setTimeout(() => {
  15. fetch(`${serverUrl}?route=search&username=${userInfo.username || 'nologin'}`, {
  16. method: 'POST',
  17. body: JSON.stringify({
  18. "edition": 5,
  19. "url": tabInfo.url
  20. }),
  21. redirect: 'follow'
  22. }).then(data => data.json()).then(dataTemp => {
  23. dataTemp.tabInfo = tabInfo
  24. resolve(dataTemp)
  25. })
  26. }, 100);
  27. })
  28. })
  29. let userInfo = {}
  30. // 获取当前选项卡ID
  31. function getCurrentTabId(callback) {
  32. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  33. if(callback) callback(tabs.length ? tabs[0]: null);
  34. })
  35. }
  36. // 登录按钮登录
  37. document.getElementsByClassName('userInfo')[0].onclick = function () {
  38. if (!userInfo || !userInfo.username || !userInfo.password || !userInfo.session) {
  39. // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  40. window.open(chrome.extension.getURL('options.html'), "_blank")
  41. } else {
  42. chrome.storage.sync.set({userInfo: {}})
  43. location.reload();
  44. }
  45. }
  46. document.getElementsByClassName('clearTemp')[0].onclick = function () {
  47. chrome.runtime.sendMessage({name:"clear"},function() {
  48. alert('缓存清除成功!')
  49. })
  50. }
  51. let dataCopy = null
  52. function load() {
  53. chrome.storage.sync.get('userInfo', function(data) {
  54. // alert(data)
  55. userInfo = data.userInfo
  56. if (userInfo && userInfo.username && userInfo.password && userInfo.session) {
  57. document.querySelector('.userInfo').innerHTML = `${userInfo.username}`
  58. }
  59. // alert("页面加载完成!");
  60. // 获取是否有脚本
  61. getSchemeData.then((dataTemp) => {
  62. // console.log(dataTemp)
  63. const scriptBox = document.getElementsByClassName('script-box')[0]
  64. if (dataTemp.edition > 5) {
  65. chrome.notifications.create(null, {
  66. type: 'basic',
  67. iconUrl: 'img/48.png',
  68. title: '版本过低',
  69. message: '有新版本请在弹出页面下载最新插件!'
  70. })
  71. // console.log(unescape(dataTemp.data))
  72. chrome.tabs.create({url: dataTemp.url})
  73. return
  74. }
  75. const data = dataTemp['data']
  76. if (dataTemp.err !== 0 || data.length == 0) {
  77. scriptBox.classList.add('no-scheme')
  78. scriptBox.classList.remove('scheme')
  79. } else {
  80. dataCopy = data
  81. let buttonHtml = ''
  82. let ind = 0
  83. data.forEach(element => {
  84. buttonHtml += `<button data-ind="${ind}">${element.name}</button>`
  85. ind++
  86. });
  87. document.querySelector('.button-box').innerHTML = buttonHtml
  88. scriptBox.classList.add('scheme')
  89. setTimeout(() => {
  90. const buttonList = document.getElementsByTagName('button')
  91. for (const key in buttonList) {
  92. if (Object.hasOwnProperty.call(buttonList, key)) {
  93. const element = buttonList[key];
  94. element.onclick = function () {
  95. let index = this.getAttribute("data-ind")
  96. index = parseInt(index)
  97. let dataTempCopy = dataCopy[index]
  98. switch (dataTempCopy.type) {
  99. case 'run': {
  100. chrome.notifications.create(null, {
  101. type: 'basic',
  102. iconUrl: 'img/48.png',
  103. title: dataTempCopy.name,
  104. message: '远程方案已载入并运行!'
  105. })
  106. // console.log(unescape(dataTemp.data))
  107. function decodeOwo (value) {
  108. value = owoReplaceAll(value, "'", 'owovar1')
  109. value = owoReplaceAll(value, "`", 'owovar2')
  110. value = owoReplaceAll(value, '"', 'owovar3')
  111. value = owoReplaceAll(value, '$', 'owovar4')
  112. return value
  113. }
  114. if (dataTempCopy.data) {
  115. let execTwmp = `
  116. function owoReplaceAll(str, s1, s2) {
  117. while (str.indexOf(s1) >= 0) {
  118. str = str.replace(s1, s2)
  119. }
  120. return str
  121. }
  122. function clearOwo (value) {
  123. value = owoReplaceAll(value, 'owovar1', "'")
  124. value = owoReplaceAll(value, 'owovar2', "\`")
  125. value = owoReplaceAll(value, 'owovar3', '"')
  126. value = owoReplaceAll(value, 'owovar4', '$')
  127. return value
  128. }
  129. var script = document.createElement("script");
  130. script.type = "text/javascript";
  131. script.charset = "UTF-8";
  132. script.innerHTML = clearOwo(\`${decodeOwo(dataTempCopy.data)}\`);
  133. document.body.appendChild(script)
  134. `
  135. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: execTwmp})
  136. }
  137. if (dataTempCopy.style || dataTempCopy.script) {
  138. dataTempCopy.style = dataTempCopy.style || '[]'
  139. dataTempCopy.script = dataTempCopy.script || '[]'
  140. let temp = `
  141. function loadScript(url, callback) {
  142. var script = document.createElement("script")
  143. script.type = "text/javascript";
  144. if (script.readyState) { //IE
  145. script.onreadystatechange = function () {
  146. if (script.readyState == "loaded" || script.readyState == "complete") {
  147. script.onreadystatechange = null;
  148. if (callback) callback();
  149. }
  150. };
  151. } else { //Others
  152. script.onload = function () {
  153. if (callback) callback();
  154. };
  155. }
  156. script.src = url;
  157. var head = document.head || document.getElementsByTagName('head')[0];
  158. head.appendChild(script);
  159. }
  160. function loadCSS (url) {
  161. var link = document.createElement("link");
  162. link.rel = "stylesheet";
  163. link.type = "text/css";
  164. link.href = url;
  165. document.getElementsByTagName("head")[0].appendChild(link);
  166. }
  167. ${dataTempCopy.style}.forEach(element => {
  168. loadCSS(element)
  169. });
  170. ${dataTempCopy.script}.forEach(element => {
  171. loadScript(element)
  172. });
  173. `
  174. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(temp)})
  175. }
  176. break
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }, 0);
  183. }
  184. })
  185. })
  186. }
  187. load()
  188. // document.getElementsByClassName('.no-script')[0].addEventListener("click", function(){
  189. // load()
  190. // })