popup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. const getSchemeData = new Promise((resolve, reject) => {
  13. getCurrentTabId((tabInfo) => {
  14. setTimeout(() => {
  15. const myHeaders = new Headers();
  16. myHeaders.append("Content-Type", "application/json");
  17. fetch(`${serverUrl}?route=search`, {
  18. method: 'POST',
  19. headers: myHeaders,
  20. body: JSON.stringify({
  21. "edition": 8,
  22. "url": tabInfo.url
  23. }),
  24. redirect: 'follow'
  25. }).then(data => data.json()).then(tabData => {
  26. tabData.tabInfo = tabInfo
  27. resolve(tabData)
  28. })
  29. }, 100);
  30. })
  31. })
  32. // 获取当前选项卡ID
  33. function getCurrentTabId(callback) {
  34. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  35. if(callback) callback(tabs.length ? tabs[0]: null);
  36. })
  37. }
  38. let dataCopy = null
  39. function load() {
  40. // 获取是否有脚本
  41. getSchemeData.then((tabData) => {
  42. // console.log(tabData)
  43. const scriptBox = document.getElementsByClassName('script-box')[0]
  44. const data = tabData['data']
  45. if (tabData.err !== 0 || data.length == 0) {
  46. scriptBox.classList.add('no-scheme')
  47. scriptBox.classList.remove('scheme')
  48. } else {
  49. dataCopy = data
  50. let buttonHtml = ''
  51. let ind = 0
  52. data.forEach(element => {
  53. buttonHtml += `<button data-ind="${ind}">${element.name}</button>`
  54. ind++
  55. });
  56. document.querySelector('.button-box').innerHTML = buttonHtml
  57. scriptBox.classList.add('scheme')
  58. setTimeout(() => {
  59. const buttonList = document.getElementsByTagName('button')
  60. for (const key in buttonList) {
  61. if (Object.hasOwnProperty.call(buttonList, key)) {
  62. const element = buttonList[key];
  63. element.onclick = function () {
  64. let index = this.getAttribute("data-ind")
  65. index = parseInt(index)
  66. let dataTempCopy = dataCopy[index]
  67. switch (dataTempCopy.type) {
  68. case 'run': {
  69. // 判断是否在js主世界执行
  70. if (dataTempCopy.id == '378') {
  71. chrome.scripting.executeScript({
  72. world: 'MAIN', // 在页面主世界中执行
  73. target: { tabId: tabData.tabInfo.id },
  74. files: ['./scripts/378.js']
  75. });
  76. } else {
  77. chrome.scripting.executeScript({
  78. target: { tabId: tabData.tabInfo.id },
  79. files: ['insert-popup.js']
  80. });
  81. }
  82. chrome.notifications.create(null, {
  83. type: 'basic',
  84. iconUrl: 'img/48.png',
  85. title: dataTempCopy.name,
  86. message: '远程方案已载入并运行!'
  87. })
  88. break
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }, 0);
  95. }
  96. })
  97. }
  98. load();
  99. document.getElementsByClassName('lxkf')[0].addEventListener("click", function(){
  100. console.log('点击客服标签')
  101. window.open('https://work.weixin.qq.com/kfid/kfc7a6930ede9575277')
  102. })