GM_getValue未定义错误 [英] GM_getValue undefined error

查看:123
本文介绍了GM_getValue未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my greasemonkey script I want to check if the GM Value : Username and Password isset but when i try the following code it gives me back the error :

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

代码:

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

});
}

推荐答案

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

GM_getValue does not return an array and does not have a length property.
That function returns undefined if the value was not set. The proper way to do the check you are attempting is:

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

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


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

  1. That error message (if it hasn't been edited) suggests that the script did not activate GM_getValue properly. You must set appropriate @grant values to use GM_ functions. EG:

// @grant    GM_getValue
// @grant    GM_setValue

  • 您正在使用的方法:

  • The approach you are starting:

    • 有错误-因此需要这个问题.
    • 存在您会发现的可用性问题.
    • 没有便利或安全功能.
    • Has errors -- hence the need for this question.
    • Has usability problems which you will discover.
    • Doesn't have convenience or security features.

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

    So, don't reinvent the wheel without a darn good reason. There are already proven, more-secure, full-featured frameworks for this kind of thing. Here's a good one.

    这篇关于GM_getValue未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆