popup.js 7.7 KB

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