tgWebAppData

复制tgWebAppData

  1. // ==UserScript==
  2. // @name tgWebAppData
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 复制tgWebAppData
  6. // @match *://*.telegram.org/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12.  
  13. let button = null; // To hold the button element
  14.  
  15. // Function to create and insert the copy button
  16. function createCopyButton(src) {
  17. // Check if the button already exists
  18. if (button) {
  19. return;
  20. }
  21.  
  22. // Create button element
  23. button = document.createElement('button');
  24. button.id = 'copyTgWebAppDataBtn';
  25. button.innerText = '复制 tgWebAppData';
  26. button.style.position = 'fixed';
  27. button.style.top = '10px';
  28. button.style.right = '10px';
  29. button.style.padding = '10px';
  30. button.style.backgroundColor = '#007BFF'; // Blue color
  31. button.style.color = 'white';
  32. button.style.border = 'none';
  33. button.style.borderRadius = '5px';
  34. button.style.cursor = 'pointer';
  35. button.style.zIndex = '10000'; // High z-index to ensure it's on top
  36. button.style.fontSize = '16px'; // Font size
  37.  
  38. // Append button to the body
  39. document.body.appendChild(button);
  40.  
  41. // Add click event to copy the formatted src value
  42. button.addEventListener('click', () => {
  43. // Extract parameters from src and format them
  44. const formattedData = extractTgWebAppData(src);
  45. navigator.clipboard.writeText(formattedData).then(() => {
  46. alert('tgWebAppData 复制成功!');
  47. }).catch(err => {
  48. console.error('Failed to copy text: ', err);
  49. });
  50. });
  51. }
  52.  
  53. // Function to remove the copy button
  54. function removeCopyButton() {
  55. if (button) {
  56. button.remove();
  57. button = null;
  58. }
  59. }
  60.  
  61. // Function to handle iframe visibility and button actions
  62. function handleIframes() {
  63. const iframes = document.querySelectorAll('iframe.zA1w1IOq');
  64. if (iframes.length > 0) {
  65. // Extract the src attribute value from the first matching iframe
  66. const src = iframes[0].src;
  67. const tgWebAppData = extractTgWebAppData(src);
  68.  
  69. // Check if tgWebAppData contains "query_id"
  70. if (tgWebAppData.includes('query_id')) {
  71. createCopyButton(src);
  72. } else {
  73. removeCopyButton();
  74. }
  75. } else {
  76. removeCopyButton();
  77. }
  78. }
  79.  
  80. // Function to extract tgWebAppData parameter without decoding
  81. function extractTgWebAppData(src) {
  82. const params = new URL(src).hash.substring(1); // Remove the leading '#'
  83. const dataParams = new URLSearchParams(params);
  84. const tgWebAppData = dataParams.get('tgWebAppData');
  85. return tgWebAppData ? tgWebAppData : 'No tgWebAppData found';
  86. }
  87.  
  88. // Use MutationObserver to handle dynamically loaded iframes
  89. const observer = new MutationObserver(() => {
  90. handleIframes();
  91. });
  92.  
  93. // Start observing the document body for changes
  94. observer.observe(document.body, { childList: true, subtree: true });
  95.  
  96. // Initial check when the page loads
  97. window.addEventListener('load', handleIframes);
  98. })();