popup.js 7.7 KB

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