是否可以使用 Javascript 自动登录 link
Is it possible to automatically log in to link using Javascript
我想 link 到一个页面,当用户点击 link 时,他的用户名和密码已经存在。但是,我也 linking 在哪里,我无法控制该页面代码。
是否可以在用户单击 link 后让我的 javascript 在那里执行?
$("#link").click(function() {
alert( "Handler for .click() called." );
var username = getCookie("username");
var password = getCookie("password");
var usernameTextBox = document.getElementById("j_username");
var passwordTextBox = document.getElementById("j_password");
usernameTextBox.value = username;
passwordTextBox.value = password;
});
您实际上不能从另一个页面执行它,但是您可以使用以下内容。
最好的方法是使用 cookie,存储用户名,并指向一个带有加密密码的加密文件。但是您也可以将加密后的密码存储在 cookie 中,只要在将其放入 cookie 之前对其进行加密即可。
我最初开发这个功能是为了让用户保持登录到一个页面,无论何时访问它都会重定向到登录名,它会点击按钮并将他们带到页面登录页面。
工作示例:将需要进行编辑以适合任何页面上的确切元素,以及页面上的 运行,可能通过 属性 或通过下载或网络应用程序等获得许可
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function stayLoggedIn() {
setCookie("logged_in","true",30);
setCookie("username", username,30);
setCookie("password",encryptedPassword,30);
return null;
}
window.onload {
var loggedIn = getCookie("logged_in");
if(loggedIn == true) {
var username = getCookie("username");
var password = getCookie("password");
var usernameTextBox = document.getElementById("username");
var passwordTextBox = document.getElementById("password");
//decrypt password here.
usernameTextBox.value = username;
passwordTextBox.value = password;
}
else {}
}
解释:
首先,我们设置一个函数来设置一个 cookie 并获取一个,我从 here
中获取了这些
然后,我设置函数 stayLoggedIn()
,这会将值为 "logged_in"
的 cookie 设置为 true
,因此当用户来到该页面时,window.onload
运行是块,触发if
语句,填写用户名和密码字段。
然后,在 logginButton 上调用 click(element)
,这可以单击 php 或 html 按钮或提交表单等。这模拟按钮被单击,用户登录。
此外:您需要在单击 link 后调用 stayLoggedIn()
函数(例如通过 google 或 firefox 扩展)
我想 link 到一个页面,当用户点击 link 时,他的用户名和密码已经存在。但是,我也 linking 在哪里,我无法控制该页面代码。
是否可以在用户单击 link 后让我的 javascript 在那里执行?
$("#link").click(function() {
alert( "Handler for .click() called." );
var username = getCookie("username");
var password = getCookie("password");
var usernameTextBox = document.getElementById("j_username");
var passwordTextBox = document.getElementById("j_password");
usernameTextBox.value = username;
passwordTextBox.value = password;
});
您实际上不能从另一个页面执行它,但是您可以使用以下内容。
最好的方法是使用 cookie,存储用户名,并指向一个带有加密密码的加密文件。但是您也可以将加密后的密码存储在 cookie 中,只要在将其放入 cookie 之前对其进行加密即可。
我最初开发这个功能是为了让用户保持登录到一个页面,无论何时访问它都会重定向到登录名,它会点击按钮并将他们带到页面登录页面。
工作示例:将需要进行编辑以适合任何页面上的确切元素,以及页面上的 运行,可能通过 属性 或通过下载或网络应用程序等获得许可
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function stayLoggedIn() {
setCookie("logged_in","true",30);
setCookie("username", username,30);
setCookie("password",encryptedPassword,30);
return null;
}
window.onload {
var loggedIn = getCookie("logged_in");
if(loggedIn == true) {
var username = getCookie("username");
var password = getCookie("password");
var usernameTextBox = document.getElementById("username");
var passwordTextBox = document.getElementById("password");
//decrypt password here.
usernameTextBox.value = username;
passwordTextBox.value = password;
}
else {}
}
解释:
首先,我们设置一个函数来设置一个 cookie 并获取一个,我从 here
中获取了这些
然后,我设置函数 stayLoggedIn()
,这会将值为 "logged_in"
的 cookie 设置为 true
,因此当用户来到该页面时,window.onload
运行是块,触发if
语句,填写用户名和密码字段。
然后,在 logginButton 上调用 click(element)
,这可以单击 php 或 html 按钮或提交表单等。这模拟按钮被单击,用户登录。
此外:您需要在单击 link 后调用 stayLoggedIn()
函数(例如通过 google 或 firefox 扩展)