-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1672 from jdi-testing/issue_1657-enrich-document-…
…with-inline-styles-and-JS Issue 1657: enrich document with inline styles and js
- Loading branch information
Showing
7 changed files
with
171 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import connector from '../../pageServices/connector'; | ||
|
||
async function waitForAllStylesToLoad() { | ||
const styleSheets = document.querySelectorAll('link[rel="stylesheet"]'); | ||
|
||
const loadPromises = Array.from(styleSheets).map( | ||
(sheet) => | ||
new Promise<void>((resolve) => { | ||
const linkElement = sheet as HTMLLinkElement; | ||
if (linkElement.sheet) { | ||
resolve(); | ||
} else { | ||
linkElement.onload = () => resolve(); | ||
linkElement.onerror = () => resolve(); | ||
} | ||
}), | ||
); | ||
|
||
await Promise.all(loadPromises); | ||
} | ||
|
||
export const getFullDocumentWithStyles = async () => { | ||
await waitForAllStylesToLoad(); | ||
|
||
const documentResult = await connector.attachContentScript(() => { | ||
const fetchCSS = () => { | ||
let allStyles = ''; | ||
for (let i = 0; i < document.styleSheets.length; i++) { | ||
const sheet = document.styleSheets[i]; | ||
try { | ||
const rules = sheet.rules || sheet.cssRules; | ||
for (let j = 0; j < rules.length; j++) { | ||
allStyles += rules[j].cssText + '\n'; | ||
} | ||
} catch (e) { | ||
console.error("Can't fetch styles: ", e); | ||
} | ||
} | ||
return allStyles; | ||
}; | ||
|
||
let outerHTML = document.documentElement.outerHTML; | ||
|
||
const stylesString = fetchCSS(); | ||
outerHTML = outerHTML.replace(/<link rel="stylesheet"[^>]+>/g, ''); | ||
outerHTML = outerHTML.replace('</head>', `<style>\n${stylesString}</style></head>`); | ||
|
||
const scripts = [...document.scripts] | ||
.map((script: HTMLScriptElement) => { | ||
return script.src ? `<script src="${script.src}"></script>` : `<script>${script.innerHTML}</script>`; | ||
}) | ||
.join('\n'); | ||
|
||
outerHTML = outerHTML.replace('</body>', `${scripts}</body>`); | ||
|
||
return JSON.stringify({ outerHTML }); | ||
}); | ||
|
||
const result = await documentResult[0].result; | ||
const { outerHTML } = JSON.parse(result); | ||
return JSON.stringify(outerHTML); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.