Bookmarklet 在粘贴到控制台时可以正常工作,但在单击书签时不能正常工作

Bookmarklet works correctly when pasted into console but not when the bookmark is clicked

我有下面的脚本,如果代码直接从控制台粘贴到运行,它会按预期工作。但是,如果我自己使用完全相同的代码并将其缩小到一行,运行通过书签 returns 将那一行 Uncaught SyntaxError: Unexpected identifier。我也尝试过使用 bookmarkleter 进行缩小,但是 returns 出现了同样的错误。如何修复此错误并将脚本作为小书签正确设置为 运行?

原代码:

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

缺少分号

})alert(cou
^^^ 

总是使用分号!您的格式化版本缺少一些内容。