Skip to content

UserScripts

Carl edited this page Mar 29, 2022 · 4 revisions

Bromite has experimental UserScript support since version 94.0.4606.109.

It is currently possible to add user scripts in two ways:

  • by selecting files from the picker in the settings
  • downloading the scripts and opening it from downloads (if ends with .user.js) The new imported scripts are disabled by default: they can be activated via the menu visible on the right.

Supported headers

UserScript support currently matches the available functionality from the Chromium Desktop version; parsed headers are:

  • @name
  • @version
  • @description
  • @include, @exclude, @match, @exclude_match (only http and https)
  • @run-at document-start After the documentElement is created, but before anything else happens document-end After the entire document is parsed. Same as DOMContentLoaded document-idle Sometime after DOMContentLoaded, as soon as the document is "idle". Currently this uses the simple heuristic of: min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no particular injection point is guaranteed

NOTE: If no patterns are specified then the user script will default to @include *; that means each and every website.

The URL-patterns are so defined:

// <url-pattern> := <scheme>://<host><port><path> | '<all_urls>'
// <scheme> := '*' | 'http' | 'https'
// <host> := '*' | <IPv4 address> | [<IPv6 address>] |
//           '*.' <anychar except '/' and '*'>+
// <port> := [':' ('*' | <port number between 0 and 65535>)]
// <path> := '/' <any chars>
//
// * Host is not used when the scheme is 'file'.
// * The path can have embedded '*' characters which act as glob wildcards.
// * '<all_urls>' is a special pattern that matches any valid URL that contains
//   a valid scheme (as specified by valid_schemes_).
// * The '*' scheme pattern excludes file URLs.
//
// Examples of valid patterns:
// - http://*/*
// - http://*/foo*
// - https://*.google.com/foo*bar
// - file://monkey*
// - http://127.0.0.1/*
// - http://[2607:f8b0:4005:805::200e]/*
//
// Examples of invalid patterns:
// - http://* -- path not specified
// - http://*foo/bar -- * not allowed as substring of host component
// - http://foo.*.bar/baz -- * must be first component
// - http:/bar -- scheme separator not found
// - foo://* -- invalid scheme
// - chrome:// -- we don't support chrome internal URLs

Where to find user scripts?

Use https://greasyfork.org/

Some scripts might not work if the URL patterns are tailored for the Desktop version of websites.

Security and trust notice

Always read the description and source code of the scripts before installing them: you are allowing third-party Javascript code to run on webpages during your navigation experience.

This is a major security risk for privacy and safety.

Example script

Here you find a working example that eliminates the Google popup, useful in always-incognito:

// ==UserScript==
// @name         Remove Google Consent
// @namespace    google
// @version      0.0.1
// @description  Autohide Accepts Cookies
// @author       uazo
// @match        https://*.google.com/search?*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    var prepareStyleSheet = function() {
        var style = document.createElement('style');
        //style.setAttribute('media', 'screen');
        style.appendChild(document.createTextNode(''));
        document.head.appendChild(style);
        style.sheet.insertRule('body { overflow:scroll !important;position:unset !important }');
    };
	
	var hideConsent = function() {
		document.getElementById("lb").style.display = "none";
	};

    var checkElementThenRun = function(selector, func) {
        var el = document.querySelector(selector);
        if ( el == null ) {
            if (window.requestAnimationFrame != undefined) {
                window.requestAnimationFrame(function(){ checkElementThenRun(selector, func)});
            } else {
                document.addEventListener('readystatechange', function(e) {
                    if (document.readyState == 'complete') {
                        func();
                    }
                });
            }
        } else {
            func();
        }
    }

    document.cookie = 'CONSENT=YES+IT.it+V13+BX;domain=.google.com';
    checkElementThenRun('head', prepareStyleSheet);
    checkElementThenRun('#lb', hideConsent);
})();

Flags

You can enable #enable-userscripts-log in chrome://flags to have verbose logging available via adb logcat | grep UserScripts. The output will look like this:

03-29 17:20:41.240 11883 11897 I chromium: [INFO:user_scripts_renderer_client.cc(33)] UserScripts: RenderThreadStarted
03-29 17:20:41.241 11883 11897 I chromium: [INFO:user_scripts_renderer_client.cc(44)] UserScripts: Configuration Updated
03-29 17:20:41.276 11883 11897 I chromium: [INFO:script_injection_manager.cc(140)] UserScripts: DidCreateDocumentElement -> DOCUMENT_START
03-29 17:20:41.276 11883 11897 I chromium: [INFO:user_script_set_manager.cc(44)] UserScripts: GetAllInjections
03-29 17:20:41.276 11883 11897 I chromium: [INFO:url_pattern_set.cc(239)] UserScripts: URLPatternSet::MatchesURL false http://example.com
03-29 17:20:41.276 11883 11897 I chromium: [INFO:user_script.cc(121)] UserScripts: No Match for url_set
03-29 17:20:41.276 11883 11897 I chromium: [INFO:user_script_set.cc(183)] UserScripts: Match name=MyScript1 (1).js id=_5 url=http://example.com
03-29 17:20:41.291 11883 11897 I chromium: [INFO:script_injection_manager.cc(171)] UserScripts: DidDispatchDOMContentLoadedEvent -> DOCUMENT_END
03-29 17:20:41.291 11883 11897 I chromium: [INFO:user_script_set_manager.cc(44)] UserScripts: GetAllInjections
03-29 17:20:41.291 11883 11897 I chromium: [INFO:url_pattern_set.cc(239)] UserScripts: URLPatternSet::MatchesURL false http://example.com
03-29 17:20:41.291 11883 11897 I chromium: [INFO:user_script.cc(121)] UserScripts: No Match for url_set
03-29 17:20:41.291 11883 11897 I chromium: [INFO:user_script_set.cc(183)] UserScripts: Match name=MyScript1 (1).js id=_5 http://example.com
03-29 17:20:41.314 11883 11897 I chromium: [INFO:script_injection_manager.cc(225)] UserScripts: RunIdle -> DOCUMENT_IDLE
03-29 17:20:41.314 11883 11897 I chromium: [INFO:user_script_set_manager.cc(44)] UserScripts: GetAllInjections
03-29 17:20:41.314 11883 11897 I chromium: [INFO:url_pattern_set.cc(239)] UserScripts: URLPatternSet::MatchesURL false http://example.com
03-29 17:20:41.314 11883 11897 I chromium: [INFO:user_script.cc(121)] UserScripts: No Match for url_set
03-29 17:20:41.314 11883 11897 I chromium: [INFO:user_script_set.cc(183)] UserScripts: Match name=MyScript1 (1).js id=_5 url=http://example.com

This allows to debug both Bromite UserScripts functionality and the user scripts themselves.

Clone this wiki locally