0

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.

Simran Bhake
  • 117
  • 8

1 Answers1

0

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

Jejun
  • 410
  • 2
  • 13