popup.js 5.9 KB

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