gorhill 2017-11-24 00:39:01 -05:00
parent 8c424c15b2
commit 91de400a64
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -38,19 +38,25 @@ vAPI.userStylesheet = {
disabled: false, disabled: false,
apply: function() { apply: function() {
}, },
inject: function() {
this.style = document.createElement('style');
this.style.disabled = this.disabled;
var parent = document.head || document.documentElement;
if ( parent === null ) { return; }
parent.appendChild(this.style);
var observer = new MutationObserver(function() {
if ( this.style === null ) { return; }
if ( this.style.sheet !== null ) { return; }
parent.appendChild(this.style);
}.bind(this));
observer.observe(parent, { childList: true });
},
add: function(cssText) { add: function(cssText) {
if ( cssText === '' || this.css.has(cssText) ) { return; } if ( cssText === '' || this.css.has(cssText) ) { return; }
if ( this.style === null ) { if ( this.style === null ) { this.inject(); }
this.style = document.createElement('style'); var sheet = this.style.sheet;
this.style.disabled = this.disabled;
var parent = document.head || document.documentElement;
if ( parent !== null ) {
parent.appendChild(this.style);
}
}
var sheet = this.style.sheet,
i = sheet.cssRules.length;
if ( !sheet ) { return; } if ( !sheet ) { return; }
var i = sheet.cssRules.length;
sheet.insertRule(cssText, i); sheet.insertRule(cssText, i);
this.css.set(cssText, sheet.cssRules[i]); this.css.set(cssText, sheet.cssRules[i]);
}, },
@ -67,12 +73,12 @@ vAPI.userStylesheet = {
this.style.sheet.deleteRule(i); this.style.sheet.deleteRule(i);
break; break;
} }
if ( rules.length === 0 ) { if ( rules.length !== 0 ) { return; }
var parent = this.style.parentNode; var style = this.style;
if ( parent !== null ) { this.style = null;
parent.removeChild(this.style); var parent = style.parentNode;
} if ( parent !== null ) {
this.style = null; parent.removeChild(style);
} }
}, },
toggle: function(state) { toggle: function(state) {