diff --git a/docs/api.md b/docs/api.md index e94c78b0189..5d6b67b4816 100644 --- a/docs/api.md +++ b/docs/api.md @@ -56,6 +56,7 @@ + [page.screenshot([options])](#pagescreenshotoptions) + [page.setContent(html)](#pagesetcontenthtml) + [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders) + + [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled) + [page.setRequestInterceptionEnabled(value)](#pagesetrequestinterceptionenabledvalue) + [page.setUserAgent(userAgent)](#pagesetuseragentuseragent) + [page.setViewport(viewport)](#pagesetviewportviewport) @@ -659,6 +660,11 @@ The extra HTTP headers will be sent with every request the page initiates. > **NOTE** page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests. +#### page.setJavaScriptEnabled(enabled) +- `enabled` <[boolean]> Whether or not to enabled JavaScript on the page. +- returns: <[Promise]> + +> **NOTE** changing this value won't affect scripts that have already been run. It will take full effect on the next [navigation](#pagegotourl-options). #### page.setRequestInterceptionEnabled(value) - `value` <[boolean]> Whether to enable request interception. diff --git a/lib/Page.js b/lib/Page.js index 35fb92781e8..1ae29623b8b 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -385,6 +385,13 @@ class Page extends EventEmitter { ]); } + /** + * @param {boolean} enabled + */ + async setJavaScriptEnabled(enabled) { + await this._client.send('Emulation.setScriptExecutionDisabled', { value: !enabled }); + } + /** * @param {?string} mediaType * @return {!Promise} diff --git a/test/test.js b/test/test.js index 2d9210e6894..f5e57ab3444 100644 --- a/test/test.js +++ b/test/test.js @@ -1696,6 +1696,24 @@ describe('Page', function() { })); }); + describe('Page.setJavaScriptEnabled', function() { + it('should work', SX(async function() { + await page.setJavaScriptEnabled(false); + await page.goto('data:text/html, '); + let error = null; + try { + await page.evaluate('something'); + } catch (e) { + error = e; + } + expect(error.message).toContain('something is not defined'); + + await page.setJavaScriptEnabled(true); + await page.goto('data:text/html, '); + expect(await page.evaluate('something')).toBe('forbidden'); + })); + }); + describe('Page.evaluateOnNewDocument', function() { it('should evaluate before anything else on the page', SX(async function() { await page.evaluateOnNewDocument(function(){