Content
Chromium displays the content of various types, e.g. images, video, PDF, Flash etc., but in most cases you will display the HTML content of a web page. This guide shows how to access the content of a web page, get the currently selected text, find some text on a web page, save the web page as a file or a set of files, etc.
Accessing HTML
To get the HTML of the page loaded in a frame, use the Frame::html()
method:
browser->mainFrame()->html();
Call this method only when the is fully loaded. Otherwise, you may receive incomplete HTML or an empty string:
navigation->onFrameLoadFinished += [](const FrameLoadFinished& event) {
std::cout << event.frame->html() << std::endl;
};
Finding text
Molybden allows finding text on the currently loaded web page or PDF and highlight all the matches.
Chromium searches text only through the visible part of the loaded content which have a non-empty size.
To perform the search on the currently loaded content use TextFinder
:
std::shared_ptr<TextFinder> text_finder = browser->textFinder();
The following example demonstrates how to find “text” on the currently loaded web page with the specified search parameters, and wait until the search has been completed:
FindOptions find_options;
find_options.match_case = true;
find_options.is_search_backward = false;
text_finder->find("text", find_options, [](const FindResult& result) {
if (!result.is_searching) {
int32_t number_of_matches = result.number_of_matches;
int32_t selected_match = result.selected_match;
}
});
Canceling search
To clear the highlighted search results on a web page and cancel the search use the
TextFinder::stopFindingAndClearSelection()
or TextFinder::stopFindingAndKeepSelection()
method.
You can load a PDF file from a remote web server or local file system. By default, PDF files are displayed using the built-in PDF Viewer.
If you don’t want to display PDF files, you can disable the built-in PDF Viewer using the following code:
app->profile()->prefs()->disablePdfViewer();
When PDF Viewer is disabled, Molybden suggests to download the PDF file.
PDF Viewer toolbar
By default, PDF Viewer displays the built-in controls such as zoom, rotate, download, and print buttons. You can hide these controls by adding #toolbar=0
to the end of the URL. For example:
browser->loadUrl("file:///Users/Me/Documents/bill.pdf#toolbar=0");
Settings
Using BrowserSettings
you can configure different content settings for a single
Browser
instance. You can disable images, plugins, JavaScript, local storage, application cache, allow
running insecure content or allow JavaScript access clipboard, etc. The list of supported settings you can find in the
API specification.
std::shared_ptr<BrowserSettings> settings = browser->settings();
Default encoding
To configure the default text encoding on a web page use:
settings->setDefaultEncoding("UTF-8");
JavaScript
By default, JavaScript is enabled on a web page. If you need to disable JavaScript, then use the following:
settings->disableJavaScript();
Images
If you do not need to display images on a web page to reduce the network traffic, then you can disable images:
settings->disallowImages();
Plugins
To disable the installed plugins, then use this code:
settings->disablePlugins();
Local storage
The local WebStorage
is enabled by default. In order to disable it and prevent JavaScript from storing date in the
local storage, use the following setting:
settings->disableLocalStorage();
Scrollbars
You might want to hide scrollbars on a web page. In this case use the following setting:
settings->hideScrollbars();
Once you call this method, the web pages loaded in the browser will not display scrollbars anymore.