|
| 1 | +# Content Visibility |
| 2 | + |
| 3 | +This snippet will output all the elements that have a content-visibility applied to them. |
| 4 | + |
| 5 | +### Attribution |
| 6 | + |
| 7 | +This snippet code it's based in the [script](https://github.com/corewebvitals/pagespeed/blob/main/code/inp/detect-content-visibility-auto.js) by [Arjen Karel](https://www.linkedin.com/posts/arjenkarel_corewebvitals-activity-7183071762769686529-owtr) |
| 8 | + |
| 9 | +### Snippet |
| 10 | + |
| 11 | +```js copy |
| 12 | +(() => { |
| 13 | + |
| 14 | + // Create an object to store the results |
| 15 | + let ret = { |
| 16 | + autoTable: [], |
| 17 | + autoNodeArray: [] |
| 18 | + }; |
| 19 | + |
| 20 | + |
| 21 | + // Get the name of the node |
| 22 | + function getName(node) { |
| 23 | + const name = node.nodeName; |
| 24 | + return node.nodeType === 1 |
| 25 | + ? name.toLowerCase() |
| 26 | + : name.toUpperCase().replace(/^#/, ''); |
| 27 | + } |
| 28 | + |
| 29 | + // Get the selector |
| 30 | + const getSelector = (node) => { |
| 31 | + let sel = ''; |
| 32 | + |
| 33 | + try { |
| 34 | + while (node && node.nodeType !== 9) { |
| 35 | + const el = node; |
| 36 | + const part = el.id |
| 37 | + ? '#' + el.id |
| 38 | + : getName(el) + |
| 39 | + (el.classList && |
| 40 | + el.classList.value && |
| 41 | + el.classList.value.trim() && |
| 42 | + el.classList.value.trim().length |
| 43 | + ? '.' + el.classList.value.trim().replace(/\s+/g, '.') |
| 44 | + : ''); |
| 45 | + if (sel.length + part.length > (100) - 1) return sel || part; |
| 46 | + sel = sel ? part + '>' + sel : part; |
| 47 | + if (el.id) break; |
| 48 | + node = el.parentNode; |
| 49 | + } |
| 50 | + } catch (err) { |
| 51 | + // Do nothing... |
| 52 | + } |
| 53 | + return sel; |
| 54 | + }; |
| 55 | + |
| 56 | + const getNodesWithContentVisibility = (node) => { |
| 57 | + // Get the computed style |
| 58 | + let cs = window.getComputedStyle(node); |
| 59 | + let cv = cs['content-visibility']; |
| 60 | + |
| 61 | + // If we find content-visibility: auto, add it to the table |
| 62 | + if (cv && cv === 'auto') { |
| 63 | + ret.autoTable.push({ selector: getSelector(node), ContentVisibility: cs['content-visibility'] }); |
| 64 | + ret.autoNodeArray.push(node); |
| 65 | + } |
| 66 | + |
| 67 | + // Recursively call this function for each child node |
| 68 | + for (let i = 0; i < node.children.length; i++) { |
| 69 | + getNodesWithContentVisibility(node.children[i]); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // find all content-visibility: auto |
| 74 | + getNodesWithContentVisibility(document.body); |
| 75 | + |
| 76 | + // Display the results in the console |
| 77 | + if (ret.autoTable.length === 0) { |
| 78 | + console.log('%cNo content-visibility: auto found. Consider applying content-visibility: auto to offscreen content (the footer perhaps?)', 'color: orange; font-weight: bold;'); |
| 79 | + } else { |
| 80 | + console.log('%cContent-visibility: auto selectors', 'color: green; font-weight: bold;'); |
| 81 | + console.table(ret.autoTable); |
| 82 | + |
| 83 | + console.log('%cNodeList for you to inspect (harder to read but more info)', 'color: green; font-weight: bold;'); |
| 84 | + console.log(ret.autoNodeArray); |
| 85 | + } |
| 86 | +})() |
| 87 | +``` |
0 commit comments