views:

52

answers:

2

Hi, I am trying to code a safari extension similar to Bubble Translate for Chrome.

when you click a button on the toolbar, it automatically translates the text currently selected to the language of your choice using the Google language API.

I use the following injected script to get the highlighted text and display the result (as an alert for the time being): http://pastebin.com/bYVuQAmp

I use the following global script to call the injected script if neccessary: hxxp://pastebin.com/VHaAKM5a

The problem I have is the following:

The script does not just get injected into the main page but also into ads and similar stuff that is embedded into the page. Due to that, the selected text gets translated multiple times because all the embedded scripts in one page respond to the message.

How can I make sure that the script is injected only into the right page or only the right page responds?

Thank you,

Philipp

A: 

Maybe you could check that the page in which the script is injected is not inside a frame:

if (window == window.parent) { /* you're not inside a frame! */ }

I'm not sure if it works from inside <object> tags embedding HTML though. Chances are that yes.

zneak
A: 

That actually solves the problem. Thank you so much!

Philipp
If you want to comment a message, use the "add comment" link below it. Also, if it worked, consider marking the answer as correct (by clicking the √ icon left to it). :)
zneak