|
| 1 | +import { Logger } from 'pino'; |
| 2 | +import { Page } from 'puppeteer'; |
| 3 | +import { ToolingScan } from 'entities/scan-data.entity'; |
| 4 | + |
| 5 | +export const buildToolingResult = async ( |
| 6 | + logger: Logger, |
| 7 | + page: Page, |
| 8 | +): Promise<ToolingScan> => { |
| 9 | + const cssLibraryResults = await getCssLibraryResults(page); |
| 10 | + |
| 11 | + return { |
| 12 | + tooling: cssLibraryResults.length ? cssLibraryResults : null, |
| 13 | + }; |
| 14 | +}; |
| 15 | + |
| 16 | +async function getCssLibraryResults(page: Page): Promise<string> { |
| 17 | + const result = await page.evaluate(() => { |
| 18 | + const detected: string[] = []; |
| 19 | + |
| 20 | + const linkHrefs = Array.from( |
| 21 | + document.querySelectorAll('link[rel="stylesheet"]'), |
| 22 | + ).map((el) => el.getAttribute('href')?.toLowerCase() ?? ''); |
| 23 | + |
| 24 | + const scriptSrcs = Array.from(document.querySelectorAll('script')).map( |
| 25 | + (el) => el.getAttribute('src')?.toLowerCase() ?? '', |
| 26 | + ); |
| 27 | + |
| 28 | + // Bootstrap |
| 29 | + if ( |
| 30 | + linkHrefs.some((href) => href.includes('bootstrap')) || |
| 31 | + document.querySelector('.container-fluid, .navbar-toggler, .btn-primary') |
| 32 | + ) { |
| 33 | + detected.push('bootstrap'); |
| 34 | + } |
| 35 | + |
| 36 | + // Tailwind |
| 37 | + if ( |
| 38 | + linkHrefs.some((href) => href.includes('tailwind')) || |
| 39 | + scriptSrcs.some((src) => src.includes('tailwind')) |
| 40 | + ) { |
| 41 | + detected.push('tailwind'); |
| 42 | + } |
| 43 | + |
| 44 | + // Foundation |
| 45 | + if ( |
| 46 | + linkHrefs.some((href) => href.includes('foundation')) || |
| 47 | + document.querySelector('.top-bar, .callout, .orbit-container') |
| 48 | + ) { |
| 49 | + detected.push('foundation'); |
| 50 | + } |
| 51 | + |
| 52 | + // Animate.css |
| 53 | + if ( |
| 54 | + linkHrefs.some((href) => href.includes('animate')) || |
| 55 | + document.querySelector('.animate__animated, .animated') |
| 56 | + ) { |
| 57 | + detected.push('animate.css'); |
| 58 | + } |
| 59 | + |
| 60 | + // Bulma |
| 61 | + if ( |
| 62 | + linkHrefs.some((href) => href.includes('bulma')) || |
| 63 | + document.querySelector('.hero-body, .navbar-burger, .is-primary') |
| 64 | + ) { |
| 65 | + detected.push('bulma'); |
| 66 | + } |
| 67 | + |
| 68 | + // Materialize |
| 69 | + if ( |
| 70 | + linkHrefs.some((href) => href.includes('materialize')) || |
| 71 | + document.querySelector( |
| 72 | + '.materialize-red, .waves-effect, .collection-item', |
| 73 | + ) |
| 74 | + ) { |
| 75 | + detected.push('materialize'); |
| 76 | + } |
| 77 | + |
| 78 | + // Semantic UI |
| 79 | + if ( |
| 80 | + linkHrefs.some((href) => href.includes('semantic')) || |
| 81 | + document.querySelector('.ui.button, .ui.menu, .ui.container') |
| 82 | + ) { |
| 83 | + detected.push('semantic-ui'); |
| 84 | + } |
| 85 | + |
| 86 | + // UIkit |
| 87 | + if ( |
| 88 | + linkHrefs.some((href) => href.includes('uikit')) || |
| 89 | + document.querySelector('[uk-grid], [uk-slider], [uk-navbar]') |
| 90 | + ) { |
| 91 | + detected.push('uikit'); |
| 92 | + } |
| 93 | + |
| 94 | + // Material Design Lite |
| 95 | + if ( |
| 96 | + linkHrefs.some( |
| 97 | + (href) => href.includes('material.min') || href.includes('mdl'), |
| 98 | + ) || |
| 99 | + document.querySelector('.mdl-button, .mdl-layout, .mdl-grid') |
| 100 | + ) { |
| 101 | + detected.push('material-design-lite'); |
| 102 | + } |
| 103 | + |
| 104 | + return detected; |
| 105 | + }); |
| 106 | + |
| 107 | + return result.join(','); |
| 108 | +} |
0 commit comments