📜 ⬆️ ⬇️

UserJS. Part I: Introduction

This is the first article in a series on writing custom scripts for Opera. Initially, a single article was planned, but it has already exceeded 10 screens and is still being finalized.

Articles are designed for an audience that knows javascript and DOM well. Only features of using javascript in userjs are described.

Definition


Userjs are javascript scripts executed by Opera before loading each page, and allowing to modify its contents.

Opera Setup


Initially, after installing Opera, the path to the scripts in the settings is not specified, so to use userjs, go to the settings (the Advanced tab → Content item → the Configure JavaScript button → the User JavaScript Script Folder field) and specify the path to the folder with the scripts.
This can be done also on the settings page at the address “opera: config # UserPrefs | UserJavaScriptFile”.

Running userjs when accessing https is disabled by default for security. If necessary, this can be changed in the settings ("opera: config # UserPrefs | UserJavaScripton HTTPS").
')

Userjs execution order


Opera downloads and executes all the "* .js" files in the specified folder. Thus, to disable the script, it is enough to change its extension. The boot order is not defined in Opera 9, in Opera 10 - in alphabetical order.

The folder is scanned every time the page loads, i.e. When adding, modifying or deleting scripts, you do not need to restart Opera, it’s enough to reload the page.

All userjs are executed before the page loads, with the exception of scripts whose name ends in “.user.js” - they are executed after the page loads for compatibility with GreaseMonkey.

Writing userjs


Cap

Cap is optional. The header contains various information about the script and the author, as well as the address patterns on which the script should be executed ( include ) or not executed (@exclude). By default, the script runs on all pages.

If the script is only useful on a specific site - use include instead of checking the address, it is faster.

 // == UserScript ==
 // @name Cool script
 // @description does some cool thing.
 // @author some@cool.programmer.com
 // @include http://some.host/*
 // @exclude http://some.host/path/*
 // == / UserScript ==


Code

Scripts are executed in the context of the loaded page and share with it the global window object, so you should not create global variables to avoid conflicts. Usually, an anonymous function is used to determine the new scope, which is immediately executed. Inside you should use “var” to declare variables.

 (function () {
   var CONST = "const";
   // do something.
 }) ();


Additional features

For the period userjs, Opera provides several additional features. After all userjs are loaded, they are disabled, so they cannot be used in event handlers, even if these handlers are written in userjs.

defineMagicVariable

window.opera.defineMagicVariable (name, getter, setter) allows you to override the global variable using the get and set value functions. Allows you to cheat scripts on sites. setter is optional (specify null ).

 window.opera.defineMagicVariable ('isIE', function (val) {return true;});


defineMagicFunction

window.opera.defineMagicFunction (name, func) overrides the global function.

 window.opera.defineMagicFunction ('setTitle', function (original, _this, title) {
   window.status = 'Setting title';
   return original.apply (_this, title);
 });


addEventListener

window.opera.addEventListener (name, handler, capture) is interface-compatible with window.addEventListener , but supports additional events. I will not describe everything, the most important are “BeforeEvent.type” and “AfterEvent.type”, where instead of “type” you need to substitute the name of the event (“DOMContentLoaded”, “click”, “load”, ...)

Typical script

The most common userjs, modifying the code page to remove ads, add links, etc. In this case, you must wait for the page to load. Type Code:

 (function () {
   opera.addEventListener ('BeforeEvent.DOMContentLoaded', function (ev) {
     var elem = document.getElementById ('ad');  // block with ads
     elem.style.display = 'none';
   }, true);
 }) ();

Source: https://habr.com/ru/post/61835/


All Articles