The Opera object?
Yes the window.opera object. This is a script object that Opera has provided since Opera 5.
It is provided to aid debugging of scripts.
Many real-world scripts use window.opera to check if the browser is Opera. However, very few of them actually seem to
use the properties or methods that are attached to this object.
Opera documents many of these itself in its jsconsole.html, but since these are not on the web, I thought I would put
them there. Some of these docs are shamelessly ripped from that file.
Since the Opera object is not part of any standard,
you should not rely on any of the properties or methods to exist in future releases, with the exception of the properties that
Opera themselves have documented publically: postError, addEventListener, removeEventListener, defineMagicFunction,
defineMagicVariable.
What the object provides
- opera.addEventListener(string: type,function: handler,bool: phase)
- Opera 5&6&7-7.6: No support
- Opera 8+: Registers an event listener for a User JavaScript event. This method only has any effect in User JavaScript files.
- opera.buildNumber()
- Opera 5: No support
- Opera 6: Returns 'Hm, were you only as smart as Bjørn Vermo...'.
- Opera 7+: Returns the current build number of the Opera version being used. (Note that the build number is different for the same Opera version on different platforms.)
- opera.buildNumber('inconspicuous')
- Opera 5: No support
- Opera 6+: Returns the current build number of the Opera version being used. (Note that the build number is different for the same Opera version on different platforms.)
- opera.clearErrorMessages()
- Opera 5&6: No support
- Opera 7&8: Clears all messages from the JavaScript console (JavaScript console shows these changes the next time it is opened). This method only has any effect in pages that are being accessed using the file: protocol.
- Opera 9+: No support
- opera.collect()
- Opera 5&6: No support
- Opera 7+: Attempts to initiate JavaScript garbage collection. This method will only have any effect if Opera has allocated enough memory to JavaScript since the last time it ran a garbage collection.
- opera.defineMagicFunction(string: functionName,function: replacementFunction)
- Opera 5&6&7-7.6: No support
- Opera 8+: Overrides functions defined by the page. This method only has any effect in User JavaScript files.
- opera.defineMagicVariable(string: variableName,function: getter,function: setter)
- Opera 5&6&7-7.6: No support
- Opera 8+: Overrides variables defined by the page. This method only has any effect in User JavaScript files.
- opera.errorIndex()
- Opera 5&6: No support
- Opera 7&8: Returns the index (beginning at 0) of the last message (or -1 for none) in the JavaScript console. Limited to about 253 errors per Opera session. This method only has any effect in pages that are being accessed using the file: protocol.
- Opera 9+: No support
- opera.errorMessage(int: index)
- Opera 5&6: No support
- Opera 7&8: Returns the error message at the specified index. Returns false if no message exists at that index, or if it has been cleared. This method only has any effect in pages that are being accessed using the file: protocol.
- Opera 9+: No support
- opera.getOverrideHistoryNavigationMode()
- Opera 5&6&7&8: No support
- Opera 9+: Returns the last last value of history navigation mode that was set using for the current document setOverrideHistoryNavigationMode. This method only has any effect in User JavaScript files.
- opera.getPreference(string: section,string: preference)
- Opera 5&6&7&8: No support
- Opera 9+: Returns the current value of the specified preference in opera6.ini (or operadef6.ini if the preference is not specified in opera6.ini). This method only has any effect on the automatically generated opera:config page.
- opera.getPreferenceDefault(string: section,string: preference)
- Opera 5&6&7&8: No support
- Opera 9+: Returns the current value of the specified preference in operadef6.ini. This method only has any effect on the automatically generated opera:config page.
- opera.isRegistered()
- Opera 5&6&7-7.6: No support
- Opera 8: Returns true or false if Opera is or is not registered.
- Opera 8.5+: No longer supported (free - no need to register).
- opera.openInSourceViewer(string: URL)
- Opera 5&6: No support
- Opera 7&8: Opens the specified URL in the user's chosen source viewer. This method only has any effect in pages that are being accessed using the file: protocol.
- Opera 9+: No support
- opera.popXSLTransform()
- Opera 5&6&7&8: No support
- Opera 9+: Removes the last XSL transform applied with opera.pushXSLTransform.
- opera.postError(string: message1[,string: message2[,string: message3[,etc.]]])
- Opera 5&6: No support
- Opera 7+: Prints each argument as a separate message in the JavaScript console.
- opera.pushXSLTransform(node: XSLStylesheet)
- Opera 5&6&7&8: No support
- Opera 9+: Applies the XSLStylesheet to the current document. Can be applied multiple times for a cumulative effect.
- opera.removeEventListener(string: type,function: handler,bool: phase)
- Opera 5&6&7-7.6: No support
- Opera 8+: Removes an event listener for a User JavaScript event. This method only has any effect in User JavaScript files.
- opera.setOverrideHistoryNavigationMode(string: mode)
- Opera 5&6&7&8: No support
- Opera 9+: Sets the the history navigation mode to 'automatic', 'compatible' or 'fast'. This method only has any effect in User JavaScript files.
- opera.setPreference(string: section,string: preference,string: value)
- Opera 5&6&7&8: No support
- Opera 9+: Sets the the specified preference in opera6.ini to the specified value. This method only has any effect on the automatically generated opera:config page.
- opera.SRPNumber()
- Opera 5: No support
- Opera 6&7&8-8.5: Returns the SRP number of branded versions (as a string). Returns a blank string in unregistered versions, and '0' in normal registered versions.
- Opera 8.5+: No longer supported (free - no need to register).
- opera.version()
- Opera 5&6&7-7.55: No support
- Opera 7.6+: Returns the current version number (as a string) of the Opera version being used.
Using the opera object
Because the Opera object is an extension to JavaScript, no other browser supports it. Therefore, it is important to test
for its existence before you try to use it:
if( window.opera ) {
alert( opera.buildNumber('inconspicuous') );
}
However, just to be awkward, older versions of Opera do not support all of the methods of the Opera object so you need
to test for these as well. Opera 5 supports none of the methods (since that leaves very little reason why they would bother
to include the Opera object - with the exception of aiding browser sniffing, I wonder if I missed something ...). Opera 6
only supports buildNumber, and even then, only if you pass it a special string.
if( window.opera ) {
if( opera.buildNumber ) {
alert( opera.buildNumber('inconspicuous') );
}
}
Don't forget that some of the methods can only be run from file: URLs, and some can only be run from user JavaScript files.
See Opera's developer article for information on how to use the
postError method to debug scripts.
See Opera's User JavaScript tutorial (written by yours truly) for
information on how to use the very powerful User JavaScript features.
Disabling it
Yes, there are some sites written by ignorant authors who think that opera should be blocked from entering their sites,
almost always because they use DOM based scripts that Opera 6 and below could not support (but Opera 7+ can), so they block Opera by detecting
the window.opera object (if all they do is use a userAgent detect, you might find it helps to change the 'Identify as ...' option in Tools - quick preferences,
or for server-side User-Agent detects, you can also use the '4' or '5' values in ua.ini). Since Opera 8 you can use a simple User JavaScript to prevent scripts on the page from accessing it:
if( location.hostname.indexOf('badsite.example.com') + 1 ){
window.opera.addEventListener('BeforeScript',function (e) {
e.element.text = e.element.text.replace(/opera/ig,'borkme');
},false);
window.opera.addEventListener('BeforeJavaScriptURL',function (e) {
e.source = e.source.replace(/opera/ig,'borkme');
},false);
window.opera.addEventListener('BeforeEventListener',function (e) {
var asStr = e.listener.toString();
if( asStr.match(/^function\s*\(\s*\)/) ) {
e.listener.toString().replace(/opera/ig,'borkme');
e.listener = new Function(asStr.replace(/^function\s*\(\s*\)\s*{|}\s*$/g,''));
}
},false);
}
Or you can use User JavaScript to completely disable it.
Older versions of Opera do not actually offer a way to hide this object, so they have no defense against these
scripts. I recommend upgrading if possible. However, not to be defeated, there are two possibilities if you cannot upgrade.
- If the scripts run a little while after the page has loaded, you might be able to use a bookmarklet:
Kill w.opera
- If the script runs immediately, the only option is to use a local web-filtering proxy like
Proxomitron. As well as being able to filter scripts that check for window.opera,
it can also filter pages and scripts that check for 'Opera' in the userAgent information.
Andrew Gregory has done an excellent job of describing
how to do this.