Oto schemat uzyskiwania i przechowywania danych logowania. Skrypt wyświetla informacje o pierwszym uruchomieniu i zapisuje je, zaszyfrowane, przy użyciu GM_setValue()
.
Dodaje również dwie pozycje do menu kontekstowego Greasemonkey, aby umożliwić zmianę nazwy użytkownika lub hasła.
// ==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 decode.
}
usr = decodeOrPrompt (usr, "U-name", "lognUsr");
pword = decodeOrPrompt (pword, "P-word", "lognPwd");
function decodeOrPrompt (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.
Jeśli poświadczenia nie są dostępne w lokalnym magazynie dla skryptu, poproś o nie. Jeśli chodzi o "lokalny magazyn dla skryptu", uważam, że jest jeden oferowany przez Greasemonkey, zobacz na przykład ten hit google na "lokalny magazyn greasemonkey": http://stackoverflow.com/questions/13889995/local-storage-across- domain-using-a-greasemonkey-script –
Odnośnie tampermonkey - nic nie znalazłem ani nie przetestowałem, czy obsługuje 'GM_setvalue' –
Myślę, że tampermokey obsługuje' GM_setvalue', użyłem go wcześniej bez problemów. – TheBronx