I have the below script which works as expected if the code is pasted into and run directly from the console. However, if I take the exact same code and minify it onto one line myself, running that one line via a bookmark returns Uncaught SyntaxError: Unexpected identifier
. I've also tried minifying using bookmarkleter, but that returns the same error. How do I fix this error and get the script to run correctly as a bookmarklet?
Original code:
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.");
})();
Manually minified:
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.");})();
Minified with 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);})();
Missing semicolon
})alert(cou
^^^
Always use semicolons! Your pretty formatted version is missing a few.