Alex said:
I am trying to insert Javascript code with grease monkey so it would
insert to a page a call to a parent frame function al()
I tried (portion of the code):
x.document.createElement('script');
What is "x". I assume it's a reference to the current window.
x.setAttribute("language", "javascript");
If wo, you are calling "setAttribute" on the window.
x.setAttribute("type", "text/javascript");
x.innerHTML ="window.load = parent.al();";
And setting innerHTML on the window (bad!).
I wouldn't use innerHTML to set the content of a script, since the
content type of a script element is CDATA, not html. It might work
anyway, though.
The script you try to run is
window.load = parent.al()
This
1) calls the parent.al function immediately, not on load
2) assigns the result of this call to window.load, which isn't
a special property. You might be thinking of window.onload.
document.getElementsByTagName('head')[0].appendChild(x);
Not knowing the constraints of greaseMonkey, I would try this:
var s = document.createElement("script");
s.type="text/javascript";
s.text = "window.onload = parent.al";
document.body.appendChild(s)
(add "x." before "document" if greaseMonkey requires it).
However, it would probably be better to just do:
window.onload = parent.al
directly as the greaseMonkey script, instead of trying to build a new
script element and have it executed. You are after all already executing
a script.
And (another try):
document.getElementsByTagName('body')[0].setAttribute("onload",
"javascript
arent.al();");
The body element can be found as "document.body", which is just as
standard compliant as using getElementsByTagName (and even the same
standard).
Setting onload properties using setAttribute is not as safe as setting
them directly. Just do:
document.body.onload = function(){ parent.al(); }
or
document.body.onload = parent.al;
However, you might want to avoid setting onload from a greaseMonkey
script this way, since it might overwrite what the page put there.
Use a non-destructive way instead:
document.body.addEventListener("load", parent.al, true);
None of them seems to do anything. Is there a Javascript error in my
reasoning or some other evident problem with the code?
What browser are you using? (Both Firefox and Opera supports
greaseMonkey scripts, but I'll assume it's Firefox).
What does the Javascript console print?
When does greaseMonkey execute its scripts? Has the page already loaded?
(then assigning to onload properties won't do anything anyway)?
/L