首页 > 解决方案 > 从 URL 获取用户名和密码并粘贴到文本框中

问题描述

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

在此处输入图像描述

标签: c#htmljquery

解决方案


我首先想解决这样一个事实,即使用 URL 传递未加密的用户名和密码是各种不安全的。

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

window.location.host并将window.location.hostname返回“stackoverflow.com”

window.location.href将返回整个 url “从 URL 中获取用户名和密码并粘贴到文本框中

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];

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

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


推荐阅读