Modules (180)

LiveDevProtocol

Description

Dependencies

Variables

Private

_connections

Type
Object
Private
    var _connections = {};
Private

_nextMsgId

Type
number
Private
    var _nextMsgId = 1;
Private

_responseDeferreds

Type
Object
Private
    var _responseDeferreds = {};
Private

_transport

Type
{start: function(), send: function(number, Array.<number>, string), close: function(number), getRemoteScript: function(): ?string}
Private
    var _transport = null;

Functions

Private

_close

clientId number
    function _close(clientId) {
        delete _connections[clientId];
        exports.trigger("ConnectionClose", {
            clientId: clientId
        });
    }
Private

_connect

clientId number
url string
    function _connect(clientId, url) {
        // add new connections
        // TODO: check URL
        _connections[clientId] = true;

        exports.trigger("ConnectionConnect", {
            clientId: clientId,
            url: url
        });
    }
Private

_receive

clientId number
ID of the client that sent the message
msg string
The message that was sent, in JSON string format
    function _receive(clientId, msgStr) {
        var msg = JSON.parse(msgStr),
            event = msg.method || "event",
            deferred;
        if (msg.id) {
            deferred = _responseDeferreds[msg.id];
            if (deferred) {
                delete _responseDeferreds[msg.id];
                if (msg.error) {
                    deferred.reject(msg);
                } else {
                    deferred.resolve(msg);
                }
            }
        } else {
            // enrich received message with clientId
            msg.clientId = clientId;
            exports.trigger(event, msg);
        }
    }
Private

_send

msg Object
The message to send.
idOrArray number,Array.<number>
ID or IDs of the client(s) that should receive the message.
Returns: $.Promise
A promise that's fulfilled when the response to the message is received.
    function _send(msg, clients) {
        var id = _nextMsgId++,
            result = new $.Deferred();

        // broadcast if there are no specific clients
        clients = clients || getConnectionIds();
        msg.id = id;
        _responseDeferreds[id] = result;
        _transport.send(clients, JSON.stringify(msg));
        return result.promise();
    }
Public API

close

Closes the connection to the given client. Proxies to the transport.

clientId number
    function close(clientId) {
        _transport.close(clientId);
    }

    function closeAllConnections() {
        getConnectionIds().forEach(function (clientId) {
            close(clientId);
        });
        _connections = {};
    }

    EventDispatcher.makeEventDispatcher(exports);

    // public API
    exports.setTransport = setTransport;
    exports.getRemoteScript = getRemoteScript;
    exports.evaluate = evaluate;
    exports.setStylesheetText = setStylesheetText;
    exports.getStylesheetText = getStylesheetText;
    exports.reload = reload;
    exports.navigate = navigate;
    exports.close = close;
    exports.getConnectionIds = getConnectionIds;
    exports.closeAllConnections = closeAllConnections;
});
Public API

evaluate

Protocol method. Evaluates the given script in the browser (in global context), and returns a promise that will be fulfilled with the result of the script, if any.

clients number,Array.<number>
A client ID or array of client IDs that should evaluate the script.
script string
The script to evalute.
Returns: $.Promise
A promise that's resolved with the return value from the first client that responds to the evaluation.
    function evaluate(script, clients) {
        return _send(
            {
                method: "Runtime.evaluate",
                params: {
                    expression: script
                }
            },
            clients
        );
    }
Public API

getConnectionIds

Returns an array of the client IDs that are being managed by this live document.

Returns: Array.<number>
    function getConnectionIds() {
        return Object.keys(_connections);
    }

getRemoteFunctionsScript

Returns a script that should be injected into the HTML that's launched in the browser in order to implement remote commands that handle protocol requests. Includes the <script> tags.

Returns: string
    function getRemoteFunctionsScript() {
        var script = "";
        // Inject DocumentObserver into the browser (tracks related documents)
        script += DocumentObserver;
        // Inject remote functions into the browser.
        script += "window._LD=(" + RemoteFunctions + "())";
        return "<script>\n" + script + "</script>\n";
    }
Public API

getRemoteScript

Returns a script that should be injected into the HTML that's launched in the browser in order to handle protocol requests. Includes the <script> tags. This script will also include the script required by the transport, if any.

Returns: string
    function getRemoteScript() {
        var transportScript = _transport.getRemoteScript() || "";
        var remoteFunctionsScript = getRemoteFunctionsScript() || "";
        return transportScript +
            "<script>\n" + LiveDevProtocolRemote + "</script>\n" +
            remoteFunctionsScript;
    }
Public API

getStylesheetText

Protocol method. Rretrieves the content of a given stylesheet (for unit testing)

clients number,Array.<number>
A client ID or array of client IDs that should navigate to the given URL.
url string
Absolute URL that identifies the stylesheet.
Returns: $.Promise
A promise that's resolved with the return value from the first client that responds to the method.
    function getStylesheetText(url, clients) {
        return _send(
            {
                method: "CSS.getStylesheetText",
                params: {
                    url: url
                }
            },
            clients
        );
    }
Public API

navigate

Protocol method. Navigates current page to the given URL.

clients number,Array.<number>
A client ID or array of client IDs that should navigate to the given URL.
url string
URL to navigate the page to.
Returns: $.Promise
A promise that's resolved with the return value from the first client that responds to the method.
    function navigate(url, clients) {
        return _send(
            {
                method: "Page.navigate",
                params: {
                    url: url
                }
            },
            clients
        );
    }
Public API

reload

Protocol method. Reloads the page that is currently loaded into the browser, optionally ignoring cache.

clients number,Array.<number>
A client ID or array of client IDs that should reload the page.
ignoreCache boolean
If true, browser cache is ignored.
Returns: $.Promise
A promise that's resolved with the return value from the first client that responds to the method.
    function reload(ignoreCache, clients) {
        return _send(
            {
                method: "Page.reload",
                params: {
                    ignoreCache: true
                }
            },
            clients
        );
    }
Public API

setStylesheetText

Protocol method. Reloads a CSS styleseet in the browser (by replacing its text) given its url.

url string
Absolute URL of the stylesheet
text string
The new text of the stylesheet
clients number,Array.<number>
A client ID or array of client IDs that should evaluate the script.
Returns: $.Promise
A promise that's resolved with the return value from the first client that responds to the evaluation.
    function setStylesheetText(url, text, clients) {
        return _send(
            {
                method: "CSS.setStylesheetText",
                params: {
                    url: url,
                    text: text
                }
            }
        );
    }
Public API

setTransport

Sets the transport that should be used by the protocol. See LiveDevelopment.setTransport() for more detail on the transport.

transport {start: function(string),send: function(number,Array.<number>,string),close: function(number),getRemoteScript: function(): ?string}
    function setTransport(transport) {
        if (_transport) {
            _transport.off(".livedev");
        }
        _transport = transport;

        _transport
            .on("connect.livedev", function (event, msg) {
                _connect(msg[0], msg[1]);
            })
            .on("message.livedev", function (event, msg) {
                _receive(msg[0], msg[1]);
            })
            .on("close.livedev", function (event, msg) {
                _close(msg[0]);
            });
        _transport.start();
    }