BigTSDMB / setStyle

// ==UserScript==
// @exclude       *
// @namespace     BigTSDMB
// @author        BigTSDMB
// @name          $LIBRARY setStyle


// ==UserLibrary==
// @name          setStyle
// @description   Adds (or modifies) styles of a document. Fully featured replacement for GM_addStyle.
// @license       MIT
// @version       1.0

// ==/UserScript==

// ==/UserLibrary==

function setStyle(css, attributes, important) {
  if (typeof attributes == 'string') { attributes = { id: attributes }; }
  let style = document.getElementById(attributes?.id) || document.createElement('style');
  style.textContent = css;
  if (attributes) { Object.assign(style, attributes); }
  if (document.documentElement) {
    document.documentElement.appendChild(style);
  } else {
    new MutationObserver( (_, observer) => {
      observer.disconnect();
      document.documentElement.appendChild(style);
    }).observe(document, { childList: true } );
  }
  if (important) { //if important, append style at the end of the document after page loads
    addEventListener('load', () => { document.documentElement.appendChild(style) });
  }
  return style;
}