Question

How to get document properties in the c++ part? For example I want to get the document.title and store it in the c++ part of the firebreath plugin?

if (window && window->getJSObject()->HasProperty("domain")) {
    FB::JSObjectPtr docObj = window->getProperty<FB::JSObjectPtr>("document");

    consoleObj->Invoke("log", FB::variant_list_of("Has obtained document"));

    if(docObj && docObj->HasProperty("domain")){
        m_domain = docObj->getJSObject()->getProperty<std::string>("domain");
        consoleObj->Invoke("log", FB::variant_list_of("Has obtained domain: " + m_domain));
    }

}

But this one fails to compile since docObj has no method HasProperty. I don't know what helper method to use.

Was it helpful?

Solution

Sorry I was sleeping when you ask in the FireBreath chat room. A slightly more abbreviated method would be:

FB::DOM::DocumentPtr dom = m_host->getDOMDocument();
try {
    if (dom && dom->getJSObject()->HasProperty("title")) {
        std::string title = m_host->getDOMDocument()->getProperty<std::string>("title");
    }
} catch (...) {
    // Could not get the title
}

You should always wrap a convert_cast in a try .. catch in case the convert fails. The getProperty abstraction on the DOM::Document object here basically is just doing a convert_cast internally.

OTHER TIPS

In case someone want to know the answer:

FB::DOM::DocumentPtr dom = m_host->getDOMDocument();
if (dom && dom->getJSObject()->HasProperty("title")) {
    std::string title = m_host->getDOMDocument()->getJSObject()->GetProperty("title").convert_cast<std::string>();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top