#789: code review: simpler, just keep a reference to the style tag

This commit is contained in:
gorhill 2015-02-14 12:27:33 -05:00
parent 65b9ef2468
commit 21ea1a6e03
2 changed files with 10 additions and 16 deletions

View File

@ -68,23 +68,17 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
(function() {
// Were there specific cosmetic filters?
var text = vAPI.specificHideStyleText;
if ( typeof text !== 'string' ) {
if ( vAPI.specificHideStyle instanceof HTMLStyleElement === false ) {
return;
}
vAPI.specificHideStyleText = undefined;
// Is our style tag still available?
var style = document.getElementById('ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
if ( style !== null ) {
if ( document.getElementById('ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5') !== null ) {
return;
}
// Put it back
style = document.createElement('style');
style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
style.appendChild(document.createTextNode(text + '\n{display:none !important;}'));
var parent = document.head || document.documentElement;
if ( parent ) {
parent.appendChild(style);
parent.appendChild(vAPI.specificHideStyle);
}
})();

View File

@ -67,8 +67,6 @@ var localMessager = vAPI.messaging.channel('contentscript-start.js');
var cosmeticFilters = function(details) {
var donthideCosmeticFilters = {};
var hideCosmeticFilters = {};
var style = document.createElement('style');
style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
var donthide = details.cosmeticDonthide;
var hide = details.cosmeticHide;
var i;
@ -96,15 +94,17 @@ var cosmeticFilters = function(details) {
}
}
if ( hide.length !== 0 ) {
var text = vAPI.specificHideStyleText = hide.join(',\n');
var text = hide.join(',\n');
hideElements(text);
var style = vAPI.specificHideStyle = document.createElement('style');
style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
// The linefeed before the style block is very important: do not remove!
style.appendChild(document.createTextNode(text + '\n{display:none !important;}'));
//console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
}
var parent = document.head || document.documentElement;
if ( parent ) {
parent.appendChild(style);
var parent = document.head || document.documentElement;
if ( parent ) {
parent.appendChild(style);
}
}
vAPI.donthideCosmeticFilters = donthideCosmeticFilters;
vAPI.hideCosmeticFilters = hideCosmeticFilters;