GreaseMonkey脚本使用HTTP身份验证自动登录

jkl*_*klp 6 javascript authentication greasemonkey http http-authentication

我在我的工作中写了很多GreaseMonkey脚本,它会自动登录我们这里的内部网站.除了我们的时间表应用程序(使用HTTP身份验证)之外,我已经设法为几乎每个站点编写脚本.

有没有办法可以使用GreaseMonkey自动登录我这个网站?

编辑:我知道浏览器中的商店密码功能,但是我的脚本更进一步,检查我是否在加载时(通过遍历HTML)登录到站点,然后将帖子提交到登录页面.这消除了必须加载站点,进入登录页面,输入我的凭据,然后点击提交的步骤

jkl*_*klp 6

可以通过设置"Authorization"HTTP标头使用HTTP身份验证登录,并将此标头的值设置为字符串"basic username:password",但使用字符串Base 64编码的"username:password"部分.

http://frontier.userland.com/stories/storyReader$2159

一些研究发现GreaseMonkey内置了一个函数,您可以将GET/POST请求发送到名为GM_xmlhttpRequest的服务器

http://diveintogreasemonkey.org/api/gm_xmlhttprequest.html

所以把它们放在一起(并且还获得这个JavaScript代码将字符串转换为base64我得到以下内容

http://www.webtoolkit.info/javascript-base64.html

var loggedInText = document.getElementById('metanav').firstChild.firstChild.innerHTML;
if (loggedInText != "logged in as jklp") {
    var username = 'jklp';
    var password = 'jklpPass';
    var base64string = Base64.encode(username + ":" + password);

    GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://foo.com/trac/login',
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
            'Accept': 'application/atom+xml,application/xml,text/xml',
            'Authorization':'Basic ' + base64string,
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,当我现在访问该网站时,它会遍历DOM,如果我没有登录,它会自动登录我.