Hi,
I have a problem with a script which - on the exactly same page sometimes works
and sometimes
does not. Reloading the page fixes the problem 99% of the time.
The purpose of the script is to replace white (or whitish) background with
something else. Many
other scripts try do this although I tried to be particularly sophisticated -
with features such
as preserving contrast differences.
My only idea is that the problem is that the DOM is loaded which triggers the
script and some
miliseconds later some CSS is loaded that changes the attributes again -
sometimes the CSS is loaded
before DOM loading is finished sometimes after.
However I am not sure how to verfiy that this is indeed the problem?
Assuming this would be the problem is it possible to add some kind of event
listener that would rerun
the script after some CSS has been loaded? Which event listener?
Here is my script.. I am quite new to greasmonkey so I am glad for any
suggestions how to make it
better:
// ==UserScript==
// @name RZ colorfix
// @namespace none yet
// @include *
// ==/UserScript==
//dump("defining script defbackground\n");
var aditb = {
getref: ['body','td','table','div','tr','html','p','window','pre'],
getRGBColor: function(rgb){
var r,g,b;
try{
if(/rgb\((\d+),\s(\d+),\s(\d+)\)/.exec(rgb))
{
r=parseInt(RegExp.$1,10);g=parseInt(RegExp.$2,10);
b=parseInt(RegExp.$3,10);return[r/255,g/255,b/255];}
}catch(e){};
return rgb; // string..
},
mc: function(el, color) {
var nc;
var color1="#FFBB60";
var color2="#FFDAA8";
var color3="#FFE2B4";
switch(el.tagName){
case "WINDOW":
case "BODY":
case "HTML":
nc=color; break;
case "TABLE":
case "DIV":
nc=color1; break;
case "TR": nc=color2; break;
default: nc=color3; break;
}
el.style.background=nc;
el.style.backgroundColor=nc;
el.bgColor=nc;
//dump(el+"\n "+el.tagName+" ..changing color to"+nc+"\n");
GM_log(el+"\n "+el.tagName+" ..changing color to"+nc+"\n");
},
tc: function(color) {
//dump("tc "+color+" type "+typeof(color)+"\n");
switch(typeof(color)){
case "undefined": return false; break;
case "string":
switch(color){
case "white":
case "#FFFFFF":
case "#ffffff":
case "#fff":
case "#FFF":
return true; break;
}
// not really sure about type..
default:
if (color[0] + color[1] + color[2] >= 2.0)
return [color[0],color[1]-0.5,color[2]-0.5];
break;
}
return false;
},
confBackg: function() {
//var rc="#FFB600"; // set to this background color
var rc="#FFD8A0";
// try
//var getref=["*"];
var getref = this.getref;
var rcompback, rcodst, rdtag;
for(var i=0, ges=getref.length; i<ges; i++) {
var getas = getref[i]; var gettag = document.getElementsByTagName(getas);
for(var j=0, ast=gettag.length; j<ast; j++) {
var astag = gettag[j];
//var compback =
document.defaultView.getComputedStyle(astag,null).backgroundColor;
var compback = document.defaultView.getComputedStyle(astag, "")
// .getPropertyValue("background-color");
//dump(document.defaultView.getComputedStyle(astag,
null).getPropertyValue("background-color")+"\n");
//dump(document.defaultView.getComputedStyle(astag,
null).backgroundColor+"\n");
var codst = astag.style.backgroundColor;
var dtag = astag.bgColor;
var mtag = false;
//dump(astag.tagName+": compback "+compback+" codst "+codst+" dtag
"+dtag+"\n\n");
if (compback)
rcompback =
this.getRGBColor(compback.getPropertyValue("background-color"));
else rcompback="transparent";
rcodst = this.getRGBColor(rcodst);
rdtag = this.getRGBColor(rdtag);
//dump(astag+": rcompback "+rcompback+" rcodst "+rcodst+" rdtag
"+rdtag+"\n\n");
// BODY,HTML gets background color even if apparenlty transparent (FFX
Bug)
switch (astag.tagName){
case "WINDOW":
case "BODY":
case "HTML":
mtag=true;
//dump("setting mtag "+astag.tagName+"\n");
}
if (mtag || this.tc(rcompback)) {this.mc(astag,rc);}
else if (this.tc(rcodst)) {this.mc(astag,rc);}
else if (this.tc(rdtag)) {this.mc(astag,rc);}
}
}
}
};
//dump("executing script defbackground\n");
aditb.confBackg();
//dump("done script defbackground\n");
//document.addEventListener('load',aditb.confBackg(),false);
There seems nothing special about the web pages where it sometimes works and
sometimes does not,
one example is http://www.heise.de/newsticker/
Regards Richard
--
You received this message because you are subscribed to the Google Groups
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/greasemonkey-users?hl=en.