Page tree
Skip to end of metadata
Go to start of metadata

It is possible to invoke methods on the DOM. Below are some examples of invoking such methods

Calling "window.alert" from your plugin -
#include "DOM/Window.h"
...
    // Retrieve a reference to the DOM Window
    FB::DOM::WindowPtr window = m_host->getDOMWindow();

    // Check if the DOM Window has an alert peroperty
    if (window && window->getJSObject()->HasProperty("window")) {
        // Create a reference to alert
        FB::JSObjectPtr obj = window->getProperty<FB::JSObjectPtr>("window");

        // Invoke alert with some text
        obj->Invoke("alert", FB::variant_list_of("This is a test alert invoked from an NPAPI Plugin"));
    }
...

 

Calling "console.log" from your plugin -
#include "DOM/Window.h"
...
    // Retrieve a reference to the DOM Window
    FB::DOM::WindowPtr window = m_host->getDOMWindow();

    // Check if the DOM Window has an the property console
    if (window && window->getJSObject()->HasProperty("console")) {
        // Create a reference to the browswer console object
        FB::JSObjectPtr obj = window->getProperty<FB::JSObjectPtr>("console");

        // Invoke the "log" method on the console object
        obj->Invoke("log", FB::variant_list_of("this is message for the console..."));
    }
...

 

Calling JSON.parse from your plugin -
#include "DOM/Window.h"
...
    // Retrieve a reference to the DOM Window
    FB::DOM::WindowPtr window = m_host->getDOMWindow();

    // A place to put the parse result
    FB::variant json_object;

    // Check if the DOM Window has an in-built JSON Parser
    if (window && window->getJSObject()->HasProperty("JSON")) {
        // Create a reference to the browswer JSON parser
        FB::JSObjectPtr obj = window->getProperty<FB::JSObjectPtr>("JSON");

        // Invoke the "parse" method on the browser JSON object
        json_object = obj->Invoke("parse", FB::variant_list_of("{\"this is a test\":true}"));
    }
... 
Getting DOM properties from your plugin -

It is also possible to get properties from the DOM. For example to retrieve the userAgent property from the navigator object:

#include "DOM/Window.h"
...
    // Retrieve a reference to the DOM Window
    FB::DOM::WindowPtr window = m_host->getDOMWindow();

    // Check if the DOM Window has the navigator object
    if (window && window->getJSObject()->HasProperty("navigator")) {
         std::string userAgent = window->getNode("navigator")->getProperty<std::string>("userAgent");
    }
... 
  • No labels