|
2 | 2 |
|
3 | 3 | import { useEffect } from 'react'; |
4 | 4 |
|
5 | | -export function BuyMeCoffeeWidget() { |
| 5 | +export function BuyMeACoffeeWidget() { |
6 | 6 | useEffect(() => { |
7 | | - // Remove any existing BMC scripts first |
8 | | - const existingScripts = document.querySelectorAll('script[data-name="BMC-Widget"]'); |
9 | | - existingScripts.forEach((script) => script.remove()); |
10 | | - |
11 | | - // Create and append the script element |
12 | 7 | const script = document.createElement('script'); |
13 | 8 | script.setAttribute('data-name', 'BMC-Widget'); |
14 | | - script.setAttribute('data-cfasync', 'false'); |
15 | | - script.setAttribute('src', 'https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js'); |
| 9 | + script.src = 'https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js'; |
16 | 10 | script.setAttribute('data-id', 'rahuldkjain'); |
17 | | - script.setAttribute('data-description', 'Support me on Buy me a coffee!'); |
18 | | - script.setAttribute('data-message', 'Loved the tool🚀. Buy me a coffee to support the work!'); |
19 | | - script.setAttribute('data-color', '#FF813F'); |
| 11 | + script.setAttribute('data-description', 'Support rahuldkjain on Buy me a coffee!'); |
| 12 | + script.setAttribute('data-message', ''); |
| 13 | + script.setAttribute('data-color', '#ffdd00'); |
20 | 14 | script.setAttribute('data-position', 'Right'); |
21 | 15 | script.setAttribute('data-x_margin', '18'); |
22 | 16 | script.setAttribute('data-y_margin', '18'); |
23 | | - |
24 | | - // Add onload handler to check if widget loaded |
25 | | - script.onload = () => { |
26 | | - console.log('BMC Widget script loaded successfully'); |
27 | | - // Try to trigger widget initialization if needed |
28 | | - setTimeout(() => { |
29 | | - if (typeof window.BMC !== 'undefined') { |
30 | | - console.log('BMC Widget initialized'); |
31 | | - } else { |
32 | | - console.log('BMC Widget not initialized, checking DOM...'); |
33 | | - } |
34 | | - }, 1000); |
35 | | - }; |
36 | | - |
37 | | - script.onerror = () => { |
38 | | - console.error('BMC Widget script failed to load'); |
| 17 | + script.async = true; |
| 18 | + |
| 19 | + script.onload = function () { |
| 20 | + const event = new CustomEvent('DOMContentLoaded', { |
| 21 | + bubbles: true, |
| 22 | + cancelable: true, |
| 23 | + }); |
| 24 | + window.dispatchEvent(event); |
39 | 25 | }; |
40 | 26 |
|
41 | 27 | document.head.appendChild(script); |
42 | 28 |
|
43 | | - // Cleanup function |
44 | 29 | return () => { |
45 | | - // Remove the script when component unmounts |
46 | | - const scriptToRemove = document.querySelector('script[data-name="BMC-Widget"]'); |
47 | | - if (scriptToRemove) { |
48 | | - scriptToRemove.remove(); |
| 30 | + document.head.removeChild(script); |
| 31 | + const widget = document.getElementById('bmc-wbtn'); |
| 32 | + if (widget) { |
| 33 | + document.body.removeChild(widget); |
49 | 34 | } |
50 | 35 | }; |
51 | 36 | }, []); |
52 | 37 |
|
53 | | - return null; // This component doesn't render anything visible |
| 38 | + return null; |
54 | 39 | } |
0 commit comments