Search code examples
webkitvala

webkit : how to get the actual content of a page after content was added via javascript?


I want to get the actual content of a page I loaded into a webview after some content has been updated by some jquery

$(document).ready(function() {
$("#main").append('<p>Test</p><p>Test</p><p>Test</p><p>Test</p><p>Test</p><p>Test</p><p>Test</p>');
});

After the page has been updated I tried to get the content of page with the following command [vala syntax]

web_view.get_main_frame ().get_data_source().get_data().str

but I only get the original content (even if loading is finished)

using

web_view.get_dom_document().document_element.text_content

I get the actual content but the tags are removed.

I guess I could walk the whole tree to get the actual document but tere should be a more easy way to do it.

EDIT: my solution

this.web_view.load_finished.connect ((source, frame) => {
 stderr.printf(this.web_view.get_dom_document().body.get_inner_html());
}

I'll probably find this awfull when I'll read this some years from now but for now I'll go with that.


Solution

  • In the HTML DOM, elements implement the HTMLElement interface. The HTMLElement interface in WebKit includes the outerHTML property. This property returns a string containing the serialized markup of the element and all of its children. I'm not familiar with Vala but based on your code snippets this would be accessed like so:

    web_view.get_dom_document().document_element.outer_html