【问题标题】:Get username and password from URL and paste into text box从 URL 获取用户名和密码并粘贴到文本框中
【发布时间】:2020-04-21 06:03:07
【问题描述】:

当我点击一个 url 并在新选项卡中打开它时,我试图从 url 复制用户名和密码,并将用户名和密码填写在文本框中。我搜索了谷歌,但无法找到解决方案。有人可以帮忙吗?

【问题讨论】:

    标签: c# html jquery


    【解决方案1】:

    我首先要说明一个事实,即使用 URL 传递未加密的用户名和密码是各种不安全的。

    但是要解决您的问题。在 JavaScript 中,您可以使用 window.location 访问 URL。这有多个您可以使用的字段。

    window.location.hostwindow.location.hostname 将返回“stackoverflow.com”

    window.location.href 将返回整个网址“Get username and password from URL and paste into text box

    window.location.pathname 将返回host "/questions/61336907/get-username-and-password-from-url-and-paste-into-text-box" 之后的部分

    window.location.search 将返回一个参数,如果它是 url 的一部分。 “https://stackoverflow.com?username=unsafe&password=password123”将返回“?username=unsafe&password=password123”。

    然后您就可以使用正则表达式从 url 获取用户名和密码。

    const match = window.location.search.match( /username=(.*)&password=(.*)/g )
    // results in array [ "username=unsafe&password=password123", "unsafe", "password123" ]
    // first array item match[0] is the full match
    // second item match[1] is the first group (you can create a group by using round brackets)
    // third item match[2] is the second group
    const username = match[1];
    const password = match[2];
    

    我不是正则表达式的明星,所以可能有比我建议的更好的解决方案。

    但我希望这可以帮助您解决问题。

    【讨论】:

    • 我没有访问代码,它是另一个网站意味着第三方。我只有网站的网址、用户名和密码。我将如何做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2021-12-22
    • 2018-08-18
    • 1970-01-01
    相关资源
    最近更新 更多