content-script.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. function assistLoad() {
  2. // 页面为加载完毕就不断等待
  3. if (!document.body) {
  4. setTimeout(() => {
  5. assistLoad()
  6. }, 500);
  7. return
  8. }
  9. // 创建div元素
  10. const newDiv = document.createElement('div');
  11. // 设置div的内容和属性
  12. newDiv.id = 'assistBox';
  13. newDiv.className = 'assist-box';
  14. newDiv.innerHTML = `
  15. <img style="position: absolute;top: -6px;left: -13px;" onclick="window.assistBox.style.right = (window.assistBox.style.right == '-301px' ? '0' : '-301px')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAA8CAYAAACw00AzAAABtklEQVRIie3WvU7bUBTA8b9vrKSGhKZqyAALWToQVYWhElM3pDbiG8aqcx+Al+jAAzCwdar6QVDLK3SgMDAa0g0JlGTDBmOw0bUasOHGyV2hZ7LO/d2jc+61ZPPQw5Dz2Qd/u405CbwD3piZzIDZRb0APgELnYTn+6jwEvAFyMSTGSGO7uIa8K1bTyL2/Bz4mXZHcbzRGbgXHgHm02Acv+8F43hKB4/qYEsHX+ngvuI/VuIgCMhms+Ryueg5FUvYbDZxXZdCoaDccIPzg4Mcn5ywWd+KXmrVhts2wpBnxSKNRoOv339gmib5fD6xITFgGIaUSsPYts1mvR7l5AwppxFSLpfZ+bPLr+1tBiwLwzDUWAiB4zg8HRqiMlbhwvdv1kwVNAzB8vIi1fFxWq3W/TYkdN0zzs895mZneFmtRlDOcQ9f+j5BGFCrvWVi4hXNf7DTb6KNU8ehUhnDemLRbrejI4vDRGVZRRgCz/OUMFFZLnYuQAWVR5cWjwYnvh+98IVO5WMdvKeDP+tgG/jdL5bxQQcfAh/7xTLWgdV+sYw1YBrYv5Mv9frfWAHmgNeA3w09wgCuAWMViFVtipj2AAAAAElFTkSuQmCC">
  16. `;
  17. newDiv.style.cssText = `
  18. position: fixed;
  19. right: 0;
  20. bottom: 20px;
  21. border: 1px solid #ccc;
  22. background-color: rgba(255, 255,255, 0.8);
  23. z-index: 99;
  24. transition: right 1s ease;
  25. `;
  26. // 将div插入到body的末尾
  27. document.body.appendChild(newDiv);
  28. // 创建 iframe 并插入页面
  29. const iframe = document.createElement('iframe');
  30. iframe.src = 'https://demos.run/assist/index.html'; // 注意:必须同源或对方允许跨源
  31. iframe.style.cssText = `width: 300px;height: 400px;border: none;`
  32. setTimeout(() => {
  33. window.assistBox.appendChild(iframe);
  34. }, 0);
  35. // 向 iframe 发送消息
  36. iframe.onload = () => {
  37. iframe.contentWindow.postMessage({
  38. type: "onload",
  39. value: location.href
  40. }, '*');
  41. };
  42. function sendCallBack(callBackID, value) {
  43. iframe.contentWindow.postMessage({
  44. type: "callBack",
  45. value,
  46. callBackID
  47. }, '*');
  48. }
  49. // 发送和接收页面
  50. const channel = new BroadcastChannel('tab_channel');
  51. // 接收消息
  52. channel.onmessage = function(event) {
  53. console.log('同源窗口消息:', event.data);
  54. if (event.data.type === 'assistTabMessage') {
  55. iframe.contentWindow.postMessage({
  56. type: "assistTabMessage",
  57. value: event.data.value,
  58. url: location.href
  59. }, '*');
  60. }
  61. };
  62. // 接收子页面发来的消息
  63. window.addEventListener('message', (event) => {
  64. // console.log('来自子页面的消息:', event.data);
  65. switch (event.data.type) {
  66. // assistMsg('click','#shi')
  67. case "click":
  68. if (typeof event.data.value === "string") {
  69. document.querySelector(event.data.value).click()
  70. } else {
  71. document.querySelectorAll(event.data.value[0])[event.data.value[1]].click()
  72. }
  73. break;
  74. // assistMsg('fetch', {
  75. // url: "https://obfuscator.lamp.run",
  76. // method: "POST",
  77. // body: "{\"code\":\"document.querySelector('.conn-box .show').innerHTML='gf'\"}",
  78. // redirect: "follow"
  79. // })
  80. case "querySelectorAll":
  81. let returnList = []
  82. document.querySelectorAll(event.data.value).forEach(element => {
  83. returnList.push({
  84. id: element.id,
  85. className: element.className,
  86. })
  87. });
  88. sendCallBack(event.data.callBackID, returnList)
  89. break;
  90. case "fetch":
  91. fetch(event.data.value.url, event.data.value)
  92. .then((response) => response.text())
  93. .then((result) => {
  94. console.log(result)
  95. if (event.data.callBackID) {
  96. sendCallBack(event.data.callBackID, response)
  97. }
  98. })
  99. .catch((error) => console.error(error));
  100. break;
  101. case "closeWindow":
  102. window.close();
  103. break;
  104. case "back":
  105. history.back();
  106. break;
  107. case "localStorageGetItem":
  108. sendCallBack(event.data.callBackID, localStorage.getItem(event.data.value))
  109. break;
  110. case "localStorageSetItem":
  111. localStorage.setItem(event.data.value[0], event.data.value[1])
  112. break;
  113. case "removeAttribute":
  114. document.querySelectorAll(event.data.value[0]).forEach(element => {
  115. element.removeAttribute(event.data.value[1])
  116. });
  117. break;
  118. case "sendTabMessage":
  119. // 发送消息
  120. channel.postMessage({type: 'assistTabMessage', value: event.data.value});
  121. break;
  122. default:
  123. break;
  124. }
  125. });
  126. }
  127. console.log('加载成功!')