【发布时间】:2020-04-21 06:03:07
【问题描述】:
【问题讨论】:
【问题讨论】:
我首先要说明一个事实,即使用 URL 传递未加密的用户名和密码是各种不安全的。
但是要解决您的问题。在 JavaScript 中,您可以使用 window.location 访问 URL。这有多个您可以使用的字段。
window.location.host 和 window.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];
我不是正则表达式的明星,所以可能有比我建议的更好的解决方案。
但我希望这可以帮助您解决问题。
【讨论】: