【问题标题】:How do I extract url parameters from a chart.googleapis.com url如何从 chart.googleapis.com url 中提取 url 参数
【发布时间】:2021-06-15 12:25:17
【问题描述】:

我想从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() 和查询字符串库,但都没有成功。

【问题讨论】:

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


    【解决方案1】:

    我相信 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

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 1970-01-01
      • 2015-05-30
      • 2014-09-03
      • 2011-01-30
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      相关资源
      最近更新 更多