popup.js 7.9 KB

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