Bookmarklet在粘贴到控制台时可正常工作,但在单击书签时则无法正常工作

时间:2015-09-02 15:09:59

标签: javascript bookmarklet

如果将代码粘贴到控制台并直接从控制台运行,我有以下脚本可以正常工作。但是,如果我采用完全相同的代码并将其缩小为一行,则通过书签运行该行返回Uncaught SyntaxError: Unexpected identifier。我也尝试使用bookmarkleter缩小,但返回相同的错误。如何修复此错误并使脚本作为书签正确运行?

原始代码:

javascript:(function() {
    var count = 0;
    var thisUser = Waze.loginManager.user;
    if (thisUser === null) return;
    var usrRank = thisUser.normalizedLevel;
    var UpdateObject;
    if (typeof(require) !== "undefined") {
        UpdateObject = require("Waze/Action/UpdateObject");
    } else {
        UpdateObject = Waze.Action.UpdateObject;
    }

    function onScreen(obj) {
        if (obj.geometry) {
            return (W.map.getExtent().intersectsBounds(obj.geometry.getBounds()));
        }
        return (false);
    }

    Object.forEach(W.model.segments.objects, function(k, v) {
        if (onScreen(v) && v.isGeometryEditable() && v.attributes.roadType === 3 && v.attributes.lockRank <= usrRank) {
            count++;
            W.model.actionManager.add(new UpdateObject(v, {
                lockRank: 0
            }))
        }
    })

    alert(count + " segments were updated.");
})();

手动缩小:

javascript:(function(){var count = 0;var thisUser = Waze.loginManager.user;if(thisUser === null)return;var usrRank = thisUser.normalizedLevel;var UpdateObject;if(typeof(require)!=="undefined"){UpdateObject=require("Waze/Action/UpdateObject");}else{UpdateObject=Waze.Action.UpdateObject;}function onScreen(obj){if(obj.geometry){return(W.map.getExtent().intersectsBounds(obj.geometry.getBounds()));}return(false);}Object.forEach(W.model.segments.objects,function(k, v){if(onScreen(v)&&v.isGeometryEditable()&&v.attributes.roadType===3&&v.attributes.lockRank<==usrRank){count++;W.model.actionManager.add(new UpdateObject(v,{lockRank: 0}))}})alert(count+" segments were updated.");})();

缩小bookmarkleter

javascript:(function(){var%20count=0;var%20thisUser=Waze.loginManager.user;if(thisUser===null)return;var%20usrRank=thisUser.normalizedLevel;var%20UpdateObject;if(typeof(require)!==%22undefined%22){UpdateObject=require(%22Waze/Action/UpdateObject%22);}else{UpdateObject=Waze.Action.UpdateObject;}function%20onScreen(obj){if(obj.geometry){return(W.map.getExtent().intersectsBounds(obj.geometry.getBounds()));}return(false);}Object.forEach(W.model.segments.objects,function(k,v){if(onScreen(v)%26%26%20v.isGeometryEditable()%26%26%20v.attributes.roadType===3%20%26%26%20v.attributes.lockRank%20%3C=usrRank){count++;W.model.actionManager.add(new%20UpdateObject(v,{lockRank:0}))}})alert(count+%22%20segments%20were%20updated.%22);})();

1 个答案:

答案 0 :(得分:1)

缺少分号

})alert(cou
^^^ 

始终使用分号!你的格式很差的版本缺少一些。