quickreplywaweb.user.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // ==UserScript==
  2. // @name Quick Reply for WhatsApp Web +
  3. // @namespace https://github.com/laksa19/textexpander4whatsappweb
  4. // @version 0.2
  5. // @description Quick Reply for WhatsApp Web with Reply Button
  6. // @author Laksamadi Guko
  7. // @match https://web.whatsapp.com/*
  8. // @icon https://laksa19.github.io/textexpander4whatsappweb/favicon.png
  9. // @grant none
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13. // JSON configuration for buttons and messages
  14. const quickReplyConfig = [
  15. { "text": "Hello!", "message": "Hello! How can I help you today?" },
  16. { "text": "Thanks", "message": "Thank you for reaching out!" },
  17. { "text": "Bye", "message": "Goodbye! Have a great day!" }
  18. ];
  19. // Function to create and insert quick reply buttons
  20. function insertQuickReplyButtons() {
  21. // Check if the attach menu button exists
  22. let attachMenuButton = document.querySelector('span[data-icon="attach-menu-plus"]');
  23. if (!attachMenuButton) {
  24. console.error('Attach menu button not found');
  25. return;
  26. }
  27. // Check if the buttons already exist to avoid duplication
  28. if (document.getElementById('quickReplyButtonsContainer')) return;
  29. // Create a container for the buttons
  30. let container = document.createElement('div');
  31. container.id = 'quickReplyButtonsContainer';
  32. container.style.display = 'flex';
  33. container.style.flexWrap = 'wrap';
  34. container.style.gap = '10px';
  35. container.style.margin = '10px 0';
  36. container.style.justifyContent = 'center'; // Align buttons to the center
  37. // Create and append each button based on the JSON configuration
  38. quickReplyConfig.forEach(config => {
  39. let button = document.createElement('button');
  40. button.innerText = config.text;
  41. button.title = config.message; // Set the title attribute
  42. button.style.padding = '8px';
  43. button.style.backgroundColor = '#25D366';
  44. button.style.color = 'white';
  45. button.style.border = 'none';
  46. button.style.borderRadius = '5px';
  47. button.style.cursor = 'pointer';
  48. // Add click event listener to the button
  49. button.addEventListener('click', function() {
  50. let messageBox = document.querySelector("div[contenteditable='true'][data-tab='10']");
  51. let predefinedMessage = config.message;
  52. if (messageBox) {
  53. // Set the message in the input field
  54. messageBox.focus();
  55. document.execCommand('insertText', false, predefinedMessage);
  56. // Dispatch input event to notify React of the change
  57. let event = new InputEvent('input', {
  58. bubbles: true,
  59. cancelable: true,
  60. });
  61. messageBox.dispatchEvent(event);
  62. // Simulate sending the message
  63. setTimeout(function(){
  64. let sendButton = document.querySelector("span[data-icon='send']");
  65. if (sendButton) {
  66. sendButton.click();
  67. }
  68. }, 200);
  69. } else {
  70. console.error("Message box not found");
  71. }
  72. });
  73. // Append the button to the container
  74. container.appendChild(button);
  75. });
  76. // Find and prepend the container to the chat footer
  77. let footer = document.querySelector('footer');
  78. if (footer) {
  79. footer.prepend(container);
  80. } else {
  81. console.error("Footer not found");
  82. }
  83. }
  84. // Function to handle body clicks
  85. function handleBodyClick() {
  86. insertQuickReplyButtons();
  87. }
  88. // Add event listener to the body
  89. document.body.addEventListener('click', handleBodyClick);
  90. })();