R2T Add-on

在Google Maps上添加一个按钮,直接将当前路径发送到Route2TXT

  1. // ==UserScript==
  2. // @name R2T Add-on
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2.2
  5. // @description 在Google Maps上添加一个按钮,直接将当前路径发送到Route2TXT
  6. // @author Kai
  7. // @match *://*.google.com/maps*
  8. // @match *://*.google.*/*map*
  9. // @match *://*.google.*.*/*map*
  10. // @match *://*.google.com.hk/*map*
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. let childWindow = null;
  20.  
  21. function sendMessageToChild(message) {
  22. if (childWindow) {
  23. childWindow.postMessage(message, 'https://r2t.h0.work/');
  24. console.log("Sent");
  25. }
  26. }
  27.  
  28. function createButton(targetContainer) {
  29. // 检查是否已经插入按钮
  30. if (targetContainer.querySelector('.custom-arrow-button')) return;
  31.  
  32. // 创建新的按钮元素
  33. const newButton = document.createElement('button');
  34. newButton.className = 'J45yZc custom-arrow-button'; // 添加标识类,避免重复插入
  35. newButton.setAttribute('aria-label', '新しいボタン');
  36. newButton.setAttribute('jsaction', 'click:newButtonClick');
  37.  
  38. // 创建新的图标 (SVG)
  39. const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  40. svgIcon.setAttribute('t', '1734341356088');
  41. svgIcon.setAttribute('class', 'icon');
  42. svgIcon.setAttribute('viewBox', '0 0 1024 1024');
  43. svgIcon.setAttribute('version', '1.1');
  44. svgIcon.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  45. svgIcon.setAttribute('p-id', '2495');
  46. svgIcon.setAttribute('width', '21');
  47. svgIcon.setAttribute('height', '21');
  48.  
  49. const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  50. path.setAttribute('d', 'M832 640h-192c-35.2 0-64-28.8-64-64s28.8-64 64-64h192s192-214 192-320-86-192-192-192-192 86-192 192c0 51 44.4 126.8 90.6 192H640c-105.8 0-192 86.2-192 192s86.2 192 192 192h192c35.2 0 64 28.8 64 64s-28.8 64-64 64H371c-32 49.6-67.6 95.4-94.6 128H832c105.8 0 192-86.2 192-192s-86.2-192-192-192z m0-512c35.4 0 64 28.6 64 64s-28.6 64-64 64-64-28.6-64-64 28.6-64 64-64zM192 512c-106 0-192 86-192 192s192 320 192 320 192-214 192-320-86-192-192-192z m0 256c-35.4 0-64-28.6-64-64s28.6-64 64-64 64 28.6 64 64-28.6 64-64 64z');
  51. path.setAttribute('p-id', '2496');
  52.  
  53. svgIcon.appendChild(path);
  54. newButton.appendChild(svgIcon);
  55.  
  56. // 为按钮添加点击事件
  57. newButton.addEventListener('click', function() {
  58. // 检查子窗口是否已经打开
  59. if (!childWindow || childWindow.closed) {
  60. childWindow = window.open('https://r2t.h0.work/', '_blank');
  61. } else {
  62. childWindow.focus();
  63. }
  64.  
  65. // 模拟 Ctrl+A 并复制当前所有文本
  66. const allText = document.body.innerText; // 获取当前页面的所有文本
  67.  
  68. // 延迟 0.5 秒发送文本到子窗口
  69. setTimeout(() => {
  70. sendMessageToChild(allText);
  71. }, 500);
  72. });
  73.  
  74. // 将按钮插入到目标容器
  75. const firstButton = targetContainer.firstChild;
  76. targetContainer.insertBefore(newButton, firstButton);
  77. }
  78.  
  79. // 使用 MutationObserver 监控 DOM 变化
  80. function observeDOM() {
  81. const observer = new MutationObserver(() => {
  82. const targetContainer = document.querySelector('.MyVbZc.Hk4XGb');
  83. if (targetContainer) {
  84. createButton(targetContainer);
  85. }
  86. });
  87.  
  88. observer.observe(document.body, {
  89. childList: true,
  90. subtree: true,
  91. });
  92. }
  93.  
  94. // 启动观察器
  95. observeDOM();
  96. })();