I saw the following JavaScript code and it confused me since logically 0 will never be 4 or 5:
if (0 === 4) { // Safari
safari.self.addEventListener("message", safariMessageListener, false);
} else if (0 === 5) { // Opera
opera.extension.onmessage = operaMessageListener;
}
I will assume that this code works. So has the programmer managed to redefine 0 in some manner or is 0 somehow not referring to the number 0? Can anyone explain this code and why would anyone code like this? What are the advantages of this technique? (except for confusing people like me!)
For completeness, this snippet came from the YouTube Center Grease Monkey plug-in. The URL of which is:
https://github.com/YePpHa/YouTubeCenter/wiki/Developer-Version
And the full method is:
function initListeners() {
if (support.CustomEvent) {
window.addEventListener("ytc-content-call", eventListener, false);
} else {
window.addEventListener("message", messageListener, false);
}
window.addEventListener("unload", windowUnload, false);
if (0 === 4) { // Safari
safari.self.addEventListener("message", safariMessageListener, false);
} else if (0 === 5) { // Opera
opera.extension.onmessage = operaMessageListener;
}
}
The source code looks quite different:
if (@identifier@ === 4) { // Safari
safari.self.addEventListener("message", safariMessageListener, false);
} else if (@identifier@ === 5) { // Opera
opera.extension.onmessage = operaMessageListener;
}
There is just an Ant build script which replaces this @identifier@
with ${indentifier.userscript}
:
<target name="copy-userscript-meta">
<copy todir="${buildDir}">
<fileset dir="${src.meta}/" />
</copy>
<antcall target="tokenreplace" />
<replace dir="${buildDir}" value="${indentifier.userscript}" token="@identifier@" encoding="${encoding}" />
</target>
${indentifier.userscript}
is defined in the same file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With