Skip to content

Instantly share code, notes, and snippets.

@htggit
Forked from jsts/gist:e398fdaef7848f994f8d532b7df741b5
Last active July 4, 2017 11:33
Show Gist options
  • Select an option

  • Save htggit/cc4da9532f8da2f76645bb247b844982 to your computer and use it in GitHub Desktop.

Select an option

Save htggit/cc4da9532f8da2f76645bb247b844982 to your computer and use it in GitHub Desktop.

Revisions

  1. gitlabc revised this gist Jul 4, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HeadlessChrome.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ module.exports = {
    ```
    ```bash
    alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
    alias canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
    alias chromium="/Applications/Chromium.app/Contents/MacOS/Chromium"

    chrome --headless --disable-gpu --dump-dom https://www.chromestatus.com/
  2. gitlabc renamed this gist Jul 4, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.md → HeadlessChrome.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Headless Chrome

    Reference URL [https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai]

    ```js
  3. htggit revised this gist Jun 21, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    Reference URL [https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai]

    ```js
    module.exports = {
    'launcher:Chrome': ['type', ChromeBrowser],
  4. htggit revised this gist Jun 21, 2017. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,13 @@

    ```js
    module.exports = {
    'launcher:Chrome': ['type', ChromeBrowser],
    'launcher:ChromeHeadless': ['type', ChromeHeadlessBrowser],
    'launcher:ChromeCanary': ['type', ChromeCanaryBrowser],
    'launcher:ChromeCanaryHeadless': ['type', ChromeCanaryHeadlessBrowser],
    'launcher:Chromium': ['type', ChromiumBrowser],
    'launcher:Dartium': ['type', DartiumBrowser]
    }
    ```
    ```bash
    alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
  5. htggit revised this gist Jun 21, 2017. 1 changed file with 79 additions and 0 deletions.
    79 changes: 79 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -54,4 +54,83 @@ async function launchChrome(headless = true) {
    launchChrome(true).then(chrome => {
    ...
    });
    ```
    ```bash
    yarn add chrome-remote-interface
    ```

    ```js
    const CDP = require('chrome-remote-interface');

    ...

    launchChrome().then(async chrome => {
    const version = await CDP.Version({port: chrome.port});
    console.log(version['User-Agent']));
    });
    ```
    ```js
    const CDP = require('chrome-remote-interface');

    ...

    (async function() {

    const chrome = await launchChrome();
    const protocol = await CDP({port: chrome.port});

    // Extract the DevTools protocol domains we need and enable them.
    // See API docs: https://chromedevtools.github.io/devtools-protocol/
    const {Page} = protocol;
    await Page.enable();

    Page.navigate({url: 'https://www.chromestatus.com/'});

    // Wait for window.onload before doing stuff.
    Page.loadEventFired(async () => {
    const manifest = await Page.getAppManifest();

    if (manifest.url) {
    console.log('Manifest: ' + manifest.url);
    console.log(manifest.data);
    } else {
    console.log('Site has no app manifest');
    }

    protocol.close();
    chrome.kill(); // Kill Chrome.
    });

    })();
    ```
    ```js
    const CDP = require('chrome-remote-interface');

    ...

    (async function() {

    const chrome = await launchChrome();
    const protocol = await CDP({port: chrome.port});

    // Extract the DevTools protocol domains we need and enable them.
    // See API docs: https://chromedevtools.github.io/devtools-protocol/
    const {Page, Runtime} = protocol;
    await Promise.all([Page.enable(), Runtime.enable()]);

    Page.navigate({url: 'https://www.chromestatus.com/'});

    // Wait for window.onload before doing stuff.
    Page.loadEventFired(async () => {
    const js = "document.querySelector('title').textContent";
    // Evaluate the JS expression in the page.
    const result = await Runtime.evaluate({expression: js});

    console.log('Title of page: ' + result.result.value);

    protocol.close();
    chrome.kill(); // Kill Chrome.
    });

    })();
    ```
  6. htggit revised this gist Jun 21, 2017. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@

    '''
    ```bash
    alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
    alias chromium="/Applications/Chromium.app/Contents/MacOS/Chromium"
    @@ -12,9 +12,10 @@ chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/
    chrome --headless --disable-gpu --screenshot --window-size=1280,1696 https://www.chromestatus.com/
    # Nexus 5x
    chrome --headless --disable-gpu --screenshot --window-size=412,732 https://www.chromestatus.com/
    chrome --headless --disable-gpu --repl https://www.chromestatus.com/
    ```

    $ chrome --headless --disable-gpu --repl https://www.chromestatus.com/

    ```js
    const execFile = require('child_process').execFile;

    function launchHeadlessChrome(url, callback) {
    @@ -26,9 +27,11 @@ function launchHeadlessChrome(url, callback) {
    launchHeadlessChrome('https://www.chromestatus.com', (err, stdout, stderr) => {
    ...
    });

    ```
    ```bash
    yarn add lighthouse

    ```
    ```js
    const chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher');

    /**
    @@ -51,4 +54,4 @@ async function launchChrome(headless = true) {
    launchChrome(true).then(chrome => {
    ...
    });
    '''
    ```
  7. htggit revised this gist Jun 21, 2017. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@


    '''
    alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
    alias chromium="/Applications/Chromium.app/Contents/MacOS/Chromium"
    @@ -15,7 +15,6 @@ chrome --headless --disable-gpu --screenshot --window-size=412,732 https://www.c

    $ chrome --headless --disable-gpu --repl https://www.chromestatus.com/

    '''
    const execFile = require('child_process').execFile;

    function launchHeadlessChrome(url, callback) {
    @@ -27,10 +26,9 @@ function launchHeadlessChrome(url, callback) {
    launchHeadlessChrome('https://www.chromestatus.com', (err, stdout, stderr) => {
    ...
    });
    '''

    yarn add lighthouse
    '''

    const chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher');

    /**
  8. htggit renamed this gist Jun 21, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. htggit created this gist Jun 21, 2017.
    56 changes: 56 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@


    alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
    alias chromium="/Applications/Chromium.app/Contents/MacOS/Chromium"

    chrome --headless --disable-gpu --dump-dom https://www.chromestatus.com/
    chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/

    chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/
    # Size of a standard letterhead.
    chrome --headless --disable-gpu --screenshot --window-size=1280,1696 https://www.chromestatus.com/
    # Nexus 5x
    chrome --headless --disable-gpu --screenshot --window-size=412,732 https://www.chromestatus.com/

    $ chrome --headless --disable-gpu --repl https://www.chromestatus.com/

    '''
    const execFile = require('child_process').execFile;

    function launchHeadlessChrome(url, callback) {
    // Assuming MacOSx.
    const CHROME = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome';
    execFile(CHROME, ['--headless', '--disable-gpu', '--remote-debugging-port=9222', url], callback);
    }

    launchHeadlessChrome('https://www.chromestatus.com', (err, stdout, stderr) => {
    ...
    });
    '''

    yarn add lighthouse
    '''
    const chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher');

    /**
    * Launches a debugging instance of Chrome.
    * @param {boolean=} headless True (default) launches Chrome in headless mode.
    * False launches a full version of Chrome.
    * @return {Promise<ChromeLauncher>}
    */
    async function launchChrome(headless = true) {
    return await chromeLauncher.launch({
    // port: 9222, // Uncomment to force a specific port of your choice.
    chromeFlags: [
    '--window-size=412,732',
    '--disable-gpu',
    headless ? '--headless' : ''
    ]
    });
    }

    launchChrome(true).then(chrome => {
    ...
    });
    '''