@x4r1s3/baileys 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1262 -2
  3. package/SECURITY.md +47 -0
  4. package/WAProto/V +1 -0
  5. package/WAProto/WAProto.proto +8892 -0
  6. package/WAProto/index.d.ts +104128 -0
  7. package/WAProto/index.js +334920 -0
  8. package/assets/rolling-circle.svg +133 -0
  9. package/assets/x4r1s3-banner.svg +32 -0
  10. package/engine-requirements.js +27 -0
  11. package/lib/Builders/A2UI.js +245 -0
  12. package/lib/Builders/AIRich.js +179 -0
  13. package/lib/Builders/Button.js +108 -0
  14. package/lib/Builders/CFC.js +284 -0
  15. package/lib/Builders/Carousel.js +81 -0
  16. package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
  17. package/lib/Call/audio-feeder.mjs +179 -0
  18. package/lib/Call/audio-recorder.mjs +250 -0
  19. package/lib/Call/fetch-wasm-resources.mjs +104 -0
  20. package/lib/Call/index.mjs +680 -0
  21. package/lib/Call/relay-transport.mjs +563 -0
  22. package/lib/Call/signaling.mjs +641 -0
  23. package/lib/Call/types.mjs +17 -0
  24. package/lib/Call/video-feeder.mjs +94 -0
  25. package/lib/Call/wasm-engine.mjs +1372 -0
  26. package/lib/Call/worker-bootstrap.mjs +1042 -0
  27. package/lib/Defaults/ban-checker.js +56 -0
  28. package/lib/Defaults/index.js +170 -0
  29. package/lib/Signal/Group/ciphertext-message.js +23 -0
  30. package/lib/Signal/Group/group-session-builder.js +41 -0
  31. package/lib/Signal/Group/group_cipher.js +93 -0
  32. package/lib/Signal/Group/index.js +23 -0
  33. package/lib/Signal/Group/keyhelper.js +29 -0
  34. package/lib/Signal/Group/sender-chain-key.js +37 -0
  35. package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
  36. package/lib/Signal/Group/sender-key-message.js +77 -0
  37. package/lib/Signal/Group/sender-key-name.js +59 -0
  38. package/lib/Signal/Group/sender-key-record.js +52 -0
  39. package/lib/Signal/Group/sender-key-state.js +95 -0
  40. package/lib/Signal/Group/sender-message-key.js +37 -0
  41. package/lib/Signal/libsignal.js +440 -0
  42. package/lib/Signal/lid-mapping.js +292 -0
  43. package/lib/Socket/Client/index.js +14 -0
  44. package/lib/Socket/Client/types.js +22 -0
  45. package/lib/Socket/Client/websocket.js +65 -0
  46. package/lib/Socket/aigroups.js +327 -0
  47. package/lib/Socket/ban-checker.js +165 -0
  48. package/lib/Socket/business.js +419 -0
  49. package/lib/Socket/chats.js +1921 -0
  50. package/lib/Socket/communities.js +443 -0
  51. package/lib/Socket/graphql.js +911 -0
  52. package/lib/Socket/groups.js +561 -0
  53. package/lib/Socket/index.js +49 -0
  54. package/lib/Socket/interactive-handler.js +612 -0
  55. package/lib/Socket/interop.js +575 -0
  56. package/lib/Socket/managed-account.js +220 -0
  57. package/lib/Socket/messages-recv.js +2191 -0
  58. package/lib/Socket/messages-send.js +4104 -0
  59. package/lib/Socket/mex.js +53 -0
  60. package/lib/Socket/newsletter.js +291 -0
  61. package/lib/Socket/privacy.js +458 -0
  62. package/lib/Socket/registration.js +440 -0
  63. package/lib/Socket/socket.js +1029 -0
  64. package/lib/Socket/status-actions.js +287 -0
  65. package/lib/Socket/username.js +241 -0
  66. package/lib/Store/index.js +16 -0
  67. package/lib/Store/make-cache-manager-store.js +101 -0
  68. package/lib/Store/make-in-memory-store.js +432 -0
  69. package/lib/Store/make-ordered-dictionary.js +90 -0
  70. package/lib/Store/object-repository.js +35 -0
  71. package/lib/Types/Auth.js +13 -0
  72. package/lib/Types/Bussines.js +13 -0
  73. package/lib/Types/Call.js +13 -0
  74. package/lib/Types/Chat.js +19 -0
  75. package/lib/Types/Contact.js +13 -0
  76. package/lib/Types/Events.js +13 -0
  77. package/lib/Types/GroupMetadata.js +13 -0
  78. package/lib/Types/Label.js +36 -0
  79. package/lib/Types/LabelAssociation.js +18 -0
  80. package/lib/Types/Message.js +30 -0
  81. package/lib/Types/Mex.js +53 -0
  82. package/lib/Types/Product.js +13 -0
  83. package/lib/Types/RichType.js +34 -0
  84. package/lib/Types/Signal.js +13 -0
  85. package/lib/Types/Socket.js +14 -0
  86. package/lib/Types/State.js +60 -0
  87. package/lib/Types/USync.js +13 -0
  88. package/lib/Types/index.js +37 -0
  89. package/lib/Utils/auth-utils.js +313 -0
  90. package/lib/Utils/ban-checker.js +48 -0
  91. package/lib/Utils/bot-capabilities.js +96 -0
  92. package/lib/Utils/bot-planning-replay.js +177 -0
  93. package/lib/Utils/browser-utils.js +40 -0
  94. package/lib/Utils/business.js +242 -0
  95. package/lib/Utils/c2582.js +147 -0
  96. package/lib/Utils/chat-utils.js +905 -0
  97. package/lib/Utils/companion-reg-client-utils.js +44 -0
  98. package/lib/Utils/consumer-application.js +114 -0
  99. package/lib/Utils/crypto.js +129 -0
  100. package/lib/Utils/decode-wa-message.js +496 -0
  101. package/lib/Utils/direct-chat-theme.js +164 -0
  102. package/lib/Utils/event-buffer.js +601 -0
  103. package/lib/Utils/experimental-proto.js +206 -0
  104. package/lib/Utils/games/miniapp.js +17 -0
  105. package/lib/Utils/games/slot-machine.js +270 -0
  106. package/lib/Utils/games/website-preview.js +306 -0
  107. package/lib/Utils/generics.js +402 -0
  108. package/lib/Utils/group-history.js +68 -0
  109. package/lib/Utils/group-wallpaper.js +168 -0
  110. package/lib/Utils/history.js +162 -0
  111. package/lib/Utils/identity-change-handler.js +60 -0
  112. package/lib/Utils/index.js +61 -0
  113. package/lib/Utils/jid-display-normalization.js +221 -0
  114. package/lib/Utils/link-preview.js +96 -0
  115. package/lib/Utils/logger.js +14 -0
  116. package/lib/Utils/lt-hash.js +19 -0
  117. package/lib/Utils/make-mutex.js +44 -0
  118. package/lib/Utils/message-composer.js +709 -0
  119. package/lib/Utils/message-inspect.js +79 -0
  120. package/lib/Utils/message-retry-manager.js +253 -0
  121. package/lib/Utils/message-security.js +167 -0
  122. package/lib/Utils/messages-media.js +1027 -0
  123. package/lib/Utils/messages.js +2467 -0
  124. package/lib/Utils/meta-ai-msmsg.js +215 -0
  125. package/lib/Utils/meta-compositing.js +239 -0
  126. package/lib/Utils/native-flow.js +158 -0
  127. package/lib/Utils/newsletter-live-update-utils.js +74 -0
  128. package/lib/Utils/newsletter-status-utils.js +152 -0
  129. package/lib/Utils/noise-handler.js +212 -0
  130. package/lib/Utils/offline-node-processor.js +51 -0
  131. package/lib/Utils/poll-manager.js +206 -0
  132. package/lib/Utils/portable-compat.js +12 -0
  133. package/lib/Utils/pre-key-manager.js +117 -0
  134. package/lib/Utils/process-message.js +610 -0
  135. package/lib/Utils/reporting-utils.js +269 -0
  136. package/lib/Utils/rich-message-utils.js +2841 -0
  137. package/lib/Utils/scheduled-message.js +107 -0
  138. package/lib/Utils/signal.js +212 -0
  139. package/lib/Utils/source-grep.js +57 -0
  140. package/lib/Utils/stanza-ack.js +49 -0
  141. package/lib/Utils/sticker-annotation.js +87 -0
  142. package/lib/Utils/sync-action-utils.js +59 -0
  143. package/lib/Utils/tc-token-utils.js +174 -0
  144. package/lib/Utils/use-multi-file-auth-state.js +132 -0
  145. package/lib/Utils/use-single-file-auth-state.js +108 -0
  146. package/lib/Utils/use-sqlite-auth-state.js +120 -0
  147. package/lib/Utils/validate-connection.js +220 -0
  148. package/lib/Utils/view-once-utils.js +218 -0
  149. package/lib/Utils/voip-rekey.js +32 -0
  150. package/lib/Utils/website-preview.js +27 -0
  151. package/lib/Utils/welcome-flow.js +228 -0
  152. package/lib/Utils/x4r1s3.js +144 -0
  153. package/lib/WABinary/constants.js +1475 -0
  154. package/lib/WABinary/decode.js +273 -0
  155. package/lib/WABinary/encode.js +231 -0
  156. package/lib/WABinary/generic-utils.js +242 -0
  157. package/lib/WABinary/index.js +17 -0
  158. package/lib/WABinary/jid-utils.js +107 -0
  159. package/lib/WABinary/types.js +13 -0
  160. package/lib/WAM/BinaryInfo.js +21 -0
  161. package/lib/WAM/constants.js +22864 -0
  162. package/lib/WAM/encode.js +161 -0
  163. package/lib/WAM/index.js +15 -0
  164. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
  165. package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
  166. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
  167. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
  168. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
  169. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
  170. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
  171. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
  172. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
  173. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
  174. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
  175. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
  176. package/lib/WAUSync/Protocols/index.js +24 -0
  177. package/lib/WAUSync/USyncQuery.js +213 -0
  178. package/lib/WAUSync/USyncUser.js +66 -0
  179. package/lib/WAUSync/index.js +15 -0
  180. package/lib/antiban.cjs +4726 -0
  181. package/lib/index.d.ts +1285 -0
  182. package/lib/index.js +30 -0
  183. package/package.json +125 -4
@@ -0,0 +1,306 @@
1
+ /**
2
+ * @X4R1S3
3
+ *
4
+ * File: lib/Utils/games/website-preview.js
5
+ * About: Website preview module for the X4R1S3 WhatsApp protocol client.
6
+ * Important exports: generateWebsitePreviewHtml, generateLinkPreviewHtml
7
+ * Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
8
+ *
9
+ * Copyright and attribution notice.
10
+ * Do not copy, reproduce, redistribute, modify, or create derivative works
11
+ * from this source without explicit written permission.
12
+ */
13
+ /**
14
+ * Website Preview — Mirror any website inside WhatsApp chat
15
+ *
16
+ * Fetches an external website and renders it directly inside the chat bubble
17
+ * using FOAHtmlPrimitiveDemoDONOTUSE. The content is inlined (no iframe)
18
+ * so it works in WhatsApp's sandboxed WebView.
19
+ *
20
+ * Usage:
21
+ * const { generateWebsitePreviewHtml } = require('./website-preview');
22
+ * const html = await generateWebsitePreviewHtml('https://example.com');
23
+ * await sock.sendHtmlMessage(jid, { html });
24
+ */
25
+
26
+ /**
27
+ * Fetch a website and return a self-contained HTML string
28
+ * that renders directly inside WhatsApp's chat bubble.
29
+ *
30
+ * @param {string} url - The website URL to mirror
31
+ * @param {Object} [options]
32
+ * @param {string} [options.height='600px'] - Preview height
33
+ * @param {boolean} [options.showUrl=true] - Show the URL bar
34
+ * @param {boolean} [options.showToolbar=true] - Show toolbar with open link button
35
+ * @param {string} [options.theme='dark'] - 'dark' or 'light'
36
+ * @returns {string} Self-contained HTML string
37
+ */
38
+ export const generateWebsitePreviewHtml = async (url, options = {}) => {
39
+ const { height = '600px', showUrl = true, showToolbar = true, theme = 'dark' } = options;
40
+ const baseUrl = new URL(url);
41
+ const isDark = theme === 'dark';
42
+
43
+ let fetchedHtml = '';
44
+ let fetchError = false;
45
+
46
+ try {
47
+ const response = await fetch(url, {
48
+ headers: {
49
+ 'User-Agent': 'Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36',
50
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
51
+ 'Accept-Language': 'en-US,en;q=0.5'
52
+ },
53
+ redirect: 'follow',
54
+ signal: AbortSignal.timeout(10000)
55
+ });
56
+ fetchedHtml = await response.text();
57
+ } catch (err) {
58
+ fetchError = true;
59
+ }
60
+
61
+ // Extract useful parts from fetched HTML
62
+ const extracted = extractContent(fetchedHtml, url);
63
+
64
+ const bg = isDark ? '#0a0a0a' : '#ffffff';
65
+ const text = isDark ? '#e0e0e0' : '#1a1a1a';
66
+ const textMuted = isDark ? '#888' : '#666';
67
+ const border = isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
68
+ const barBg = isDark ? '#1a1a2e' : '#f5f5f5';
69
+ const linkColor = '#4CAF50';
70
+
71
+ return `<html><head><style>
72
+ *{box-sizing:border-box;margin:0;padding:0}
73
+ html,body{width:100%;height:100%;overflow:hidden;background:${bg};font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif}
74
+ .webview{width:100%;height:${height};position:relative;border-radius:12px;overflow:hidden;border:1px solid ${border};background:${bg}}
75
+
76
+ /* URL Bar */
77
+ .url-bar{display:flex;align-items:center;gap:8px;padding:8px 12px;background:${barBg};border-bottom:1px solid ${border};font-size:12px}
78
+ .url-bar .lock{color:${linkColor};font-size:11px}
79
+ .url-bar .url{flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:${textMuted}}
80
+ .url-bar .refresh{cursor:pointer;color:${linkColor};padding:4px;font-size:14px}
81
+
82
+ /* Content Area */
83
+ .content{width:100%;height:calc(100% - ${showUrl ? '36px' : '0px'} - ${showToolbar ? '44px' : '0px'});overflow-y:auto;overflow-x:hidden;-webkit-overflow-scrolling:touch;scrollbar-width:thin;scrollbar-color:${isDark ? '#333' : '#ccc'} transparent}
84
+ .content::-webkit-scrollbar{width:4px}
85
+ .content::-webkit-scrollbar-thumb{background:${isDark ? '#333' : '#ccc'};border-radius:2px}
86
+
87
+ /* Extracted site content */
88
+ .site-title{font-size:18px;font-weight:700;color:${text};margin-bottom:8px}
89
+ .site-desc{font-size:13px;color:${textMuted};line-height:1.5;margin-bottom:12px}
90
+ .site-text{font-size:14px;color:${text};line-height:1.7}
91
+ .site-text a{color:${linkColor};text-decoration:none}
92
+ .site-text img{max-width:100%;height:auto;border-radius:8px;margin:8px 0;display:block}
93
+ .site-headings{font-size:15px;font-weight:600;color:${text};margin:16px 0 6px}
94
+ .site-list{padding-left:18px;font-size:13px;color:${text};line-height:1.8}
95
+ .site-links{display:flex;flex-wrap:wrap;gap:6px;margin-top:10px}
96
+ .site-link{display:inline-block;padding:4px 10px;background:${barBg};border:1px solid ${border};border-radius:6px;font-size:12px;color:${linkColor};text-decoration:none}
97
+ .site-images{display:grid;grid-template-columns:repeat(2,1fr);gap:6px;margin:10px 0}
98
+ .site-images img{width:100%;height:80px;object-fit:cover;border-radius:6px;background:${barBg}}
99
+
100
+ /* Error / Fallback state */
101
+ .error-state{display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;padding:30px;text-align:center}
102
+ .error-state .icon{font-size:48px;margin-bottom:12px}
103
+ .error-state h3{font-size:16px;color:${text};margin-bottom:6px}
104
+ .error-state p{font-size:13px;color:${textMuted};margin-bottom:16px;line-height:1.5}
105
+
106
+ /* Toolbar */
107
+ .toolbar{display:flex;align-items:center;justify-content:space-between;padding:8px 12px;border-top:1px solid ${border};background:${barBg}}
108
+ .toolbar .site-info{display:flex;align-items:center;gap:6px;font-size:11px;color:${textMuted}}
109
+ .toolbar .site-favicon{width:14px;height:14px;border-radius:3px;background:${border}}
110
+ .toolbar .open-btn{display:inline-flex;align-items:center;gap:5px;padding:6px 14px;background:${linkColor};color:#fff;border:none;border-radius:8px;font-size:12px;font-weight:600;text-decoration:none;cursor:pointer}
111
+ </style></head><body>
112
+ <div class="webview">
113
+ ${showUrl ? `
114
+ <div class="url-bar">
115
+ <span class="lock">🔒</span>
116
+ <span class="url">${baseUrl.hostname}${baseUrl.pathname.length > 1 ? baseUrl.pathname.substring(0, 30) + '...' : ''}</span>
117
+ <span class="refresh" onclick="location.reload()">↻</span>
118
+ </div>` : ''}
119
+
120
+ <div class="content">
121
+ ${fetchError ? `
122
+ <div class="error-state">
123
+ <div class="icon">🌐</div>
124
+ <h3>${baseUrl.hostname}</h3>
125
+ <p>Could not load this website directly. Tap the button below to open it in your browser.</p>
126
+ <a href="${url}" style="display:inline-flex;align-items:center;gap:5px;padding:10px 20px;background:${linkColor};color:#fff;border-radius:8px;text-decoration:none;font-size:13px;font-weight:600">🔗 Open Website</a>
127
+ </div>` : `
128
+ ${extracted.title ? `<div style="padding:16px">` : `<div style="padding:16px">`}
129
+ ${extracted.favicon ? `<img src="${extracted.favicon}" style="width:20px;height:20px;border-radius:4px;margin-bottom:8px" onerror="this.style.display='none'">` : ''}
130
+ ${extracted.title ? `<div class="site-title">${escapeHtml(extracted.title)}</div>` : ''}
131
+ ${extracted.description ? `<div class="site-desc">${escapeHtml(extracted.description)}</div>` : ''}
132
+ ${extracted.heroImage ? `<img src="${escapeHtml(extracted.heroImage)}" style="width:100%;border-radius:8px;margin-bottom:12px" onerror="this.style.display='none'">` : ''}
133
+ ${extracted.images.length > 0 ? `
134
+ <div class="site-images">
135
+ ${extracted.images.slice(0, 6).map(img => `<img src="${escapeHtml(img)}" onerror="this.style.display='none'" loading="lazy">`).join('')}
136
+ </div>` : ''}
137
+ ${extracted.headings.length > 0 ? extracted.headings.slice(0, 5).map(h => `<div class="site-headings">${escapeHtml(h)}</div>`).join('') : ''}
138
+ ${extracted.text ? `<div class="site-text">${escapeHtml(extracted.text).substring(0, 2000)}</div>` : ''}
139
+ ${extracted.links.length > 0 ? `
140
+ <div class="site-links">
141
+ ${extracted.links.slice(0, 8).map(l => `<a href="${escapeHtml(l.url)}" class="site-link">${escapeHtml(l.text)}</a>`).join('')}
142
+ </div>` : ''}
143
+ </div>`}
144
+ </div>
145
+
146
+ ${showToolbar ? `
147
+ <div class="toolbar">
148
+ <div class="site-info">
149
+ <div class="site-favicon"></div>
150
+ <span>${baseUrl.hostname}</span>
151
+ </div>
152
+ <a href="${url}" class="open-btn">🔗 Open Site</a>
153
+ </div>` : ''}
154
+ </div>
155
+ </body></html>`;
156
+ };
157
+
158
+ /**
159
+ * Extract meaningful content from fetched HTML
160
+ */
161
+ function extractContent(html, baseUrl) {
162
+ if (!html) return { title: '', description: '', text: '', headings: [], links: [], images: [], favicon: '', heroImage: '' };
163
+
164
+ const base = new URL(baseUrl);
165
+
166
+ // Title
167
+ const titleMatch = html.match(/<title[^>]*>([^<]+)<\/title>/i);
168
+ let title = titleMatch ? titleMatch[1].trim() : '';
169
+
170
+ // Meta description
171
+ const descMatch = html.match(/<meta[^>]*name=["']description["'][^>]*content=["']([^"']+)["']/i)
172
+ || html.match(/<meta[^>]*content=["']([^"']+)["'][^>]*name=["']description["']/i);
173
+ const description = descMatch ? descMatch[1].trim() : '';
174
+
175
+ // Favicon
176
+ const faviconMatch = html.match(/<link[^>]*rel=["'](?:shortcut )?icon["'][^>]*href=["']([^"']+)["']/i);
177
+ let favicon = faviconMatch ? resolveUrl(faviconMatch[1], base) : `${base.origin}/favicon.ico`;
178
+
179
+ // Hero image (og:image)
180
+ const ogImageMatch = html.match(/<meta[^>]*property=["']og:image["'][^>]*content=["']([^"']+)["']/i)
181
+ || html.match(/<meta[^>]*content=["']([^"']+)["'][^>]*property=["']og:image["']/i);
182
+ const heroImage = ogImageMatch ? resolveUrl(ogImageMatch[1], base) : '';
183
+
184
+ // Extract headings
185
+ const headings = [];
186
+ const headingRegex = /<h[1-6][^>]*>([^<]+)<\/h[1-6]>/gi;
187
+ let hm;
188
+ while ((hm = headingRegex.exec(html)) !== null) {
189
+ headings.push(hm[1].trim());
190
+ }
191
+
192
+ // Extract links
193
+ const links = [];
194
+ const linkRegex = /<a[^>]*href=["']([^"']+)["'][^>]*>([^<]*)<\/a>/gi;
195
+ let lm;
196
+ while ((lm = linkRegex.exec(html)) !== null) {
197
+ const href = lm[1].trim();
198
+ const text = lm[2].trim();
199
+ if (text && href && !href.startsWith('#') && !href.startsWith('javascript:')) {
200
+ links.push({ text: text.substring(0, 50), url: resolveUrl(href, base) });
201
+ }
202
+ }
203
+
204
+ // Extract images
205
+ const images = [];
206
+ const imgRegex = /<img[^>]*src=["']([^"']+)["'][^>]*>/gi;
207
+ let im;
208
+ while ((im = imgRegex.exec(html)) !== null) {
209
+ const src = im[1].trim();
210
+ if (src && !src.startsWith('data:')) {
211
+ images.push(resolveUrl(src, base));
212
+ }
213
+ }
214
+
215
+ // Extract text content (strip tags)
216
+ let text = html
217
+ .replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
218
+ .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
219
+ .replace(/<nav[^>]*>[\s\S]*?<\/nav>/gi, '')
220
+ .replace(/<footer[^>]*>[\s\S]*?<\/footer>/gi, '')
221
+ .replace(/<header[^>]*>[\s\S]*?<\/header>/gi, '')
222
+ .replace(/<[^>]+>/g, ' ')
223
+ .replace(/&nbsp;/g, ' ')
224
+ .replace(/&amp;/g, '&')
225
+ .replace(/&lt;/g, '<')
226
+ .replace(/&gt;/g, '>')
227
+ .replace(/&quot;/g, '"')
228
+ .replace(/&#39;/g, "'")
229
+ .replace(/\s+/g, ' ')
230
+ .trim();
231
+
232
+ // Take a reasonable chunk of text
233
+ if (text.length > 2000) {
234
+ text = text.substring(0, 2000) + '...';
235
+ }
236
+
237
+ return { title, description, text, headings: headings.slice(0, 10), links: links.slice(0, 20), images: images.slice(0, 12), favicon, heroImage };
238
+ }
239
+
240
+ /**
241
+ * Resolve a relative URL against a base
242
+ */
243
+ function resolveUrl(href, base) {
244
+ try {
245
+ if (href.startsWith('http://') || href.startsWith('https://')) return href;
246
+ if (href.startsWith('//')) return base.protocol + href;
247
+ return new URL(href, base).href;
248
+ } catch {
249
+ return href;
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Escape HTML entities for safe embedding
255
+ */
256
+ function escapeHtml(str) {
257
+ if (!str) return '';
258
+ return String(str)
259
+ .replace(/&/g, '&amp;')
260
+ .replace(/</g, '&lt;')
261
+ .replace(/>/g, '&gt;')
262
+ .replace(/"/g, '&quot;')
263
+ .replace(/'/g, '&#39;');
264
+ }
265
+
266
+ /**
267
+ * Generate a simple link preview (thumbnail + title + description)
268
+ * without fetching — lighter weight alternative.
269
+ *
270
+ * @param {string} url - The URL to preview
271
+ * @param {Object} [options]
272
+ * @param {string} [options.title] - Override title
273
+ * @param {string} [options.description] - Override description
274
+ * @param {string} [options.image] - Override image URL
275
+ * @returns {string} HTML string for link preview card
276
+ */
277
+ export const generateLinkPreviewHtml = (url, options = {}) => {
278
+ const { title, description, image } = options;
279
+ const baseUrl = new URL(url);
280
+
281
+ return `<html><head><style>
282
+ *{box-sizing:border-box;margin:0;padding:0}
283
+ body{background:transparent;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;padding:0}
284
+ .card{background:#1a1a2e;border:1px solid rgba(255,255,255,0.1);border-radius:12px;overflow:hidden;max-width:100%}
285
+ .card-image{width:100%;height:160px;object-fit:cover;background:linear-gradient(135deg,#1a1a2e,#2d2d44)}
286
+ .card-body{padding:14px}
287
+ .card-title{font-size:16px;font-weight:600;color:#fff;margin-bottom:4px}
288
+ .card-desc{font-size:13px;color:#aaa;line-height:1.4;margin-bottom:8px}
289
+ .card-url{font-size:11px;color:#4CAF50;display:flex;align-items:center;gap:4px}
290
+ </style></head><body>
291
+ <a href="${url}" target="_blank" style="text-decoration:none">
292
+ <div class="card">
293
+ ${image ? `<img class="card-image" src="${image}" alt="" onerror="this.style.display='none'">` :
294
+ `<div class="card-image" style="display:flex;align-items:center;justify-content:center;font-size:48px">🌐</div>`}
295
+ <div class="card-body">
296
+ <div class="card-title">${escapeHtml(title || baseUrl.hostname)}</div>
297
+ <div class="card-desc">${escapeHtml(description || 'Click to visit ' + baseUrl.hostname)}</div>
298
+ <div class="card-url">🔒 ${baseUrl.hostname}</div>
299
+ </div>
300
+ </div>
301
+ </a>
302
+ </body></html>`;
303
+ };
304
+
305
+ export default generateWebsitePreviewHtml;
306
+