add Style without GM_addStyle
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/410150/919980/addStyle.js
// ==UserScript==
// @name addStyle
// @namespace https://greasyfork.org
// @version 0.3.4
// @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);
}
};