addStyle

add Style without GM_addStyle

Version vom 21.01.2021. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/410150/893089/addStyle.js

// ==UserScript==
// @name          addStyle
// @namespace     https://github.com
// @version       0.3.3
// @description   add Style without GM_addStyle
// @match         *://*/*
// @grant         none
// ==/UserScript==

/**
 * Replace GM_addStyle;
 * Support external stylesheet path.
 * @param {String} inputCSS
 */
const addStyle = (inputCSS) => {
  let node;
  let css = inputCSS.trim();
  if (css.length === 0) return;
  if (css.includes('{') && css.includes('}')) {
    // Internal stylesheet
    node = document.createElement('style');
    node.appendChild(document.createTextNode(css));
  } else if (css.includes('.css')) {
    // External stylesheet
    node = document.createElement('link');
    node.rel = 'stylesheet';
    node.href = css;
  } else {
    console.error(`Function addStyle does't support input parameter: ${css}`);
  }
  const heads = document.getElementsByTagName('head');
  if (heads.length > 0) {
    heads[0].appendChild(node);
  } else {
    // No head yet, stick it whereever.
    document.documentElement.appendChild(node);
  }
};