popup.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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": 7,
  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. const data = dataTemp['data']
  72. if (dataTemp.err !== 0 || data.length == 0) {
  73. scriptBox.classList.add('no-scheme')
  74. scriptBox.classList.remove('scheme')
  75. } else {
  76. dataCopy = data
  77. let buttonHtml = ''
  78. let ind = 0
  79. data.forEach(element => {
  80. buttonHtml += `<button data-ind="${ind}">${element.name}</button>`
  81. ind++
  82. });
  83. document.querySelector('.button-box').innerHTML = buttonHtml
  84. scriptBox.classList.add('scheme')
  85. setTimeout(() => {
  86. const buttonList = document.getElementsByTagName('button')
  87. for (const key in buttonList) {
  88. if (Object.hasOwnProperty.call(buttonList, key)) {
  89. const element = buttonList[key];
  90. element.onclick = function () {
  91. let index = this.getAttribute("data-ind")
  92. index = parseInt(index)
  93. let dataTempCopy = dataCopy[index]
  94. switch (dataTempCopy.type) {
  95. case 'run': {
  96. chrome.notifications.create(null, {
  97. type: 'basic',
  98. iconUrl: 'img/48.png',
  99. title: dataTempCopy.name,
  100. message: '远程方案已载入并运行!'
  101. })
  102. // console.log(unescape(dataTemp.data))
  103. function decodeOwo (value) {
  104. value = owoReplaceAll(value, "'", 'owovar1')
  105. value = owoReplaceAll(value, "`", 'owovar2')
  106. value = owoReplaceAll(value, '"', 'owovar3')
  107. value = owoReplaceAll(value, '$', 'owovar4')
  108. return value
  109. }
  110. if (dataTempCopy.data) {
  111. let execTwmp = `
  112. function owoReplaceAll(str, s1, s2) {
  113. while (str.indexOf(s1) >= 0) {
  114. str = str.replace(s1, s2)
  115. }
  116. return str
  117. }
  118. function clearOwo (value) {
  119. value = owoReplaceAll(value, 'owovar1', "'")
  120. value = owoReplaceAll(value, 'owovar2', "\`")
  121. value = owoReplaceAll(value, 'owovar3', '"')
  122. value = owoReplaceAll(value, 'owovar4', '$')
  123. return value
  124. }
  125. var script = document.createElement("script");
  126. script.type = "text/javascript";
  127. script.charset = "UTF-8";
  128. script.innerHTML = clearOwo(\`${decodeOwo(dataTempCopy.data)}\`);
  129. document.body.appendChild(script)
  130. `
  131. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: execTwmp})
  132. }
  133. if (dataTempCopy.style || dataTempCopy.script) {
  134. dataTempCopy.style = dataTempCopy.style || '[]'
  135. dataTempCopy.script = dataTempCopy.script || '[]'
  136. let temp = `
  137. function loadScript(url, callback) {
  138. var script = document.createElement("script")
  139. script.type = "text/javascript";
  140. if (script.readyState) { //IE
  141. script.onreadystatechange = function () {
  142. if (script.readyState == "loaded" || script.readyState == "complete") {
  143. script.onreadystatechange = null;
  144. if (callback) callback();
  145. }
  146. };
  147. } else { //Others
  148. script.onload = function () {
  149. if (callback) callback();
  150. };
  151. }
  152. script.src = url;
  153. var head = document.head || document.getElementsByTagName('head')[0];
  154. head.appendChild(script);
  155. }
  156. function loadCSS (url) {
  157. var link = document.createElement("link");
  158. link.rel = "stylesheet";
  159. link.type = "text/css";
  160. link.href = url;
  161. document.getElementsByTagName("head")[0].appendChild(link);
  162. }
  163. ${dataTempCopy.style}.forEach(element => {
  164. loadCSS(element)
  165. });
  166. ${dataTempCopy.script}.forEach(element => {
  167. loadScript(element)
  168. });
  169. `
  170. chrome.tabs.executeScript(dataTemp.tabInfo.id, {code: unescape(temp)})
  171. }
  172. break
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }, 0);
  179. }
  180. })
  181. })
  182. }
  183. load();
  184. // document.getElementsByClassName('.no-script')[0].addEventListener("click", function(){
  185. // load()
  186. // })