Exemplo n.º 1
0
(global => {
    const { DOMParser } = require('xmldom');

    // DOMParser
    //
    // Required by:
    // - lib-jitsi-meet requires this if using WebSockets
    global.DOMParser = DOMParser;

    // addEventListener
    //
    // Required by:
    // - jQuery
    if (typeof global.addEventListener === 'undefined') {
        // eslint-disable-next-line no-empty-function
        global.addEventListener = () => {};
    }

    // Array.prototype[@@iterator]
    //
    // Required by:
    // - for...of statement use(s) in lib-jitsi-meet
    const arrayPrototype = Array.prototype;

    if (typeof arrayPrototype['@@iterator'] === 'undefined') {
        arrayPrototype['@@iterator'] = function() {
            return new Iterator(this);
        };
    }

    // document
    //
    // Required by:
    // - jQuery
    // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
    // - Strophe
    if (typeof global.document === 'undefined') {
        const document
            = new DOMParser().parseFromString(
                '<html><head></head><body></body></html>',
                'text/xml');

        // document.addEventListener
        //
        // Required by:
        // - jQuery
        if (typeof document.addEventListener === 'undefined') {
            // eslint-disable-next-line no-empty-function
            document.addEventListener = () => {};
        }

        // document.cookie
        //
        // Required by:
        // - herment
        if (typeof document.cookie === 'undefined') {
            document.cookie = '';
        }

        // document.implementation
        //
        // Required by:
        // - jQuery
        if (typeof document.implementation === 'undefined') {
            document.implementation = {};
        }

        // document.implementation.createHTMLDocument
        //
        // Required by:
        // - jQuery
        if (typeof document.implementation.createHTMLDocument === 'undefined') {
            document.implementation.createHTMLDocument = function(title = '') {
                const htmlDocument
                    = new DOMParser().parseFromString(
                        `<html>
                            <head><title>${title}</title></head>
                            <body></body>
                        </html>`,
                        'text/xml');

                Object.defineProperty(htmlDocument, 'body', {
                    get() {
                        return htmlDocument.getElementsByTagName('body')[0];
                    }
                });

                return htmlDocument;
            };
        }

        // Element.querySelector
        //
        // Required by:
        // - lib-jitsi-meet/modules/xmpp
        const elementPrototype
            = Object.getPrototypeOf(document.documentElement);

        if (elementPrototype) {
            if (typeof elementPrototype.querySelector === 'undefined') {
                elementPrototype.querySelector = function(selectors) {
                    return _querySelector(this, selectors);
                };
            }

            // Element.remove
            //
            // Required by:
            // - lib-jitsi-meet ChatRoom#onPresence parsing
            if (typeof elementPrototype.remove === 'undefined') {
                elementPrototype.remove = function() {
                    if (this.parentNode !== null) {
                        this.parentNode.removeChild(this);
                    }
                };
            }

            // Element.innerHTML
            //
            // Required by:
            // - jQuery's .append method
            if (!elementPrototype.hasOwnProperty('innerHTML')) {
                Object.defineProperty(elementPrototype, 'innerHTML', {
                    get() {
                        return this.childNodes.toString();
                    },

                    set(innerHTML) {
                        // MDN says: removes all of element's children, parses
                        // the content string and assigns the resulting nodes as
                        // children of the element.

                        // Remove all of element's children.
                        this.textContent = '';

                        // Parse the content string.
                        const d
                            = new DOMParser().parseFromString(
                                `<div>${innerHTML}</div>`,
                                'text/xml');

                        // Assign the resulting nodes as children of the
                        // element.
                        const documentElement = d.documentElement;
                        let child;

                        // eslint-disable-next-line no-cond-assign
                        while (child = documentElement.firstChild) {
                            this.appendChild(child);
                        }
                    }
                });
            }

            // Element.children
            //
            // Required by:
            // - lib-jitsi-meet ChatRoom#onPresence parsing
            if (!elementPrototype.hasOwnProperty('children')) {
                Object.defineProperty(elementPrototype, 'children', {
                    get() {
                        const nodes = this.childNodes;
                        const children = [];
                        let i = 0;
                        let node = nodes[i];

                        while (node) {
                            if (node.nodeType === 1) {
                                children.push(node);
                            }
                            i += 1;
                            node = nodes[i];
                        }

                        return children;
                    }
                });
            }
        }

        // FIXME There is a weird infinite loop related to console.log and
        // Document and/or Element at the time of this writing. Work around it
        // by patching Node and/or overriding console.log.
        const documentPrototype = Object.getPrototypeOf(document);
        const nodePrototype
            = _getCommonPrototype(documentPrototype, elementPrototype);

        if (nodePrototype

                // XXX The intention was to find Node from which Document and
                // Element extend. If for whatever reason we've reached Object,
                // then it doesn't sound like what expected.
                && nodePrototype !== Object.getPrototypeOf({})) {
            // Override console.log.
            const { console } = global;

            if (console) {
                const loggerLevels = require('jitsi-meet-logger').levels;

                Object.keys(loggerLevels).forEach(key => {
                    const level = loggerLevels[key];
                    const consoleLog = console[level];

                    /* eslint-disable prefer-rest-params */

                    if (typeof consoleLog === 'function') {
                        console[level] = function(...args) {
                            // XXX If console's disableYellowBox is truthy, then
                            // react-native will not automatically display the
                            // yellow box for the warn level. However, it will
                            // still display the red box for the error level.
                            // But I disable the yellow box when I don't want to
                            // have react-native automatically show me the
                            // console's output just like in the Release build
                            // configuration. Because I didn't find a way to
                            // disable the red box, downgrade the error level to
                            // warn. The red box will still be displayed but not
                            // for the error level.
                            if (console.disableYellowBox && level === 'error') {
                                console.warn(...args);

                                return;
                            }

                            const { length } = args;

                            for (let i = 0; i < length; ++i) {
                                let arg = args[i];

                                if (arg
                                        && typeof arg !== 'string'

                                        // Limit the console.log override to
                                        // Node (instances).
                                        && nodePrototype.isPrototypeOf(arg)) {
                                    const toString = arg.toString;

                                    if (toString) {
                                        arg = toString.call(arg);
                                    }
                                }
                                args[i] = arg;
                            }

                            consoleLog.apply(this, args);
                        };
                    }

                    /* eslint-enable prefer-rest-params */
                });
            }
        }

        global.document = document;
    }

    // location
    if (typeof global.location === 'undefined') {
        global.location = {
            href: '',

            // Required by:
            // - lib-jitsi-meet/modules/xmpp/xmpp.js
            search: ''
        };
    }

    const { navigator } = global;

    if (navigator) {
        // platform
        //
        // Required by:
        // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
        if (typeof navigator.platform === 'undefined') {
            navigator.platform = '';
        }

        // plugins
        //
        // Required by:
        // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
        if (typeof navigator.plugins === 'undefined') {
            navigator.plugins = [];
        }

        // userAgent
        //
        // Required by:
        // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
        // - lib-jitsi-meet/modules/browser/BrowserDetection.js
        let userAgent = navigator.userAgent || '';

        // react-native/version
        const { name, version } = require('react-native/package.json');
        let rn = name || 'react-native';

        version && (rn += `/${version}`);
        if (userAgent.indexOf(rn) === -1) {
            userAgent = userAgent ? `${rn} ${userAgent}` : rn;
        }

        // (OS version)
        const os = `(${Platform.OS} ${Platform.Version})`;

        if (userAgent.indexOf(os) === -1) {
            userAgent = userAgent ? `${userAgent} ${os}` : os;
        }

        navigator.userAgent = userAgent;
    }

    // WebRTC
    require('./polyfills-webrtc');
    require('react-native-callstats/csio-polyfill');

    // XMLHttpRequest
    if (global.XMLHttpRequest) {
        const { prototype } = global.XMLHttpRequest;

        // XMLHttpRequest.responseXML
        //
        // Required by:
        // - Strophe
        if (prototype && !prototype.hasOwnProperty('responseXML')) {
            Object.defineProperty(prototype, 'responseXML', {
                get() {
                    const { responseText } = this;

                    return (
                        responseText
                            && new DOMParser().parseFromString(
                                responseText,
                                'text/xml'));
                }
            });
        }
    }

    // Timers
    //
    // React Native's timers won't run while the app is in the background, this
    // is a known limitation. Replace them with a background-friendly
    // alternative.
    //
    // Required by:
    // - lib-jitsi-meet
    // - Strophe
    global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
    global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
    global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
    global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);

})(global || window || this); // eslint-disable-line no-invalid-this
Exemplo n.º 2
0
(global => {
    const DOMParser = require('xmldom').DOMParser;

    // addEventListener
    //
    // Required by:
    // - jQuery
    if (typeof global.addEventListener === 'undefined') {
        // eslint-disable-next-line no-empty-function
        global.addEventListener = () => {};
    }

    // Array.prototype[@@iterator]
    //
    // Required by:
    // - for...of statement use(s) in lib-jitsi-meet
    const arrayPrototype = Array.prototype;

    if (typeof arrayPrototype['@@iterator'] === 'undefined') {
        arrayPrototype['@@iterator'] = function() {
            return new Iterator(this);
        };
    }

    // document
    //
    // Required by:
    // - jQuery
    // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
    // - Strophe
    if (typeof global.document === 'undefined') {
        const document
            = new DOMParser().parseFromString(
                '<html><head></head><body></body></html>',
                'text/xml');

        // document.addEventListener
        //
        // Required by:
        // - jQuery
        if (typeof document.addEventListener === 'undefined') {
            // eslint-disable-next-line no-empty-function
            document.addEventListener = () => {};
        }

        // Document.querySelector
        //
        // Required by:
        // - strophejs-plugins/caps/strophe.caps.jsonly.js
        const documentPrototype = Object.getPrototypeOf(document);

        if (documentPrototype) {
            if (typeof documentPrototype.querySelector === 'undefined') {
                documentPrototype.querySelector = function(selectors) {
                    return _querySelector(this.elementNode, selectors);
                };
            }
        }

        // Element.querySelector
        //
        // Required by:
        // - strophejs-plugins/caps/strophe.caps.jsonly.js
        const elementPrototype
            = Object.getPrototypeOf(document.documentElement);

        if (elementPrototype) {
            if (typeof elementPrototype.querySelector === 'undefined') {
                elementPrototype.querySelector = function(selectors) {
                    return _querySelector(this, selectors);
                };
            }

            // Element.innerHTML
            //
            // Required by:
            // - jQuery's .append method
            if (!elementPrototype.hasOwnProperty('innerHTML')) {
                Object.defineProperty(elementPrototype, 'innerHTML', {
                    get() {
                        return this.childNodes.toString();
                    },

                    set(innerHTML) {
                        // MDN says: removes all of element's children, parses
                        // the content string and assigns the resulting nodes as
                        // children of the element.

                        // Remove all of element's children.
                        this.textContent = '';

                        // Parse the content string.
                        const d
                            = new DOMParser().parseFromString(
                                `<div>${innerHTML}</div>`,
                                'text/xml');

                        // Assign the resulting nodes as children of the
                        // element.
                        const documentElement = d.documentElement;
                        let child;

                        // eslint-disable-next-line no-cond-assign
                        while (child = documentElement.firstChild) {
                            this.appendChild(child);
                        }
                    }
                });
            }
        }

        // FIXME There is a weird infinite loop related to console.log and
        // Document and/or Element at the time of this writing. Work around it
        // by patching Node and/or overriding console.log.
        const nodePrototype
            = _getCommonPrototype(documentPrototype, elementPrototype);

        if (nodePrototype

                // XXX The intention was to find Node from which Document and
                // Element extend. If for whatever reason we've reached Object,
                // then it doesn't sound like what expected.
                && nodePrototype !== Object.getPrototypeOf({})) {
            // Override console.log.
            const console = global.console;

            if (console) {
                const loggerLevels = require('jitsi-meet-logger').levels;

                Object.keys(loggerLevels).forEach(key => {
                    const level = loggerLevels[key];
                    const consoleLog = console[level];

                    /* eslint-disable prefer-rest-params */

                    if (typeof consoleLog === 'function') {
                        console[level] = function(...args) {
                            const length = args.length;

                            for (let i = 0; i < length; ++i) {
                                let arg = args[i];

                                if (arg
                                        && typeof arg !== 'string'

                                        // Limit the console.log override to
                                        // Node (instances).
                                        && nodePrototype.isPrototypeOf(arg)) {
                                    const toString = arg.toString;

                                    if (toString) {
                                        arg = toString.call(arg);
                                    }
                                }
                                args[i] = arg;
                            }

                            consoleLog.apply(this, args);
                        };
                    }

                    /* eslint-enable prefer-rest-params */
                });
            }
        }

        global.document = document;
    }

    // location
    if (typeof global.location === 'undefined') {
        global.location = {
            href: '',

            // Required by:
            // - lib-jitsi-meet/modules/xmpp/xmpp.js
            search: ''
        };
    }

    const { navigator } = global;

    if (navigator) {
        // platform
        //
        // Required by:
        // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
        if (typeof navigator.platform === 'undefined') {
            navigator.platform = '';
        }

        // plugins
        //
        // Required by:
        // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
        if (typeof navigator.plugins === 'undefined') {
            navigator.plugins = [];
        }

        // userAgent
        //
        // Required by:
        // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
        // - lib-jitsi-meet/modules/RTC/RTCBrowserType.js
        (() => {
            const reactNativePackageJSON = require('react-native/package.json');
            let userAgent = reactNativePackageJSON.name || 'react-native';

            const version = reactNativePackageJSON.version;

            if (version) {
                userAgent += `/${version}`;
            }

            if (typeof navigator.userAgent !== 'undefined') {
                const s = navigator.userAgent.toString();

                if (s.length > 0 && s.indexOf(userAgent) === -1) {
                    userAgent = `${s} ${userAgent}`;
                }
            }

            navigator.userAgent = userAgent;
        })();
    }

    // performance
    if (typeof global.performance === 'undefined') {
        global.performance = {
            now() {
                return 0;
            }
        };
    }

    // sessionStorage
    //
    // Required by:
    // - Strophe
    if (typeof global.sessionStorage === 'undefined') {
        global.sessionStorage = {
            /* eslint-disable no-empty-function */
            getItem() {},
            removeItem() {},
            setItem() {}

            /* eslint-enable no-empty-function */
        };
    }

    // WebRTC
    require('./polyfills-webrtc');

    // XMLHttpRequest
    if (global.XMLHttpRequest) {
        const prototype = global.XMLHttpRequest.prototype;

        // XMLHttpRequest.responseXML
        //
        // Required by:
        // - Strophe
        if (prototype && !prototype.hasOwnProperty('responseXML')) {
            Object.defineProperty(prototype, 'responseXML', {
                get() {
                    const responseText = this.responseText;
                    let responseXML;

                    if (responseText) {
                        responseXML
                            = new DOMParser().parseFromString(
                                responseText,
                                'text/xml');
                    }

                    return responseXML;
                }
            });
        }
    }

    // Timers
    //
    // React Native's timers won't run while the app is in the background, this
    // is a known limitation. Replace them with a background-friendly
    // alternative.
    //
    // Required by:
    // - lib-jitsi-meet
    // - Strophe
    global.clearTimeout
        = window.clearTimeout
        = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
    global.clearInterval
        = window.clearInterval
        = BackgroundTimer.clearInterval.bind(BackgroundTimer);
    global.setInterval
        = window.setInterval
        = BackgroundTimer.setInterval.bind(BackgroundTimer);
    global.setTimeout
        = window.setTimeout
        = BackgroundTimer.setTimeout.bind(BackgroundTimer);

})(global || window || this); // eslint-disable-line no-invalid-this