0

I wanna create an script that changes the way a chat work. The chat is made with AJAX technology but the point is that I cannot edit the chat at any level.

The point of the script is to add a sound when you receive a message. I was thinking about editing the AJAX function and add some script there to make a sound. The point is to add JS and put a new function over the original one.

So... How can I create new JavaScript from a JavaScript written from outside of the iframe? I know the way you can edit the DOM but not how to add new scripts.

I can hear another ways to edit the chat if you have new ideas but this one (I'm not even feeling confortable with it).

Flerex
  • 324
  • 2
  • 12
  • 1
    If the iframe is on a different domain then you'll run in to security issues... – Lee Taylor Aug 25 '14 at 22:00
  • @LeeTaylor yeah, the chat is on the same domain. Don't worry about that. – Flerex Aug 25 '14 at 22:02
  • So, if it's on the same domain why can't you change the code in some way? – Lee Taylor Aug 25 '14 at 22:09
  • @LeeTaylor It was my fault. I thought that when you add new scripts when the DOM is already loaded they wouldn't be parsed, but now I noticed that this just happens with jQuery. – Flerex Aug 25 '14 at 22:12

2 Answers2

1

Lee Taylor is right that you will only be able to edit an iframe from the same domain, but since you say you can edit the dom, you can always add a script tag into the dom, that will get parsed immediately.

e.g.

var scriptElement = document.createElement('script');
scriptElement.textContent = 'alert(\'hello world\');';
iframe.body.appendChild(scriptElement);
Nick
  • 220
  • 1
  • 2
  • 12
  • I'm feeling kinda stupid right now. I don't know why I had on mind that when you add new – Flerex Aug 25 '14 at 22:08
0

If the iframe is on the same domain, just create a script and add it to the head.

$('<script />', {html: code}).appendTo($('iframe').contents().find('head'))

or

$('<script />', {src: srcUrl}).appendTo($('iframe').contents().find('head'))

Sources:

https://stackoverflow.com/a/4975050/3969707

https://stackoverflow.com/a/1639342/3969707

Community
  • 1
  • 1
Drew Faubion
  • 441
  • 3
  • 12