Skip to content

Instantly share code, notes, and snippets.

@ktemkin
Last active November 11, 2021 06:25
Show Gist options
  • Save ktemkin/eaa19a42e63ffff88d2033b063520178 to your computer and use it in GitHub Desktop.
Save ktemkin/eaa19a42e63ffff88d2033b063520178 to your computer and use it in GitHub Desktop.
user script body to set Gerrit fonts
// ==UserScript==
// @name Gerrit Fonts
// @namespace https://gerrit.YOURDOMAINHERE.com
// @match https://gerrit.YOURDOMAINHERE.com/*
// ==/UserScript==
'use strict';
(function() {
const font = "MonoLisa";
// Gerrit dynamically populates its window, and then populates _that_ with a ton of shadow DOMs.
// Accordingly, we'll run this when the main DOM is modified to add all the shadow DOMs.
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver((mutations, observer) => {
// This code is tantamount to a war crime.
document
.querySelector("#app").shadowRoot
.querySelector("#app-element").shadowRoot
.querySelector("gr-diff-view").shadowRoot
.querySelector("gr-diff-host").shadowRoot
.querySelector("gr-diff").shadowRoot
.styleSheets[0].addRule("*", 'font-family: "' + font + '" !important');
});
// Select the node that will be observed for mutations.
// Generally, adding something to the root node would mean it run a lot;
// but thanks to Gerrit's aggressive shadow-dom'ing it only runs ~three times.
const targetNode = document.getRootNode();
// Options for the observer (which mutations to observe)
const config = { childList: true, subtree: true, attribute: true };
observer.observe(targetNode, config);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment