Connecting Tech Pros Worldwide Forums | Help | Site Map

Element Insertion question

Alex
Guest
 
Posts: n/a
#1: Feb 25 '06
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');
x.setAttribute("language", "javascript");
x.setAttribute("type", "text/javascript");
x.innerHTML ="window.load = parent.al();";
document.getElementsByTagName('head')[0].appendChild(x);

And (another try):

document.getElementsByTagName('body')[0].setAttribute("onload",
"javascript:parent.al();");


None of them seems to do anything. Is there a Javascript error in my
reasoning or some other evident problem with the code?


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Feb 25 '06

re: Element Insertion question


"Alex" <a.r.austin@gmail.com> writes:
[color=blue]
> 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');[/color]

What is "x". I assume it's a reference to the current window.
[color=blue]
> x.setAttribute("language", "javascript");[/color]

If wo, you are calling "setAttribute" on the window.
[color=blue]
> x.setAttribute("type", "text/javascript");
> x.innerHTML ="window.load = parent.al();";[/color]

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.
[color=blue]
> document.getElementsByTagName('head')[0].appendChild(x);[/color]

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.
[color=blue]
> And (another try):
>
> document.getElementsByTagName('body')[0].setAttribute("onload",
> "javascript:parent.al();");[/color]

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);

[color=blue]
> None of them seems to do anything. Is there a Javascript error in my
> reasoning or some other evident problem with the code?[/color]

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
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Martin Honnen
Guest
 
Posts: n/a
#3: Feb 25 '06

re: Element Insertion question




Alex wrote:
[color=blue]
> 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');
> x.setAttribute("language", "javascript");
> x.setAttribute("type", "text/javascript");
> x.innerHTML ="window.load = parent.al();";
> document.getElementsByTagName('head')[0].appendChild(x);[/color]

Simply doing
window.addEventListener(
'load',
function (evt) {
if (typeof parent.al != 'undefined') {
parent.al();
}
},
false
);
in your Greasemonkey script suffice to add an event listener for the
load event which then calls the function named al in the parent if it is
available.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Alex
Guest
 
Posts: n/a
#4: Feb 26 '06

re: Element Insertion question


For Martin Honnen:
---------------------------------------------------
Error: [Exception... "'Permission denied to get property Window.al'
when calling method: [nsIDOMEventListener::handleEvent]" nsresult:
"0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "<unknown>"
data: no]
---------------------------------------------------

For Lasse Reichstein Nielsen:
(first, allow me to thank you for your step by step advice, it should
have been "x=" not "x.")
Your way also gives an error:
---------------------------------------------------
Error: uncaught exception: Permission denied to get property Window.al
---------------------------------------------------

Is there away to disable the security module in Firefox so it would
permit to access the AL function?

Closed Thread