Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari extension running injected javascript multiple times

I have copied the code from the following example on the Apple Developer site.

https://developer.apple.com/library/content/documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html#//apple_ref/doc/uid/TP40009977-CH14-SW1

When I run the extension, it runs normally except the injected script is run 3 times causing the output to be written to console.log 3 times. I also put a alert in the doBigCalc function and it opened 3 alerts.

Why is this happening/is it supposed to happen?

like image 941
Craig Siemens Avatar asked Jul 06 '10 22:07

Craig Siemens


2 Answers

You don't speak to your implementation, but it could be that your code isn't the problem. The key is understanding that an injected script is loaded .... From the injected script documentation:

Scripts are injected into the top-level page and any children with HTML sources, such as iframes. Do not assume that there is only one instance of your script per browser tab.

like image 163
Rob Wilkerson Avatar answered Oct 26 '22 08:10

Rob Wilkerson


The documentation mentions that you can dispatch messages using event handlers on three different levels:

safari.application.activeBrowserWindow.activeTab.addEventListener("message", waitForMessage, false);

safari.application.activeBrowserWindow.addEventListener("message", waitForMessage, false);

safari.application.addEventListener("message", waitForMessage, false);

But you should choose one of the the three. You aren't attaching event listeners to all three are you? If you are, you'll get it run three times for sure.

like image 24
Andrew Avatar answered Oct 26 '22 07:10

Andrew