popup.js 7.4 KB

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