hack vk

hack vk


/**
 * Open blank popup
 *
 * @param {Object} popupOptions
 * @param {string} name
 * @returns {Promise}
 */
function openBlankPopup({name, popupOptions}) {
    return new Promise(resolve => {
        if (window.popup) {
            window.popup.close();
        }

        const
            width = popupOptions.width || 500,
            height = popupOptions.height || 500,
            options = stringifyOptions({
                width: width,
                height: height,
                top: window.screenY + ((window.outerHeight - height) / 2.5),
                left: window.screenX + ((window.outerWidth - width) / 2)
            });

        window.popup = window.open('', name, options);
        resolve();
    });
}

/**
 * Helper for provider popups initialization
 *
 * @param {string} url
 * @param {string} redirectUri
 */
function initPopup({url, redirectUri}) {
    window.popup.location = url;

    return new Promise((resolve, reject) => {

        const redirectUriParser = document.createElement('a');
        redirectUriParser.href = redirectUri;
        const redirectUriPath = getFullUrlPath(redirectUriParser);

        const polling = setInterval(() => {
            if (!window.popup || window.popup.closed || window.popup.closed === 'undefined') {
                clearInterval(polling);
            }

            try {
                const popupWindowPath = getFullUrlPath(window.popup.location);

                if (popupWindowPath === redirectUriPath) {
                    if (window.popup.location.search || window.popup.location.hash) {
                        const
                            query = parseQueryString(window.popup.location.search.substring(1).replace(/\/$/, '')),
                            hash = parseQueryString(window.popup.location.hash.substring(1).replace(/[\/$]/, '')),
                            params = {...query, ...hash};

                        if (params.error) {
                            reject(params.error);
                        } else {
                            resolve(params);
                        }
                    } else {
                        reject({
                            message: 'OAuth redirect has occurred but no query or hash parameters were found. ' +
                            'They were either not set during the redirect, or were removed—typically by a ' +
                            'routing library—before popup utility could read it.'
                        });
                    }

                    clearInterval(polling);
                    window.popup.close();
                }
            } catch (e) {
                /**
                 * Ignore DOMException: Blocked a frame with origin from accessing a cross-origin frame.
                 * A hack to get around same-origin security policy errors in IE.
                 */
            }
            /**
             * Use quite small interval in order to not miss hash from popup
             */
        }, 50);
    });
}

/**
 * Get full path from url
 *
 * @param {Object} location
 * @returns {string}
 */
function getFullUrlPath(location) {
    const isHttps = location.protocol === 'https:';
    return location.protocol + '//' + location.hostname +
        ':' + (location.port || (isHttps ? '443' : '80')) +
        (/^\//.test(location.pathname) ? location.pathname : '/' + location.pathname);
}