GM_getValue undefined error

In my greasemonkey script, I want to check if the GM value is: Username and password, but when I try the following code, it returns me an error:

TypeError: GM_getValue(...) is undefined    
...f (GM_getValue ("username").length == 0 + GM_getValue ("password").length == 0 )

the code:

if (GM_getValue ("username").length == 0 + GM_getValue ("password").length == 0 ){
var username = $('input[name=username]');
var password = $('input[name=password]');

//Username en Password in Firefox zetten met GM_setValue
$(".button").click(function(){

GM_setValue ("username", username.val() );
GM_setValue ("password", password.val() );

});
}
0
source share
2 answers

GM_getValuedoes not return an array and has no property length.
This function returns undefinedif the value has not been set. The correct way to do the check you are trying to do is:

var uName = GM_getValue ("username", "");
var pWord = GM_getValue ("password", "");

if ( ! uName   &&  ! pWord) {
    uName = $('input[name=username]').val();
    pWord = $('input[name=password]').val();
}


However, two additional things to know / consider:

  • ( ) , script GM_getValue . @grant GM_. EG:

    // @grant    GM_getValue
    // @grant    GM_setValue
    
  • , :

    • - .
    • .
    • .

, . , , . .

+7

-, , . -, , , boolean-AND && +. - :

var username = GM_getValue("username");
var password = GM_getValue("password");
if ((username.length == 0) && (password.length == 0)) {
    username = $('input[name=username]').val();
    password = $('input[name=password]').val();
}
$(".button").click(function(){
    GM_setValue ("username", username);
    GM_setValue ("password", password);
});
0

Source: https://habr.com/ru/post/1547664/


All Articles