1

I have a javascript debug code that I need to run from time to time on specific website.

The site has several iframes.

When I use Chrome's debug console I can choose which iFrame to run the code.

I created a bookmark with the code javascript:my_debug_code() - but how can I tell it to run on the iFrame I need?

Edit

This is not a duplicate of Invoking JavaScript code in an iframe from the parent page as was suggested.

Since I wasn't trying to run the code from the page itself.

My question is different - I want to run the code from a bookmark.

Answer

I can't post a new answer, because question is marked is duplicated.

But taken from @Gothdo answer, apparently all it took for me was to add to my bookmark:

javascript:window.frames[0].my_debug_code()
Community
  • 1
  • 1
Igal S.
  • 13,146
  • 5
  • 30
  • 48

1 Answers1

2

You can do this provided that the iframe has the same origin as the main window. Simply create a new script and append it to the <head> element of that iframe.

const code = "console.log('hello world from iframe', location.href)"
     ,iframeName = "YOUR-IFRAME-NAME"

const script = document.createElement("script")
window.frames[iframeName].document.head.appendChild(script)
script.src = "data:text/javascript;base64," + btoa(code)
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • I have found the correct answer from this answer. I didn't need the entire script. Just the windows.frames[num] which did the trick for me. – Igal S. May 18 '16 at 04:38