Last update: March 16, 2011
window.opera.defineMagicVariablewindow.opera.defineMagicFunctionwindow.opera.addEventListenerwindow.opera.removeEventListenerwindow.opera.scriptStoragewindow.opera.setOverrideHistoryNavigationModewindow.opera.getOverrideHistoryNavigationModewindow.opera.defineMagicVariable
This method can be used by User JavaScripts to override global variables defined by regular scripts. It accepts
three parameters:
NameGetterSettersetter function
is required, the value null must be used instead.For example:
window.opera.defineMagicVariable('isGoodBrowser', function (curVal)
{
return true;
}, function (newVal)
{
if (!newVal)
{
window.status = 'Repairing script';
}
});
window.opera.defineMagicFunction
This method can be used by User JavaScripts to override global functions defined by regular scripts. It accepts
two parameters:
NameImplementationthis keyword in the real function.magic
function).For example:
window.opera.defineMagicFunction('getLayer', function (oRealFunc, oThis, oParam1, oParam2)
{
return oParam1.getElementById(oParam2).style;
});
This example overrides a function f. If the this object refers to the window object,
it returns false. Otherwise it just runs the real function instead:
window.opera.defineMagicFunction('f', function (real, thisObject)
{
if (thisObject == window)
{
return false;
}
else
{
return real.apply(thisObject, Array.prototype.slice.call(arguments, 2));
}
});
window.opera.addEventListener
This is the generic method used to add listeners for User JavaScript events. When an event is detected, the event handler
is passed a UserJSEvent object (which is also available through window.event). In
addition to the usual event properties, the UserJSEvent object may also populate the element, event,
listener, eventCancelled, propagationStopped, source and returnValue
properties, depending on what type of event was being detected. Some events can be cancelled using preventDefault,
and this may be used to prevent the script or event handler from being executed.
Listeners for the User JavaScript events can be added almost anywhere within a User JavaScript file. Listeners can not be added by the handler functions for regular events, or by scripts activated by timers. The following events can be detected:
BeforeExternalScriptscript element with a src attribute is encountered.script element is available as the element attribute of the UserJSEvent.script element is not executed.BeforeScript and AfterScript events will not fire.BeforeScriptscript element is executed.script element is available as the element attribute of the UserJSEvent.text property of the script element, and is also
writable. Do note that this behavior is different from the one specified in
DOM 3 Core by design, but only during the BeforeScript and AfterScript events. Usage example
follows:
opera.addEventListener('BeforeScript', function(userJSEvent){
userJSEvent.element.text = userJSEvent.element.text
.replace(/function\s+window\.onload\(/g,'window.onload = function(');
}, false);
BeforeScript event is fired for inline scripts as well as external scripts, including scripts with a
type that Opera normally does not execute (such as VBScript). If the type is changed to something recognizable (only javascript
currently), and the contents changed to match the new script type, then the script will execute.script element is not executed.AfterScriptscript element has finished executing.script element is available as the element attribute of the UserJSEvent.text property of the element attribute of the UserJSEvent has no effect, unlike
the BeforeScript event.BeforeEventevent attribute of the UserJSEvent.BeforeEventListener
events are not fired.BeforeEvent.typeBeforeEvent, but fired only for events of the specified type (for example, BeforeEvent.click).BeforeEvent.type event, no BeforeEvent
event is fired.AfterEventevent attribute of the UserJSEvent.UserJSEvent object will also have the eventCancelled property, that will be set to
true if any regular event handlers have cancelled the event. This feature is duplicated by defaultPrevented from
DOM 3 Events, but kept for legacy reasons.AfterEvent.typeAfterEvent, but fired only for events of the specified type (for example, AfterEvent.click).AfterEvent.type event, no AfterEvent
event is fired.BeforeEventListenerevent attribute of the UserJSEvent, and the listener to
be invoked is available as the listener attribute of the UserJSEvent.BeforeEventListener.typeBeforeEventListener, but fired only for events of the specified type (for example, BeforeEventListener.click).BeforeEventListener.type event, no BeforeEventListener
event is fired.AfterEventListenerlistener for regular event is called.event attribute of the UserJSEvent, and the listener to
be invoked is available as the listener attribute of the UserJSEvent.UserJSEvent object will also have the propagationStopped property, that will be set to
true if any regular event handlers have cancelled the event propagation.AfterEventListener.typeAfterEventListener, but fired only for events of the specified type (for example, AfterEventListener.click).AfterEventListener.type event, no AfterEventListener
event is fired.BeforeJavascriptURLjavascript: URL is executed.javascript: in the URL) is available as the source
attribute of the UserJSEvent, and is also writable.javascript: URL is not executed.AfterJavascriptURLjavascript: URL is executed.javascript: in the URL) is available as the
source attribute of the UserJSEvent, and any value returned by that code is available as the returnValue
attribute of the UserJSEvent.returnValue is also writable.PluginInitializedelement attribute of the UserJSEvent.BeforeCSSelement attribute of the UserJSEvent refers either to the style or the
link elements that include the styles in the document. For style sheets imported using a CSS import rule, a placeholder
link element is returned which provides access to the style sheet url, media, and other properties.cssText property of the UserJSEvent object,
and is also writable. Usage example follows:
opera.addEventListener('BeforeCSS', function(userJSEvent){
userJSEvent.cssText = userJSEvent.cssText
.replace(/-(moz|ms|webkit|o)-(border|text-overflow)/g,'$2')
.replace(/-(moz|ms|webkit)-(gradient|transform|transition)/g,'-o-$2');
}, false);
textContent
on the style element, will only be applied after the BeforeCSS event, if it is not cancelled.AfterCSSelement attribute of the UserJSEvent refers either to the style or the
link elements that include the styles in the document. For style sheets imported using a CSS import rule, a placeholder
link element is returned which provides access to the style sheet url, media, and other properties.cssText property of the UserJSEvent object.cssText property of the event object triggers a new BeforeCSS event.window.opera.removeEventListener
This can be used to remove User JavaScript event listeners that were added using window.opera.addEventListener.
This cannot be used if the event listener was added as an anonymous function. For further details, see the
W3C DOM Level 2 Events - Event registration interfaces specification.
window.opera.scriptStorage
This property returns a Storage object as specified in the
W3C Web Storage API, unique per script. User scripts can use this object to share persistent data across domains.
Script storage objects currently do not fire storage events. This property is available in the Opera Presto 2.6 rendering
engine; see Web specifications support in Opera Presto 2.6: Web
Storage.
The scriptStorage object is available only during the main User JavaScript thread, being therefore accessible
only in the main body of the user script. To access the storage object later, keep a reference to the object.
(function(opera, storage)
{
if (storage)
storage.key = 'value';
/* Rest of script */
}
)(opera, opera.scriptStorage);
The scriptStorage object has a limited quota, defined in
opera:config. If the quota is 0, the object is not accessible. To see all storage objects created by user scripts,
refer to opera:webstorage.
window.opera.setOverrideHistoryNavigationModeThis method can be used by User JavaScripts to set what history navigation mode Opera should use for the current document. It accepts one parameter:
Modeautomatic, compatible, or fast.See the knowledge base article for more details.
window.opera.getOverrideHistoryNavigationMode
Retrieves the last value of history navigation mode that was set for the current document using setOverrideHistoryNavigationMode.
Defaults to automatic.
UserJSEvent objectThis is the event object passed to User JavaScript event handler functions (it is also available through window.event). As well as the usual event object properties, it has a few that are specific to User JavaScript events. While many of the properties serve little purpose in User JavaScript (for example, the currentTarget, srcElement, and target properties all reference window.opera), the following properties and methods are the most useful:
elementscript, object, embed, style, or link
elements.getAttribute and setAttribute.BeforeExternalScript, BeforeScript, AfterScript, PluginInitialized,
BeforeCSS and AfterCSS events.element.textBeforeScript and AfterScript events.eventBeforeEvent, AfterEvent, BeforeEventListener and AfterEventListener
events.eventCancelledAfterEvent events.listenerBeforeEventListener and AfterEventListener events.preventDefaultAfterScript and AfterCSS events.propagationStoppedAfterEventListener events.returnValueAfterJavascriptURL events.sourceBeforeJavascriptURL and AfterJavascriptURL events.typeBeforeJavascriptURL.cssTextBeforeCSS and AfterCSS events.Need help? Hit F1 anytime while using Opera to access our online help files, or go here.