User JavaScript
User JavaScript allows the execution of a JavaScript file each time a page is loaded, before the scripts on the page are run.
Why use User JavaScript?
This is a very powerful feature that can be used for several purposes, such as:
- Adding functionality to the browser
- Adding or changing content on Web pages
- Preventing undesired scripts from running
- Fixing broken scripts to ensure that they work properly
How to use it
Opera will automatically load and run all JavaScript files in the folder specified in the opera.ini file. The folder is specified as follows:
[User Prefs] User JavaScript File=/path/to/dir/userjs/ User JavaScript=1
Another important thing to note is that User JavaScript is by default only triggered on pages that contain other scripts. To override this setting, use the following setting in the opera.ini file:
[User Prefs] Always Load User JavaScript=1
Example
As already stated, User JavaScript can be very useful. However, to demonstrate how they work, we have provided a trivial example, to better focus on the methodology rather than the result. In this case, we will create a script that replaces the string Opera with a string specified by the user. Furthermore, this script will only apply to the Opera Software - About Web page.
Below is a helpful walk-through of making such a script.
First, create a JavaScript file, in our case opera-replace.js. So, it seems at first that this will be a very simple task. Apparently, all we need to do is this:
// Version 1 var name = prompt("What is your name?", "Chrille"); document.body.innerHTML=document.body.innerHTML.replace(/Opera/g,name);
Unfortunately, this script does not have any effect. This script is executed before the page is loaded, so the document will be empty and nothing will be replaced. The only content that appears is a prompt for the user to enter a username. A better idea is to add a new listener, which listens to when the document has finished loading, as follows:
// Version 2 opera.addEventListener("AfterEvent.load", function (ev) { var name = prompt("What is your name?", "Chrille"); document.body.innerHTML=document.body.innerHTML.replace(/Opera/g,name); }, false)
While this works, it still does not work not very well. This event is triggered many times when different objects have loaded, which can be frustrating. It is a good idea to be more specific about when to trigger this event, specifically when the document is finished loading, as follows:
// Version 3 opera.addEventListener("AfterEvent.load", function (ev) { if (ev.event.target instanceof Document) { var name = prompt("What is your name?", "Chrille"); document.body.innerHTML=document.body.innerHTML.replace(/Opera/g,name); } }, false)
This works better, but we still have not addressed the issue of only triggering on a certain URL. Right now the script is run on all Web pages, which is quite annoying. This can be easily resolved as follows:
// Version 4 (final) opera.addEventListener("AfterEvent.load", function (ev) { if (location == "http://www.opera.com/company/") { if (ev.event.target instanceof Document) { var name = prompt("What is your name?", "Chrille"); document.body.innerHTML=document.body.innerHTML.replace(/Opera/g,name); } } },false)
Finally it works as expected. This is a very simple script to show the methodology, but as previously mentioned, User JavaScript can be very useful. Please explore the available resources to see the full possibilities.
Resources
For complete documentation as well on how to write and use UserJS, please visit the UserJS.org Web site. There are also plenty of User JavaScripts available for download.