GM_getValue未定义错误

use*_*839 0 greasemonkey

在我的greasemonkey脚本中,我想检查GM值:用户名和密码是否设置,但是当我尝试以下代码时它会给我回错:

TypeError: GM_getValue(...) is undefined    
...f (GM_getValue ("username").length == 0 + GM_getValue ("password").length == 0 )
Run Code Online (Sandbox Code Playgroud)

码:

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() );

});
}
Run Code Online (Sandbox Code Playgroud)

Bro*_*ams 7

GM_getValue不返回数组,也没有length属性.如果未设置该值,则
返回该函数undefined.正在尝试检查的正确方法是:

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

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


但是,还有两件事要知道/考虑:

  1. 该错误消息(如果尚未编辑)表明该脚本未GM_getValue正确激活.您必须设置适当的@grant值才能使用GM_函数.例如:

    // @grant    GM_getValue
    // @grant    GM_setValue
    
    Run Code Online (Sandbox Code Playgroud)
  2. 你开始的方法:

    • 有错误 - 因此需要这个问题.
    • 您会发现可用性问题.
    • 没有便利或安全功能.

因此,不要在没有充分理由的情况下重新发明轮子.已经有经过验证的,更安全,功能齐全的框架可用于此类事情. 这是一个很好的.