Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add document.scripts implementation #499

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib/web-worker/worker-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ export const patchDocument = (
},
},

scripts: {
get() {
return getter(this, ['scripts']);
},
},

implementation: {
get() {
return {
Expand Down
6 changes: 6 additions & 0 deletions tests/platform/document/document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,10 @@ test('document', async ({ page }) => {
const testImages = page.locator('#testDocumentImages');
const pageUrl = new URL(page.url());
await expect(testImages).toHaveText(`${pageUrl.origin}/fake.jpg`);

const testDocumentScriptsInitial = page.locator('#testDocumentScriptsInitial');
await expect(+(await testDocumentScriptsInitial.innerText())).toBeGreaterThan(0);
const value = +(await testDocumentScriptsInitial.innerText());
const testDocumentScriptsAfter = page.locator('#testDocumentScriptsAfter');
await expect(+(await testDocumentScriptsAfter.innerText())).toEqual(value + 1);
});
19 changes: 19 additions & 0 deletions tests/platform/document/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,25 @@ <h1 class="title">Document</h1>
</script>
</li>

<li>
<strong>document.scripts</strong>
<code id="testDocumentScripts">
<span id="testDocumentScriptsInitial"></span>
<span id="testDocumentScriptsAfter"></span>
</code>
<script type="text/partytown">
(function () {
document.getElementById('testDocumentScriptsInitial').textContent = document.scripts.length;

const newScript = document.createElement('script');
newScript.innerText = `console.log("new script")`;
document.body.appendChild(newScript);

document.getElementById('testDocumentScriptsAfter').textContent = document.scripts.length;
})();
</script>
</li>

<script type="text/partytown">
(function () {
document.body.classList.add('completed');
Expand Down
Loading