Skip to content

Instantly share code, notes, and snippets.

@khrona
Created October 3, 2011 22:21
Show Gist options
  • Save khrona/1260421 to your computer and use it in GitHub Desktop.
Save khrona/1260421 to your computer and use it in GitHub Desktop.
A full example based off the "HelloAwesomium" sample (1.6.2) that demonstrates how to load a page from Wikipedia, jump to a certain named anchor, and render a screenshot of the result.
// Various included headers
#include <Awesomium/WebCore.h>
#include <iostream>
#if defined(__WIN32__) || defined(_WIN32)
#include <windows.h>
#elif defined(__APPLE__)
#include <unistd.h>
#endif
// Various macro definitions
#define WIDTH 512
#define HEIGHT 512
#define URL "http://en.wikipedia.org/wiki/Couch"
#define SLEEP_MS 50
// Helper function that updates the WebCore and sleeps a bit
void updateCore();
// Our main program
int main()
{
// Create our WebCore singleton with the default options
Awesomium::WebCore* webCore = new Awesomium::WebCore();
// Create a new WebView instance with a certain width and height, using the
// WebCore we just created
Awesomium::WebView* webView = webCore->createWebView(WIDTH, HEIGHT);
// Load a certain URL into our WebView instance
webView->loadURL(URL);
std::cout << "Page is now loading..." << std::endl;;
// Wait for our WebView to finish loading
while(webView->isLoadingPage())
{
updateCore();
}
std::cout << "Page has finished loading." << std::endl;
// Execute some JS that will cause the page to jump to
// the anchor named 'Types' and return a value of 0
Awesomium::FutureJSValue futureVal =
webView->executeJavascriptWithResult(L"window.location.hash = 'Types', 0");
// This will block the main thread until the result has been
// obtained from the child-process.
futureVal.get();
// Update the WebCore one more time for good measure
updateCore();
// Get our rendered buffer from our WebView. All actual rendering takes
// place in our WebView sub-process which passes the rendered data to our
// main process during each call to WebCore::update.
const Awesomium::RenderBuffer* renderBuffer = webView->render();
// Make sure our render buffer is not NULL-- WebView::render will return
// NULL if the WebView process has crashed.
if(renderBuffer != NULL)
{
// Save our RenderBuffer directly to a JPEG image
renderBuffer->saveToJPEG(L"./result.jpg");
std::cout << "Saved a render of the page to 'result.jpg'." << std::endl;
// Open up the saved JPEG
#if defined(__WIN32__) || defined(_WIN32)
system("start result.jpg");
#elif defined(__APPLE__)
system("open result.jpg");
#endif
}
// Destroy our WebView instance
webView->destroy();
// Destroy our WebCore instance
delete webCore;
return 0;
}
void updateCore()
{
// Sleep a little bit so we give other threads some time
#if defined(__WIN32__) || defined(_WIN32)
Sleep(SLEEP_MS);
#elif defined(__APPLE__)
usleep(SLEEP_MS * 1000);
#endif
// Update the WebCore and check for messages from
// each child-process
Awesomium::WebCore::Get().update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment