这是用于获取和存储登录凭证的框架。† 脚本会在首次运行时提示您输入信息,并使用对其进行加密存储
GM_setValue()。
它还将两个项目添加到Greasemonkey上下文菜单中,以允许更改用户名或密码。
// ==Userscript==// @name _Autologin, sensitive info framework// @include http://YOUR_SERVER.COM/YOUR_PATH/*// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js// @require http://crypto.stanford.edu/sjcl/sjcl.js// @grant GM_getValue// @grant GM_setValue// @grant GM_registerMenuCommand// ==/Userscript==var encKey = GM_getValue ("encKey", "");var usr = GM_getValue ("lognUsr", "");var pword = GM_getValue ("lognPwd", "");if ( ! encKey) { encKey = prompt ( 'script key not set for ' + location.hostname + '. Please enter a random string:', '' ); GM_setValue ("encKey", encKey); usr = pword = ""; // New key makes prev stored values (if any) unable to depre.}usr = depreOrprompt (usr, "U-name", "lognUsr");pword = depreOrprompt (pword, "P-word", "lognPwd");function depreOrprompt (targVar, userprompt, setValVarName) { if (targVar) { targVar = unStoreAndDecrypt (targVar); } else { targVar = prompt ( userprompt + ' not set for ' + location.hostname + '. Please enter it now:', '' ); GM_setValue (setValVarName, encryptAndStore (targVar) ); } return targVar;}function encryptAndStore (clearText) { return JSON.stringify (sjcl.encrypt (encKey, clearText) );}function unStoreAndDecrypt (jsonObj) { return sjcl.decrypt (encKey, JSON.parse (jsonObj) );}//-- Add menu commands that will allow U and P to be changed.GM_registerMenuCommand ("Change Username", changeUsername);GM_registerMenuCommand ("Change Password", changePassword);function changeUsername () { promptAndChangeStoredValue (usr, "U-name", "lognUsr");}function changePassword () { promptAndChangeStoredValue (pword, "P-word", "lognPwd");}function promptAndChangeStoredValue (targVar, userprompt, setValVarName) { targVar = prompt ( 'Change ' + userprompt + ' for ' + location.hostname + ':', targVar ); GM_setValue (setValVarName, encryptAndStore (targVar) );}// ADD YOUR CODE TO SET THE USERNAME AND PASSWORD ON THE LOGIN PAGE, HERE.†重要:
- 使用用户脚本登录始终会带来风险。
- 该框架大大降低了这种风险,但是Greasemonkey和Tampermonkey可用的存储机制并不安全,并且浏览器供应商CYA不能存储机密信息。如果一个坏人 同时 获取 了 您的用户脚本 和 浏览器数据,那么他可以对您的密码进行反向工程。当然,如果他有此权限,那么无论如何,他很可能都已将您的其中一台计算机投入使用。
- 该 聪明的做法是使用密码管理器 像 LastPass的 , KeePass的 ,等等。
- 绝对 最糟糕的事情 是将凭据存储在用户脚本本身中。即使是客人也可以看到它们,然后您将被“砍死”。



