javascripturlparametersgoogle-apigoogle-2fa

How do I extract url parameters from a chart.googleapis.com url


I want to extract the secret param from https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Fnamemememe%3Anull%3Fsecret%3DXMYUGG7MAT9GFRXA%26issuer%3Dnamemememe

So I should get "XMYUGG7MAT9GFRXA"

I am using JavaScript/React, so far I have tried URLSearchParams(), decodeURIComponent() and query-string library, but none worked.


Solution

  • I believe URLSearchParams() works fine.

    const url = "https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Fnamemememe%3Anull%3Fsecret%3DXMYUGG7MAT9GFRXA%26issuer%3Dnamemememe"
    let otpAuthParams = new URLSearchParams(url).get("chl").split("?")[1].split("&");
    for(let i=0; i<otpAuthParams.length; i++) {
        if(otpAuthParams[i].indexOf("secret=") == 0) {
            console.log(otpAuthParams[i].replace("secret=", ""));
        }
    }

    If the URL is in the address bar, replace the variable url to window.location.search