popup.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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": 8,
  23. "url": tabInfo.url
  24. }),
  25. redirect: 'follow'
  26. }).then(data => data.json()).then(tabData => {
  27. tabData.tabInfo = tabInfo
  28. resolve(tabData)
  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((tabData) => {
  69. // console.log(tabData)
  70. const scriptBox = document.getElementsByClassName('script-box')[0]
  71. const data = tabData['data']
  72. if (tabData.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. chrome.scripting.executeScript({
  103. target: { tabId: tabData.tabInfo.id },
  104. func: () => {
  105. assistLoad()
  106. }
  107. });
  108. break
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }, 0);
  115. }
  116. })
  117. })
  118. }
  119. load();
  120. // document.getElementsByClassName('.no-script')[0].addEventListener("click", function(){
  121. // load()
  122. // })