|
| 1 | +// oxlint-disable no-empty-file |
| 2 | +// import { Attachment } from "svelte/attachments"; |
| 3 | +// import { SvelteSet } from "svelte/reactivity"; |
| 4 | + |
| 5 | +// export let cssVariables = new SvelteSet(); |
| 6 | + |
| 7 | +// export function documentAttachment(): Attachment { |
| 8 | +// let observer = new MutationObserver((mutations) => { |
| 9 | +// let shouldUpdate = false; |
| 10 | + |
| 11 | +// mutations.forEach((mutation) => { |
| 12 | +// if (mutation.type === "childList") { |
| 13 | +// // Check if any added nodes are style/link elements |
| 14 | +// mutation.addedNodes.forEach((node) => { |
| 15 | +// if (node.nodeType === Node.ELEMENT_NODE) { |
| 16 | +// const tagName = node.tagName.toLowerCase(); |
| 17 | +// if ( |
| 18 | +// tagName === "style" || |
| 19 | +// (tagName === "link" && |
| 20 | +// node.rel === "stylesheet") |
| 21 | +// ) { |
| 22 | +// shouldUpdate = true; |
| 23 | +// } |
| 24 | +// } |
| 25 | +// }); |
| 26 | + |
| 27 | +// // Check if any removed nodes were style/link elements |
| 28 | +// mutation.removedNodes.forEach((node) => { |
| 29 | +// if (node.nodeType === Node.ELEMENT_NODE) { |
| 30 | +// const tagName = node.tagName.toLowerCase(); |
| 31 | +// if ( |
| 32 | +// tagName === "style" || |
| 33 | +// (tagName === "link" && |
| 34 | +// node.rel === "stylesheet") |
| 35 | +// ) { |
| 36 | +// shouldUpdate = true; |
| 37 | +// } |
| 38 | +// } |
| 39 | +// }); |
| 40 | +// } |
| 41 | + |
| 42 | +// // Watch for attribute changes on style/link elements |
| 43 | +// if (mutation.type === "attributes" && mutation.target) { |
| 44 | +// const tagName = mutation.target.tagName.toLowerCase(); |
| 45 | +// if ( |
| 46 | +// tagName === "style" || |
| 47 | +// (tagName === "link" && |
| 48 | +// mutation.target.rel === "stylesheet") |
| 49 | +// ) { |
| 50 | +// shouldUpdate = true; |
| 51 | +// } |
| 52 | +// } |
| 53 | +// }); |
| 54 | + |
| 55 | +// // if (shouldUpdate) { |
| 56 | +// // console.log( |
| 57 | +// // "Stylesheet changes detected, updating CSS variable blocks...", |
| 58 | +// // ); |
| 59 | +// // // Small delay to ensure stylesheets are fully loaded |
| 60 | +// // setTimeout(() => { |
| 61 | +// // this.blockDocumentCSSVariables(); |
| 62 | +// // }, 100); |
| 63 | +// // } |
| 64 | +// }); |
| 65 | +// return (_element: Element) => { |
| 66 | +// const targetSelectors = [":root", "html", "html:root", "*"]; |
| 67 | + |
| 68 | +// try { |
| 69 | +// // Iterate through all stylesheets |
| 70 | +// for (const styleSheet of document.styleSheets) { |
| 71 | +// try { |
| 72 | +// console.log("🚀 ~ return ~ styleSheet:", styleSheet); |
| 73 | +// // Skip stylesheets we can't access (CORS) |
| 74 | +// if (!styleSheet.cssRules) continue; |
| 75 | + |
| 76 | +// // Check each rule in the stylesheet |
| 77 | +// for (const rule of styleSheet.cssRules) { |
| 78 | +// if (rule instanceof CSSStyleRule) { |
| 79 | +// // Check if this rule targets a top-level selector |
| 80 | +// const selectorText = rule.selectorText |
| 81 | +// .toLowerCase(); |
| 82 | +// const isTargetSelector = true || |
| 83 | +// targetSelectors.some( |
| 84 | +// selector => selectorText.includes(selector), |
| 85 | +// ); |
| 86 | + |
| 87 | +// if (isTargetSelector && rule.style) { |
| 88 | +// // Extract CSS custom properties from this rule |
| 89 | +// for (let i = 0; i < rule.style.length; i++) { |
| 90 | +// const property = rule.style[i]; |
| 91 | +// if (property.startsWith("--")) { |
| 92 | +// cssVariables.add(property); |
| 93 | +// } |
| 94 | +// } |
| 95 | +// } |
| 96 | +// } |
| 97 | +// } |
| 98 | +// } catch (e) { |
| 99 | +// // Skip stylesheets that can't be accessed (cross-origin) |
| 100 | +// console.warn( |
| 101 | +// "Cannot access stylesheet:", |
| 102 | +// styleSheet.href, |
| 103 | +// e, |
| 104 | +// ); |
| 105 | +// } |
| 106 | +// } |
| 107 | +// } catch (e) { |
| 108 | +// console.error("Error extracting CSS variables:", e); |
| 109 | +// } |
| 110 | + |
| 111 | +// setupStyleSheetObserver(observer); |
| 112 | + |
| 113 | +// $inspect(cssVariables); |
| 114 | + |
| 115 | +// return () => { |
| 116 | +// observer.disconnect(); |
| 117 | +// }; |
| 118 | +// }; |
| 119 | +// } |
| 120 | + |
| 121 | +// // Target selectors that define global CSS variables |
| 122 | + |
| 123 | +// const a = (document: Document) => { |
| 124 | +// let element = document.querySelector("amgif-lol"); |
| 125 | +// let elementhead = document.querySelector("amgif-lol > head"); |
| 126 | +// console.log( |
| 127 | +// "🚀 ~ <svelte:body{@attach ~ body:", |
| 128 | +// document, |
| 129 | +// document.styleSheets, |
| 130 | +// element, |
| 131 | +// elementhead, |
| 132 | +// ); |
| 133 | +// }; |
| 134 | + |
| 135 | +// function setupStyleSheetObserver(observer: MutationObserver) { |
| 136 | +// // Watch for changes to the document head (new stylesheets) |
| 137 | + |
| 138 | +// // Start observing |
| 139 | +// observer.observe(document.head, { |
| 140 | +// childList: true, |
| 141 | +// subtree: true, |
| 142 | +// attributes: true, |
| 143 | +// attributeFilter: ["href", "rel"], |
| 144 | +// }); |
| 145 | + |
| 146 | +// // Also observe the document element for dynamically added styles |
| 147 | +// observer.observe(document.documentElement, { |
| 148 | +// childList: true, |
| 149 | +// subtree: true, |
| 150 | +// attributes: false, |
| 151 | +// }); |
| 152 | +// } |
0 commit comments