Skip to content

Commit b010356

Browse files
authored
fix(addInitScript): tolerate trailing comments (#13275)
1 parent 66a95c6 commit b010356

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

packages/playwright-core/src/client/clientHelper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function evaluationScript(fun: Function | string | { path?: string,
4343
if (fun.path !== undefined) {
4444
let source = await fs.promises.readFile(fun.path, 'utf8');
4545
if (addSourceUrl)
46-
source += '//# sourceURL=' + fun.path.replace(/\n/g, '');
46+
source += '\n//# sourceURL=' + fun.path.replace(/\n/g, '');
4747
return source;
4848
}
4949
throw new Error('Either path or content property must be present');

packages/playwright-core/src/server/webkit/wkPage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ export class WKPage implements PageDelegate {
777777
scripts.push(this._bindingToScript(binding));
778778
scripts.push(...this._browserContext.initScripts);
779779
scripts.push(...this._page.initScripts);
780-
return scripts.join(';');
780+
return scripts.join(';\n');
781781
}
782782

783783
async _updateBootstrapScript(): Promise<void> {

tests/page/page-add-init-script.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ it('should throw without path and content', async ({ page }) => {
4343
expect(error.message).toContain('Either path or content property must be present');
4444
});
4545

46+
it('should work with trailing comments', async ({ page, asset }) => {
47+
await page.addInitScript({ content: '// comment' });
48+
await page.addInitScript({ content: 'window.secret = 42;' });
49+
await page.goto('data:text/html,<html></html>');
50+
expect(await page.evaluate('secret')).toBe(42);
51+
});
52+
4653
it('should support multiple scripts', async ({ page, server }) => {
4754
await page.addInitScript(function() {
4855
window['script1'] = 1;

0 commit comments

Comments
 (0)