allow user's HTML to override title and favicon
Hunter
Friday, July 25th, 2025 at 4:56 PM
187 }
188 });
189
190 + function extractTitleAndFavicon(htmlCode) {
191 + const parser = new DOMParser();
192 + const doc = parser.parseFromString(htmlCode, 'text/html');
193 +
194 + // Extract title
195 + const titleElement = doc.querySelector('title');
196 + const title = titleElement ? titleElement.textContent.trim() : null;
197 +
198 + // Extract favicon
199 + const faviconSelectors = [
200 + 'link[rel="icon"]',
201 + 'link[rel="shortcut icon"]',
202 + 'link[rel="apple-touch-icon"]',
203 + 'link[rel="mask-icon"]'
204 + ];
205 +
206 + let favicon = null;
207 + for (const selector of faviconSelectors) {
208 + const faviconElement = doc.querySelector(selector);
209 + if (faviconElement && faviconElement.href) {
210 + favicon = faviconElement.href;
211 + break;
212 + }
213 + }
214 +
215 + return { title, favicon };
216 + }
217 +
218 + function updateMainPageTitleAndFavicon(title, favicon) {
219 + // Update title
220 + if (title) {
221 + document.title = title;
222 + } else {
223 + document.title = 'Web Workshop';
224 + }
225 +
226 + // Update favicon
227 + let faviconLink = document.querySelector('link[rel="icon"]');
228 + if (!faviconLink) {
229 + faviconLink = document.createElement('link');
230 + faviconLink.rel = 'icon';
231 + document.head.appendChild(faviconLink);
232 + }
233 +
234 + if (favicon) {
235 + faviconLink.href = favicon;
236 + } else {
237 + faviconLink.href = 'resources/rollerskate.png';
238 + }
239 + }
240 +
241 function updatePreview() {
242 const code = editorView.state.doc.toString();
243
244 + // Extract and update title and favicon from user's HTML
245 + const { title, favicon } = extractTitleAndFavicon(code);
246 + updateMainPageTitleAndFavicon(title, favicon);
247 +
248 // Store scroll position before updating
249 let scrollX = 0, scrollY = 0;
250 try {