0

I write code for Greasemonkey and I have an iframe without a src in which I add code like this:

 var appendToHead = function(iframe, content) {
    var doc = iframe[0].contentWindow.document;
    var element = doc.createElement('script');
    element.type = 'text/javascript';
    element.text = content;
    doc.head.appendChild(element);
};


I see in Firebug that the function is added in the iframe. But when I try to get them, EG frames[0].contentWindow.functionName I get undefined and still see them in debugger.

Before I used Greasemonkey this approach worked well. How can invoke these functions?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Suhan
  • 1,434
  • 2
  • 13
  • 28
  • possible duplicate of [How do I access an iframe's javascript from a userscript?](http://stackoverflow.com/questions/11638595/how-do-i-access-an-iframes-javascript-from-a-userscript) – Brock Adams Oct 10 '13 at 20:23

1 Answers1

1

See How do I access an iframe's javascript from a userscript.

You would invoke the function like: frames[0].functionName();, but it must be from injected code, not the Greasemonkey script scope. (Except that @grant none might work in some cases.)

You may also need a delay, depending. Post a full working example of what you are doing.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • So without @grant none there is no way i can invoke function's from Greasemonkey script scope? And why need a delay? – Suhan Oct 10 '13 at 12:16
  • And if i choose not to use api with grant none, how can i load big css files(if not to do big string)? – Suhan Oct 10 '13 at 12:23
  • That's right, there is no way you can invoke the functions from Greasemonkey script scope without `@grant none` -- which I don't recommend. Inject the code. The CSS bit is not clear. Might need to ask another question for that. – Brock Adams Oct 10 '13 at 12:39
  • i use GM_addStyle(GM_getResourceText(name)) on what to replace GM_getResourceText? – Suhan Oct 10 '13 at 13:21
  • Don't replace `GM_getResourceText`; just inject the part of your code that calls iframe code. And before you ask, see ["How to call Greasemonkey's GM_ functions from code that must run in the target page scope"](http://stackoverflow.com/a/14255440/331508). – Brock Adams Oct 10 '13 at 20:29