User JavaScript

Last update: 2012-06-02

While User JavaScript is still supported in Opera, extensions are the preferred method for adding custom JavaScript. Support for extensions is more robust and receives frequent updates and enhancements.

What it can do

User JavaScript is loaded and executed as if it were a part of the page that you visit. It is run immediately before the first script on the page. If the page does not contain any scripts of its own, User JavaScript will be executed immediately before the page is about to complete loading. It is usually run before the DOM for the page has been completed. (Note that this does not apply to Greasemonkey scripts.) User JavaScript will not be loaded on pages accessed using the opera: protocol. By default, it is also not loaded on pages accessed using the HTTPS: protocol.

User JavaScript is able to do anything that any normal script is able to do, including things like detecting events, creating variables, creating functions and reading the cookies that are stored for that page. In addition, User JavaScript is able to perform a few extra functions. It is able to control what scripts the page is allowed to load, rewrite the page's own scripts if needed, intercept events that the page is detecting, alter the details of the event, and override variables and functions that the page uses.

To provide the extra functionality, the window.opera object has multiple methods and one object that are described on the specifications page.

Note: These methods only have an effect if called from a User JavaScript, and and any calls to them from regular page scripts will be ignored.

Adding User JavaScripts

To enable User JavaScript:

Select the directory where you will put your User JavaScript files. Opera will load all files in the specified directory whose names end with .js and will use them as User JavaScript files. If a file name ends with .user.js it will be assumed to use Greasemonkey notation.

To write your own User JavaScripts, use a text editor to create the JavaScript file, and save it in your User JavaScript directory. The file will be automatically loaded the next time a page is loaded in Opera. Opera does not need to be restarted to see any changes to User JavaScript files.

To avoid running the entire script on pages where it is not applicable, it is best to check if the page address matches a desired address first, and only run each enhancement or fix if it applies to the current page. Failure to perform this check where necessary could result in errors or impact performance. Two possible ways of checking the page address are:

if (location.hostname.indexOf('') != -1)
  if (location.href.match(/^http:\/\/example\.com\/resources\//))

In order to prevent conflicts with any scripts on the page, it is best to use the DOM addEventListener method when detecting events. Use of the traditional event registration models, such as window.onload = myfunction; is not recommended. Using anonymous functions where possible in event handlers is also preferred.

To restrict an entire User JavaScript file to being executed only for certain pages, the following comment block can be included at the top of the script file:

// ==UserScript==
// @include*
// @include*
// @exclude*
// @exclude
// ==/UserScript==

You can use as many @include and @exclude declarations as necessary to target the desired page addresses.

User scripts are parsed by default as utf-8. To specify another encoding for the user script, use the encoding rule in the script header as follows:

// ==UserScript==
// @encoding iso-8859-1
// ==/UserScript==

Important note:

User JavaScript runs on the global scope, meaning everything that is declared in a script will be shared with the Web page. As such, it is recommended to wrap your script with an anonymous function to protect the script's own data. See the following example.

(function ()
{ /* user script code */

Using User JavaScripts on secure pages

To protect your security, User JavaScript is disabled by default on pages accessed using the HTTPS: protocol.If you want to run User JavaScripts on these pages, you will need to enable the User Javascript on HTTPS setting in opera:config.

If you enable User JavaScript on secure pages, you will need to ensure that the scripts that are in your User JavaScripts folder are safe to use, especially if you use scripts written by other authors. If you enable this option, you will be prompted to allow User Javascript the first time you access a secure page in each browsing session.


Opera Help

Need help? Hit F1 anytime while using Opera to access our online help files, or go here.