ReadViewSDK/Sources/RDReaderView/EPUBCore/Resources/cssInjector.js

46 lines
1.4 KiB
JavaScript

(function() {
if (window.RDInjectedCSS) { return; }
function applyStyle(doc, identifier, cssText) {
if (!doc || !doc.head) { return; }
var style = doc.getElementById(identifier);
if (!style) {
style = doc.createElement('style');
style.id = identifier;
doc.head.appendChild(style);
}
style.textContent = cssText || '';
}
function walkDocuments(rootDoc, includeFrames, visitor) {
if (!rootDoc) { return; }
visitor(rootDoc);
if (!includeFrames || !rootDoc.querySelectorAll) { return; }
Array.prototype.forEach.call(rootDoc.querySelectorAll('iframe'), function(frame) {
try {
if (!frame.contentDocument) { return; }
walkDocuments(frame.contentDocument, includeFrames, visitor);
} catch (error) {
}
});
}
window.RDInjectedCSS = {
setStyle: function(identifier, cssText, options) {
var includeFrames = !!(options && options.includeFrames);
walkDocuments(document, includeFrames, function(doc) {
applyStyle(doc, identifier, cssText);
});
},
removeStyle: function(identifier, options) {
var includeFrames = !!(options && options.includeFrames);
walkDocuments(document, includeFrames, function(doc) {
var style = doc.getElementById(identifier);
if (style && style.parentNode) {
style.parentNode.removeChild(style);
}
});
}
};
})();