popup.js 7.6 KB

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