1function getPageMetadata() { 2 const canonicalUrl = document.querySelector('meta[property="og:url"]')?.content || window.location.href; 3 4 const title = 5 document.querySelector('meta[property="og:title"]')?.content || 6 document.querySelector('meta[name="twitter:title"]')?.content || 7 document.title || 8 'Check out this article'; 9 10 const description = 11 document.querySelector('meta[property="og:description"]')?.content || 12 document.querySelector('meta[name="twitter:description"]')?.content || 13 document.querySelector('meta[name="description"]')?.content || 14 'Interesting article worth reading'; 15 16 const image = 17 document.querySelector('meta[property="og:image"]')?.content || 18 document.querySelector('meta[name="twitter:image"]')?.content || 19 document.querySelector('.blog img')?.src || 20 ''; 21 22 const cleanedMetadata = { 23 url: canonicalUrl.trim(), 24 title: title.trim(), 25 description: description.trim().replace(/\\n/g, ' ').replace(/\\s+/g, ' '), 26 image: image.trim() 27 }; 28 29 console.log('Page metadata extracted:', cleanedMetadata); 30 31 return { 32 url: encodeURIComponent(cleanedMetadata.url), 33 title: encodeURIComponent(cleanedMetadata.title), 34 description: encodeURIComponent(cleanedMetadata.description), 35 image: encodeURIComponent(cleanedMetadata.image) 36 }; 37} 38 39 40const shareBuilders = { 41 facebook(metadata) { 42 const url = decodeURIComponent(metadata.url); 43 console.log('Facebook sharing URL:', url); 44 return `https://www.facebook.com/sharer/sharer.php?u=${metadata.url}`; 45 }, 46 47 'twitter-x'(metadata) { 48 const text = `${decodeURIComponent(metadata.title)} - ${decodeURIComponent(metadata.description)}`; 49 const url = decodeURIComponent(metadata.url); 50 console.log('Twitter sharing - text:', text, 'url:', url); 51 return `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(text)}`; 52 }, 53 54 envelope(metadata) { 55 const subject = decodeURIComponent(metadata.title); 56 const description = decodeURIComponent(metadata.description); 57 const url = decodeURIComponent(metadata.url); 58 const body = `${description}\n\nRead more: ${url}`; 59 const mailtoUrl = `mailto:?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`; 60 console.log('Email sharing - subject:', subject, 'url:', url); 61 console.log('Mailto URL:', mailtoUrl); 62 return mailtoUrl; 63 } 64}; 65 66async function shareTo(platform) { 67 const metadata = getPageMetadata(); 68 69 if (navigator.share && (platform === 'envelope' || platform === 'native')) { 70 try { 71 await navigator.share({ 72 title: decodeURIComponent(metadata.title), 73 text: decodeURIComponent(metadata.description), 74 url: decodeURIComponent(metadata.url) 75 }); 76 console.log('â Native sharing successful'); 77 return 'native-share-success'; 78 } catch (shareError) { 79 console.log('Native sharing cancelled or failed:', shareError.message); 80 } 81 } 82 83 if (!shareBuilders[platform]) { 84 console.warn(`No sharing method defined for platform: ${platform}`); 85 return '#'; 86 } 87 88 try { 89 const shareUrl = shareBuilders[platform](metadata); 90 91 if (shareUrl && shareUrl !== '#' && !shareUrl.startsWith('mailto:')) { 92 console.log('Opening social platform URL:', shareUrl); 93 window.open(shareUrl, '_blank', 'width=600,height=500,scrollbars=yes,resizable=yes'); 94 } else if (shareUrl && shareUrl.startsWith('mailto:')) { 95 96 //console.log('Attempting email share with URL:', shareUrl); 97 98 99 try { 100 const emailWindow = window.open(shareUrl, '_blank', 'width=1,height=1'); 101 if (emailWindow) { 102 setTimeout(() => emailWindow.close(), 500); 103 } 104 //console.log('Email sharing: window.open method used'); 105 return shareUrl; 106 } catch (error) { 107 //console.log('Window.open method failed, trying direct location'); 108 } 109 110 try { 111 window.location.href = shareUrl; 112 console.log('â Email sharing: window.location method used'); 113 return shareUrl; 114 } catch (locationError) { 115 console.log('Direct location method failed, trying anchor click'); 116 } 117
118 try { 119 const anchor = document.createElement('a'); 120 anchor.href = shareUrl; 121 anchor.style.display = 'none'; 122 anchor.target = '_blank'; 123 document.body.appendChild(anchor); 124 anchor.click(); 125 document.body.removeChild(anchor); 126 console.log('â Email sharing: anchor click method used'); 127 return shareUrl; 128 } catch (anchorError) { 129 console.error('â All email sharing methods failed:', anchorError); 130 131 const subject = decodeURIComponent(metadata.title); 132 const body = decodeURIComponent(metadata.description); 133 const url = decodeURIComponent(metadata.url); 134 135 const emailContent = `Subject: ${subject}\n\nMessage:\n${body}\n\nRead more: ${url}`; 136 137 if (confirm(`Unable to open email client automatically.\n\nWould you like to copy the email content to your clipboard?`)) { 138 try { 139 await navigator.clipboard.writeText(emailContent); 140 alert('ð§ Email content copied to clipboard! You can now paste it into your email client.'); 141 } catch (clipboardError) { 142 alert(`ð§ Please copy this email content manually:\n\n${emailContent}`); 143 } 144 } 145 return shareUrl; 146 } 147 } 148 149 return shareUrl; 150 } catch (error) { 151 console.error(`Error sharing to ${platform}:`, error); 152 return '#'; 153 } 154} 155 156 157function socialShareComponent() { 158 return { 159 160 shareToSocial(platform, event) { 161 event.preventDefault(); 162 163 const button = event.currentTarget; 164 const originalTitle = button.title; 165 button.title = 'Sharing...'; 166 button.style.opacity = '0.7'; 167 168 try { 169 shareTo(platform); 170 } finally { 171 172 setTimeout(() => { 173 button.title = originalTitle; 174 button.style.opacity = '1'; 175 }, 500); 176 } 177 }, 178 179 getShareTitle(platform) { 180 const titles = { 181 facebook: 'Share on Facebook', 182 'twitter-x': 'Share on Twitter/X', 183 envelope: 'Share via Email' 184 }; 185 186 return titles[platform] || `Share on ${platform}`; 187 } 188 }; 189} 190 191export { socialShareComponent, shareTo, getPageMetadata }; 192export default socialShareComponent;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.