在我的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)
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)
该错误消息(如果尚未编辑)表明该脚本未GM_getValue正确激活.您必须设置适当的@grant值才能使用GM_函数.例如:
// @grant GM_getValue
// @grant GM_setValue
Run Code Online (Sandbox Code Playgroud)你开始的方法:
因此,不要在没有充分理由的情况下重新发明轮子.已经有经过验证的,更安全,功能齐全的框架可用于此类事情. 这是一个很好的.
| 归档时间: |
|
| 查看次数: |
4066 次 |
| 最近记录: |