A
A
Andrey Osty2018-04-25 10:09:16
Angular
Andrey Osty, 2018-04-25 10:09:16

Google reCAPTCHA not working in China?

As you know, Google services do not work in China, but I really want Google reCAPTCHA to work. I have a service like this:

import {Injectable, OnDestroy} from "@angular/core";
import {Observable} from "rxjs/Observable";
import {BehaviorSubject} from "rxjs/BehaviorSubject";
import {Router, NavigationEnd} from "@angular/router";
declare var grecaptcha: any;

@Injectable()
export class ReCaptchaDynamicLanguageLoaderService implements OnDestroy {

    ngOnDestroy(): void {
        ReCaptchaDynamicLanguageLoaderService.ready.next(null);
    }

    public ready: Observable<any>;
    public language: string;
    public langParam: string;
    private url = "https://www.recaptcha.net:443/recaptcha/";

    private static ready: BehaviorSubject<any> = new BehaviorSubject<any>(null);

    constructor(
        private router: Router
    ) {
        window["__recaptcha_api"] = this.url;
        this.ng2ReCaptchaLoaded();
        this.setLanguage();
        this.ready = ReCaptchaDynamicLanguageLoaderService.ready.asObservable();
    }

    public updateLanguage(newLang: string): void {
        this.language = newLang;
        ReCaptchaDynamicLanguageLoaderService.ready.next(null);
        this.init();
    }

    private ng2ReCaptchaLoaded(): void {
        window["ng2recaptchaloaded"] = () => {
            ReCaptchaDynamicLanguageLoaderService.ready.next(grecaptcha);
        };
    }

    public setLanguage(lang: string = null): void {
        if (lang) {
            if (lang !== this.language) {
                this.updateLanguage(lang);
            }
        } else {
            this.router.events
                .subscribe((event) => {
                    if (event instanceof NavigationEnd) {
                        this.language = event.url.substring(1, 3);
                        this.init();
                    }
                });
        }
    }

    private addScript(langParam: string): void  {
        let script: HTMLScriptElement = document.createElement("script") as HTMLScriptElement;
        script.innerHTML = "";
        script.src = `${this.url}api.js?render=explicit&onload=ng2recaptchaloaded${langParam}`;
        script.async = true;
        script.defer = true;
        document.head.appendChild(script);
    }

    private init(): void {
        let langParam: string = this.language
            ? "&hl=" + this.language
            : "";
        if (this.langParam !== langParam) {
            this.langParam = langParam;
            if (ReCaptchaDynamicLanguageLoaderService.ready) {
                let isActive: any = ReCaptchaDynamicLanguageLoaderService.ready.getValue();
                if (isActive) {
                    return;
                }
            }
            this.addScript(langParam);
        }
    }
}

Where I try to bypass the "great" Chinese firewall through https://www.recaptcha.net:443/recaptcha/
I get an error in the console:
VM791 recaptcha__en.js:218 Uncaught DOMException: Failed to construct 'Worker': Script at 'https://www.google.com/recaptcha/api2/webworker.js?hl=en&v=v1523860362251' cannot be accessed from origin 'https://www.recaptcha.net'.
    at new Xm (https://www.gstatic.com/recaptcha/api2/v1523860362251/recaptcha__en.js:218:623)
    at new Ao (https://www.gstatic.com/recaptcha/api2/v1523860362251/recaptcha__en.js:247:433)
    at Object.init (https://www.gstatic.com/recaptcha/api2/v1523860362251/recaptcha__en.js:248:72)
    at https://www.recaptcha.net/recaptcha/api2/anchor?k=6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI&co=aHR0cDovL2xvY2FsaG9zdDo1NjU5Nw..&hl=en&v=v1523860362251&size=normal&cb=gtaulfjyul46:197:29

I get an error in Google reCAPTCHA:
Please upgrade to a supported browser to get a reCAPTCHA challenge.

Alternatively if you think you are getting this page in error, please check your internet connection and reload.

Why is this happening to me?

5ae02923b7b42160520653.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question