User JavaScript in Opera

Introduction

Yes, it's a cool feature. You can get Opera to include your own scripts on the pages that you visit. Those scripts are allowed to do whatever a normal script is allowed to do, and in addition, you can rewrite scripts on the page, intercept events, and control what the page is allowed to see you do.

If a page was badly written and causes problems, and the author refuses to fix it, you can fix it yourself. Or you can extend a page with useful tools of your choice.

See the official Opera User JavaScript tutorial (written by yours truly) for an introduction to how you can use it. See also the Greasemonkey Script Repository, since Opera is capable of running many of those too. But I recommend you read the tutorial first, as it contains useful info about using Greasemonkey scripts.

The scripts can often be shared with other Opera users, and that is what this page is here for. These are scripts I thought you might find useful.

My User JavaScripts

See below for license and terms of use.

Debugging and developing

Script Description
XML tree Opera (Key)

Deprecated - not needed since Opera 10 - Opera now has its own native handling similar to this script

If an XML file contains no style information, Opera will render it as an unstyled Web page. In many cases, this serves no purpose. Some other browsers display the XML tree for the page. This script replicates that behaviour in Opera, making it a useful tool for analysing XML files. Files are syntax highlighted, and their contents can be expanded and collapsed. There is also the option to go back to the normal rendered XML view. By default, the script just works, but for those of you who feel the need to tweak everything (you know who you are ;), the script allows you to:

  • Exclude file types (based on the root XML node).
  • Protect files that import elements from the (X)HTML namespace.
  • Configure the syntax highlighting for XML processing instructions (Opera 9+), doctype tags, elements, attributes, attribute values, comments, text nodes and CDATA blocks.
  • Configure text strings so you can translate it into your own language.

See the sample XML file and the sample XML tree output. Known limitations in Opera 8.x (these are fixed in Opera 9):

  • Displaying large XML files is very slow.
  • XML tree view cannot display XML processing instructions.
  • XML files will be converted into XML tree view even if they already have CSS or XSLT stylesheets (Opera 8 did not understand XSLT anyway, so this does not really matter). You can use the link to go back to the rendered view. Note that this does not affect XHTML documents; they will always be rendered correctly.
  • Doctype declarations are sometimes displayed with one letter missing from the doctype name (as in the example above).

Note, this script uses inline frames so if you disable inline frames, you will need to enable them to use this script.

Map areas Opera (Key)

The Opera version of my IE context menu image map area highlight script (but with fixes for z-index and dynamic map issues, and including support for the parts of image maps that Opera supports and IE doesn't, such as default shapes, and maps created from regular links as well as AREA elements).

An aid for authors who are editing an image map. Especially when re-editing an image map, it becomes very difficult to remember exactly where the areas were, and what the coordinates relate to, in particular when the relationship between areas becomes quite complicated. This script uses SVG to overlay each area with a translucent shape corresponding to the coordinates. Each coordinate is shown as a dot, and the lines joining the dots are also shown, using random colours to help distinguish between shapes. Moving the mouse over the image map shows the coordinates in the status bar. Shapes can be clicked to effectively click the underlying area (including firing onclick event handlers). Due to limitations of SVG, only pixel-based image maps are supported (no major browser understands percentage coordinates in image maps anyway). Once you have added this User JavaScript, you can then use this bookmarklet to display the image map areas:

Map areas (hold shift, and drag it to a toolbar, or bookmark it and use the bookmark).

If the maps that are displayed on the page can change, run the bookmarklet again to redraw the updated image map shapes.

Script tracer Opera (Key)

Deprecated - not needed since Opera 9.5 - use Developer Tools to debug and trace script flow

Designed to aid debugging of big scripts, such as Web mail services, DHTML menus, and other scripts whose code is too big or complicated to read easily. This script allows you to trace what parts of the script are running, and to some extent, what they are doing. It can show what functions have run, what innerHTML has been written, what code has been evaluated, what events have fired, and a number of other things. It is not designed to replace a proper script debugger, as it cannot give a record of every line of code, and it cannot set breakpoints, but it does help to give an overview of the flow of the script. It will output debugging information into the error console (Tools - Advanced - Error console). Set options in the script file, and read the warnings, they are there for a reason. Test the script on the test page.

Monitor browserjs Opera (Key)

Since Opera 8.01 it has been possible to have an automatically updated script (browserjs) fix sites for you. Every time it makes a fix, it posts an error in the JavaScript console. This can sometimes leave the console a little cluttered, and you might not want browserjs to post messages into it. This script provides the option to prevent browserjs posting messages into the console. Additionally, it allows you to check how many times it changed scripts on the page like this:

var numChanges = opera.hasModified();

If desired, be notified whenever it makes a change:

opera.sayWhenChangingBJS = function () { ... };
Safe alert Opera (Key)

Partly deprecated - not needed for most users since Opera 9 (abusive dialogs can be blocked, but not with so much control as this script)

Every time a page attempts to use an alert, it is replaced by a confirmation dialog that allows you to choose if any more alerts should be displayed. If a page attempts to open more than 5 confirms or prompts, this prompts to ask if you want to allow more to open. If you allow it, this will prompt again after another 5 dialogs. Useful for detecting infinite alert loops (usually self inflicted by mistake during testing). Once you have added this User JavaScript, you can also use this bookmarklet to enable or disable alerts: Toggle alert (note that this will have no effect if the page is already locked in an infinite loop).

Prevent Web page annoyances

Script Description
Fix content type Opera Greasemonkey+Firefox (with edit) (Key)

If HTML pages are incorrectly served as text, this script attempts to correct them, and load them as HTML. It detects these pages by seeing if they have either a HTML or BODY tag in the first 256 bytes (this approximates the algorithm used by Internet Explorer), with a couple of extra conditions for text files, etc. This is not perfect, and it may generate a few false positives and negatives. For false positives, you can hit the back button to go back to the plain text. For false negatives, you can also use my 'Force HTML' bookmarklet from my bookmarklets page.

Note: This script will add an extra history entry for every page that it fixes.

Check window.close Opera (Key)

With most browsers, if a page tries to close the window using window.close(), the browser checks if they should be allowed to close the window (if they opened it), and prompts for permission if not. In Opera, there is no such restriction. This script makes Opera mimic the behaviour of other browsers, to prevent malicious sites from closing windows that they did not open, or do not have permission to close.

Stop form focus Opera (Key)

Attempts to stop scripts from focusing form elements as the page loads (and optionally after loading as well). This is mainly useful for search engines that attempt to force the search input to be focused, preventing keyboard shortcuts from working as expected. You can always focus the form field yourself, when you are good and ready.

No title scrolling Opera (Key)

Prevents scripts from being able to modify the document title (seen on the taskbar button, page bar tab, and window title bar). Disables title scrolling animations.

Hide parsing errors Opera (Key)

When a page served as XHTML fails XML validation, Opera displays an error message, and allows you to click a link to tell it to re-parse the page as normal HTML. This script automates clicking the link so as soon as a parsing error is displayed, the page is immediately reparsed as HTML.

Block unwanted content

Script Description
Block Last Measure Opera (Key)

Last Measure is a shock site (and several mirrors) that uses a combination of Flash, forms and ActiveX to spawn multiple popups in virtually all browsers, while displaying graphic images, and announcing audible messages. Generally this leaves the browser totally unresponsive, and may crash the operating system with certain browsers. In Opera, it is possible to defeat it with swift use of Ctrl+W, but it is easier to just install this User JavaScript, which attempts to automatically detect and disable the Last Measure exploit. See Wikipedia's old article for more details about Last Measure.

Text censor Opera Greasemonkey+Firefox (Key)

Censors text on Web pages. Can be used to remove offensive words, or simply words that you do not wish to see. Note that it only operates on the text of the page. It cannot censor words written across two elements, words written using images, words written as image alt text, words written with CSS, or words written using /\sc|| art or replaced l3tt3r5 (unless you tell it what specific variations to block). However, it does cope with the majority of situations encountered on real pages. The list of words to block is fully configurable, and should be customised to suit your needs.

Block external Opera (Key)

Removes all scripts, images, iframes, objects, embeds, and applets, that come from a different domain to the page itself. This will misfire and will end up blocking content that you did not want it to block (for example, a site that serves all of its images from a subdomain). I take no responsibility for how you choose to use this script. You have been warned.

Accessibility

Script Description
Frameset links Opera Greasemonkey+Firefox (with edit) (Key)

If frames are disabled, many pages become inaccessible by displaying a useless message saying to enable frames again. In many cases, the frameset pages would work perfectly well on their own, but the page refuses to let you see them unless you enable frames. This script extracts the addresses of all pages in the frameset, and displays a list of links to the individual frames. Additionally, the script enhances image maps by displaying a list of links when images are disabled, and adds links to longdesc and cite URLs.

Clean URI Opera Greasemonkey+Firefox (with edit) (Key)

Attempts to recognise links that use redirected URLs (http://example.com/?url=http://www.howtocreate.co.uk) and replace them with the direct URL. Also optionally attempts to work with links that use window.open in either the href or onclick event handler - even if 'Disable script' is used to disable regular page scripts. Optionally (configurable in the source), it can create a 'CleanURL' link instead of replacing the original link. The replaced link's tooltip will show what has been changed. The link will be given an additional rel value 'redirected', and the custom 'replacedurl' attribute which contains the original URL (for use with content:attr(replacedurl);). For replaced links, these can be selected for styling using a[rel~="redirected"] and CleanURL links can be selected using a[rel~="redirected"] + a. Test the script on the test page.

Disable script Opera (Key)

You might be one of those people that likes to disable JavaScript, but you might actually have a use for User JavaScripts, which means that you have to enable scripts. This script allows you to write your scripts, but does not allow the page to run its own. It also extracts contents of <noscript> tags, and puts them inside wasnoscript tags instead, which you can then style with User CSS. Any event handlers you create in your User JavaScript must have the word 'mwjenabled' in their names to allow them to run (the site signature script already uses this):

document.addEventListener(
  'onload',
  function mwjenabledLoadListener() {
    yourscript();
  },
  false
);

Other page enhancements

Script Description
Link alert Opera (Key)

This script identifies links to specific file types, and displays an appropriate icon when the link is hovered. It can also display appropriate icons for specific types of links, such as links that open new windows, or run scripts. The script has a few options that allow you to choose:

  • If the icons should appear only when the link is hovered, or if they should always be visible.
  • If the icons should appear if the link already contains an image.
  • If the icons should appear if the link does not contain any text.
  • If the icons should disappear after a timeout.

Additionally, you can configure what file and link types you want to see icons for, what file extensions or link protocols to match, and what icons to use. Note that the script works by checking the file extensions of the links. It does not try to retrieve the file to check if it is actually what it claims to be, and it does not check to see if server-side scripts are serving known file types indirectly. However, it does work for the majority of links.

The following link types can be detected (note that these links do not point to real files, they are just there so you can test the script):

* Some links may show two 'JavaScript link' icons - this is intentional, as it shows that the link has script in both the href and onclick attributes. This is an indicator that, even if you can understand the script you see in the tooltip or status bar, it still might do something else when you click it.

This script was inspired by the TargetAlert Firefox extension, but was written independently to work with Opera's User JavaScript. Icons are the property of their respective copyright holders.

Show what's-related Opera Greasemonkey+Firefox (with edit) (Key)

Provides a button in the top-left corner that toggles a "What's related" panel. The panel shows the related links as provided by a service of your choice.

Site signature Opera Greasemonkey+Firefox (with edit) (Key)

Deprecated - not needed since Opera 9 - use Site Preferences to add custom User CSS

Appends a class to the HTML element using the domain of the current page. For example, pages on this site receive class="howtocreate co uk" - this allows you to use multiple classes or class selectors that match substrings (so matching all Google sites is done like this: html.google), even though Opera 8 does not support attribute substring selectors.

Plugins

Script Description
Download embeds Opera Greasemonkey+Firefox (with edit) (Key)

Puts a link beside every object and embed on the page (aka plugins ;) ) to allow you to download the associated file. The link is inserted inline immediately before the object/embed, with class 'pdownlink' so you can style it. Load a page containing a plugin, and double click to create the link(s). The file that it links to may be only a pointer to another file - if it is, you will need to decypher the file yourself ...

Fake Flash Opera (Key)

If Flash or plugins are disabled, some scrips will redirect you to a page telling you to enable Flash. Often, this is not necessary. This script either pretends that flash is enabled even when it is not, or it attempts to catch redirection attempts and asks you if you want to allow the redirect.

No click-to-activate Opera (Key)

Deprecated. This script does not work reliably enough to be used. It often prevents plugin content from loading correctly. Until such time as it works reliably, it is deprecated. Do not use this script.

Since version 9, Opera (like Internet Explorer and possibly others in future) requires some plugins to be clicked before they can be used, which can be annoying if you visit sites that use a lot of plugin content. Sites can avoid this, but not all of them do (see my article about avoiding click-to-activate for more details). This script applies the workaround to avoid making you click. It works for the vast majority of Flash sites. One Linux user has experienced problems where this script caused Java applets not to display. There is a setting in the script to tell it not to try to fix Java applets.

Hide objects until double click Opera Greasemonkey+Firefox (rename to .user.js) (Key)

Deprecated - not needed since Opera 9 - use the content blocker instead, it is more reliable (I will not release any more updates for this script)

Hides all objects, embeds, applets, and iframes (you can add to this list, or remove from it). Once the page has loaded, you can double click to display them again. The script can optionally display a notification when it blocks something. By default, this notification is shown for 5 seconds.

General site patching

Script Description
Spoof identity as Firefox or Internet Explorer Opera (Key)

This attempts to completely mask your identity as either Firefox or Internet Explorer. Not just the userAgent, but all the other properties are changed as well. Simply set the userAgent to whatever you want, and this script will automatically change all the other properties:

Opera 8
In ua.ini;
badsite.example.com=4
badsite.example.com=5
Opera 9
Use site preferences to set the browser identification to mask as Mozilla or Internet Explorer.

window.opera is also deleted, so most other User JavaScripts will not run after this script has run. That is why it has a name beginning with zz, in the hopes that it will be loaded last. No promises, you have been warned. This script is adapted from Andrew Gregory's page.

Specific site patching

Script Description
Fix Mininova menu Opera (Key)

Deprecated - not needed since Opera 8.5

The menu on Mininova.org relies on an iframe element detecting mouse events, but it is obscured by the page it contains (whether this is correct or not is debatable). This script makes the iframe page detect the events instead so the menu works correctly.

www.start.com Opera (Key)

Deprecated - this patch no longer works, as the site has changed (and has become www.live.com). However, thanks to the work on Opera 9, and Microsoft for fixing the site, the Atlas framework the site used now works in Opera 9.

This patches the www.start.com site. The page uses code to emulate JScript in Mozilla. Opera already implements JScript, but the code incorrectly thinks Opera is Mozilla, so it tries to emulate it anyway, and fails. This User JavaScript fixes the mis-identification, and adds in a few other tweaks to get the (badly written) page working in Opera. I wrote an article describing how this patches the site, and how it demonstrates the power of User JavaScript. The article was featured on the Opera Wiki and in theory should also appear on the My Opera Community website.

Fix ToGoParts menu Opera (Key)

Deprecated - not needed - the site now works without this script

The menu on ToGoParts.com has not been updated since Opera 6. It detects Opera, and sends it a broken copy of the menu script that is intended to work in Opera 6 (it would also fail in Opera 6, but that is another story). This script changes the browser sniffer to make it send Opera the correct menu code.

MSDN extras Opera (Key)

Deprecated - the site has changed and browserjs no longer applies the fixes that this script hooks into

In browserjs, there are some fixes for the MSDN reference site, to fix some browser sniffers and invalid code. This script enhances the behaviour of that script, adding some extra optional functionality to the MSDN site, to allow the member tables to be expanded and collapsed, and to prevent the site forcing you to use its frameset.

Fun

Script Description
Opera 9 Easter egg Opera (Key)

Opera 9 beta 2 was the first proper public release (excluding weekly builds) of Opera to pass the demanding Acid 2 test. To celebrate that fact, and the fact that it was released just after Easter, a small Easter egg was included (written by yours truly), in the site patches for that version. It was nothing big, but it was nice to be able to show that not only is very Opera good at Web standards, it also offers many helpful abilities for users too - two of which are site patching and User JavaScript. The Easter egg has now been removed, as it was never intended to be in the final release (so next time your site patches are updated, the Easter egg will stop working). Of course, that little script has some sentimental value to many of us, for what it represents. It would be a shame for it to disappear forever without being held in the archives. Well, here is the archive. Install it as a User JavaScript, load the Acid 2 test, and watch it for at least two minutes.

Operafy Asa's blog Opera Greasemonkey+Firefox (Key)

Another joke. Asa Dotzler is part of the Firefox team. Unfortunately he has a very skewed view of the world. Many articles in his blog describe features that Firefox has copied from Opera, which he then touts as a new Firefox invention, and he slams Opera users when they mention that Opera had them first. If that is not enough, he will then go on unfounded rants about Opera, spreading lies, and deliberately riling Opera users. His attempts do nothing for either Firefox or Opera, he makes them both look bad, and even annoys many Firefox users as well (occasionally, his continued Opera bashing leads Firefox users to try Opera to see what he is on about, and they end up sticking with Opera). For Firefox, he just comes across as immature and misinformed, making Firefox appear to be as stupid as he is (and I believe that Firefox deserves better than that - despite my preference for Opera). Anyway, to correspond with Asa's childish attitude, I present this script, which shows off Asa's blog for what it really is. Asa sees the world through rose Opera tinted spectacles. Obsessed with Opera.

Fix Acid2 Opera Greasemonkey+Firefox Internet Explorer+Turnabout (partial) (Key)

Deprecated - Opera 9 passes Acid 2 - but this script is still funny ;)

Ok, ok, it's just a joke. At the time of its release, no browsers passed the Acid2 test. So while we wait for the browsers to fix the bugs, and implement the required features, Opera (and Firefox+Greasemonkey) users can use this user JavaScript to ... er ... "fix" the test results. (Oh, and in case you want to see them, here they are: 1, 2, 3, 4.)

Fix Ben Goodger's blog Opera (Key)

Deprecated - the offensive message no longer appears on the site

OK, so it was written as an April Fools joke, but the message is quite serious. "Alternative" browsers like Opera and Firefox have worked hard to become standards compliant, despite the problems caused by Internet Explorer. The message displayed on Ben Goodger's site (http://www.bengoodger.com/weblog/) is simply insulting and inaccurate (try validating his page), and belittles the work that Opera have done. Add this User JavaScript, and it will rewrite the offending script to produce a much more appropriate message.

User JavaScript compatibility

Script Description
Emulate GM functions Opera (Key)

Opera's User JavaScript event listeners and magic variables/functions may offer several advantages over Greasemonkey's more simplistic approach, but Greasemonkey offers a few features that Opera's User JavaScript does not have. It can store persistent data, add temporary menu entries, and also do a few other things. This User JavaScript attempts to replicate these functions in Opera. It is not perfect, but it should allow you to use a few more Greasemonkey scripts in Opera. The file name starts with 'aa' to make sure it loads before all other regular Opera User JavaScripts - no promises, but it should always load before Greasemonkey scripts. It provides these functions:

GM_setValue(key,value)
Stores the variable value as a cookie so it can be retrieved later. With Greasemonkey, these would be available to the script no matter what site it is being used on. With this script, it is available only for the current domain (as with normal cookies). Unlike Greasemonkey, which can store only numbers, strings and boolean values, this script can store regular expressions, dates, functions, arrays and objects as well (it utilises my toRecoverableString function).
GM_getValue(key[,defaultValue])
Returns the previously stored variable using the given key. If none, it returns the defaultValue, or undefined if no defaultValue is provided.
GM_deleteValue(key)
Deletes the stored value - this is not a normal Greasemonkey function, but it just seemed sensible to provide a way to delete them again...
GM_registerMenuCommand(title,function)
Adds a menu item with the specified title that runs the specified function when clicked. Unlike Greasemonkey (which uses the browser's own menu), this script places a menu in the top right corner of the page when needed - it is even keyboard accessible, using spatial navigation.
GM_xmlhttpRequest(details)
Performs an XMLHttpRequest using the XMLHttpRequest constructor. With Greasemonkey, this has no cross domain restrictions. I cannot replicate that behaviour here.
GM_addStyle(CSSString)
Adds a stylesheet LINK tag to the document containing the new CSS. Note that this can cause problems with sites using jQuery, or stylesheet switching scripts.
GM_log(message)
Synonym for window.opera.postError.

Additionally, it maps window._content to window, as this is unnecessarily used by many Greasemonkey scripts.

Fix document.addEventListener Greasemonkey+Firefox (Key)

Deprecated - since Mozilla have failed for so long to fix the bug, HTML 5 now mandates that other browsers copy it instead (sounds like a joke, but that's how it works when you're big and popular, just ask IE) - all current versions of non-deprecated scripts do not need this fix any more

Yes, it is a script for Greasemonkey, not Opera. It should allow Greasemonkey users to run a few more of my scripts, assuming they can make this one run first. Firefox fails to detect the load event on the document, and requires it to be attached to window. This user script fixes the behaviour - it may not run fast enough, since Greasemonkey runs user scripts after the page has loaded anyway. I cannot help with that problem. Ask the Mozilla group to fix the document.addEventListener bug, and ask the Greasemonkey developers to get scripts to run earlier.

Miscellaneous

Script Description
Emulate Wiimote Opera Greasemonkey+Firefox (rename to .user.js) (Key)

Attempts to emulate the Wii Remote API and JavaScript events using a mouse and keyboard. This should allow many games or special pages written for the Internet Channel on Nintendo Wii to work on normal desktop installs. See the script information page for more details.

The script needs to be configured to tell it what pages to run on.

Note that sites may still choose not to provide access to the games as they detect that you are not using the Wii Internet Channel. In these cases, it may help to add the value 'Nintendo Wii' to the ISP Id setting in opera:config (and restart Opera - remember to change it back when you're fnished). Alternatively, you can use Proxomitron or Proximodo to set your user agent to something like this:

Opera/9.10 (Nintendo Wii; U; ; 1621; en)

Note that although this script can work in Greasemonkey, it cannot work as well as in Opera. It can only work in Greasemonkey if the page waits until after it has loaded before attempting to use the API.

Fit-to-width notice Opera (Key)

Displays a small reminder notice at the bottom of the page if there is a horizontal scrollbar, and Fit-to-window-width is not enabled. Especially useful for new or inexperienced users, to remind them to enable Fit-to-window-width. The script provides the option to stop notifying after a certain date, which you can specify in the source.

Terms of use

My normal terms and conditions do not apply to these User JavaScripts, so they have their own set of terms and conditions:

Buttons

To go along with the scripts, drag one of these links to your toolbars (not the personal bar, page bar, or panel) to give yourself a button where you can toggle User JavaScript on and off with a single click - it requires Opera 9 or above:

Checkbox with changing words:
Checkbox with static words:
Just changing words:
Just static words:

Links to other scripts

Support key:

Back to Opera resources | Back to How To Create