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

Render raw HTML instead of navigating to a URL #95

Closed
westy92 opened this issue Apr 13, 2017 · 7 comments
Closed

Render raw HTML instead of navigating to a URL #95

westy92 opened this issue Apr 13, 2017 · 7 comments

Comments

@westy92
Copy link
Contributor

westy92 commented Apr 13, 2017

Is there a way to have Chrome render a string of raw HTML instead of having it navigate to a URL?

@cyrus-and
Copy link
Owner

Maybe using Page.setDocumentContent. This should work:

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

CDP(async (client) => {
    const {Page} = client;
    try {
        const {frameId} = await Page.navigate({url: 'about:blank'});
        const html = '<marquee>It works!</marquee>';
        await Page.setDocumentContent({frameId, html});
    } catch (err) {
        console.error(err);
        client.close();
    }
}).on('error', (err) => {
    console.error(err);
});

Note that you can fetch frameId by other means for example by using the Page.frameAttached event.

@utkuturunc
Copy link

@cyrus-and This works for html and css but remote scripts does not get loaded. Any ideas how to invoke them?

@cyrus-and
Copy link
Owner

@utkuturunc you're right it doesn't work for scripts, you should submit an issue here. You can always evaluate arbitrary JavaScript code in the tab context using, for example, the Runtime domain.

@utkuturunc
Copy link

utkuturunc commented Apr 30, 2017

@cyrus-and Thanks for the replies. I will submit that issue.

In the mean time, if anybody wants to do this you can simply convert the html string to a data uri and navigate to that uri. This also makes getting the frameId easier.

@westy92
Copy link
Contributor Author

westy92 commented May 2, 2017

@utkuturunc did you get the issue submitted?

@utkuturunc
Copy link

Sorry for the delay. I just did.
ChromeDevTools/devtools-protocol#13

By the way I cannot capture the frameId using any event, Page.frameAttached does not seem to be firing. I couldn't get it to work without navigating to a blank page.

@cyrus-and
Copy link
Owner

@utkuturunc AFAIK Page.frameAttached is fired during the page loading phase when a new frame is parsed from the HTML then attached to the parent frame, where the root frame is the one you get with Page.navigate. But it still implies a page navigation.

Alternatively you can use Page.getResourceTree, for example to get the root frame:

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

CDP(async (client) => {
    try {
        const {Page} = client;
        const {frameTree} = await Page.getResourceTree();
        const root = frameTree.frame;
        console.log(root.id);
    } catch (err) {
        console.error(err);
    } finally {
        client.close();
    }
}).on('error', (err) => {
    console.error(err);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants