@livelybone/script-injector
Version:
A module for script dynamic injection
139 lines (117 loc) • 4.33 kB
JavaScript
/**
* Bundle of @livelybone/script-injector
* Generated: 2022-06-25
* Version: 1.2.0
* License: MIT
* Author: 2631541504@qq.com
*/
import stringHash from 'string-hash';
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
enumerableOnly && (symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
})), keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = null != arguments[i] ? arguments[i] : {};
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
_defineProperty(target, key, source[key]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function inject(content, options) {
var _options$attrs$type, _options$attrs, _options$attrs$type2, _options$attrs2;
var map = {
jsInline: {
tag: 'script',
attrs: {
type: (_options$attrs$type = options === null || options === void 0 ? void 0 : (_options$attrs = options.attrs) === null || _options$attrs === void 0 ? void 0 : _options$attrs.type) !== null && _options$attrs$type !== void 0 ? _options$attrs$type : 'text/javascript'
}
},
jsLink: {
tag: 'script',
attrs: {
type: (_options$attrs$type2 = options === null || options === void 0 ? void 0 : (_options$attrs2 = options.attrs) === null || _options$attrs2 === void 0 ? void 0 : _options$attrs2.type) !== null && _options$attrs$type2 !== void 0 ? _options$attrs$type2 : 'text/javascript',
src: content
}
},
cssInline: {
tag: 'style',
attrs: {}
},
cssLink: {
tag: 'link',
attrs: {
type: 'text/css',
rel: 'stylesheet',
href: content
}
}
};
var doc = (options === null || options === void 0 ? void 0 : options.document) || document;
var type = (options === null || options === void 0 ? void 0 : options.type) || 'js';
var id = "".concat(type, "-").concat((options === null || options === void 0 ? void 0 : options.id) || String(stringHash(content)));
var tag = doc.getElementById(id);
if (tag && options !== null && options !== void 0 && options.shouldReplace) {
tag.parentElement.removeChild(tag);
tag = null;
}
return new Promise(function (resolve, reject) {
var _options$isLink;
var isLink = (_options$isLink = options === null || options === void 0 ? void 0 : options.isLink) !== null && _options$isLink !== void 0 ? _options$isLink : /(^https?:)|(^\.{0,2}\/)/.test(content);
var key = "".concat(type).concat(isLink ? 'Link' : 'Inline');
var info = map[key];
if (!tag) {
tag = doc.createElement(info.tag);
var attrs = _objectSpread2(_objectSpread2({}, options === null || options === void 0 ? void 0 : options.attrs), info.attrs);
Object.keys(attrs).forEach(function (attr) {
tag.setAttribute(attr, String(attrs[attr]));
});
tag.id = id;
if (!isLink) {
tag.innerHTML = content;
} else {
tag.onload = function () {
return resolve(tag);
};
tag.onerror = function (ev) {
var err = new Error("The ".concat(info.tag, " element load failed."));
Object.assign(err, {
event: ev
});
reject(err);
};
}
var injectIn = (options === null || options === void 0 ? void 0 : options.injectIn) || 'head';
var parent = injectIn === 'body' ? doc.body : doc.head;
parent.appendChild(tag);
if (!isLink) {
resolve(tag);
}
} else {
reject(new Error("The ".concat(info.tag, " element already exists.")));
}
});
}
export { inject };