如何从 chart.googleapis.com url 中提取 url 参数

时间:2021-03-18 05:43:23

标签: javascript url parameters google-api google-2fa

我想从 https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Fnamemememe%3Anull%3Fsecret%3DXMYUGG7MAT9GFRXA%26issuer%3Dnamemememe 中提取秘密参数

所以我应该得到“XMYUGG7MAT9GFRXA”

我正在使用 JavaScript/React,到目前为止我已经尝试过 URLSearchParams()、decodeURIComponent() 和查询字符串库,但都没有奏效。

1 个答案:

答案 0 :(得分:0)

我相信 URLSearchParams() 工作正常。

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=", ""));
    }
}

如果网址在地址栏中,请将变量 url 替换为 window.location.search

相关问题