Safari App Extension injected scripts are not unloaded when refreshing page in Safari

Originator:jonas.m.gessner
Number:rdar://30446857 Date Originated:9.2.2017
Status:Open Resolved:
Product:macOS + SDK Product Version:10.12.3
Classification: Reproducible:Always
 
Area:
Safari

Summary:
When using a basic injected script that registers for messages sent by the app extension, the message handlers are not removed when refreshing the page. However the message handlers are added again each time the page is refreshed, because the script is injected again every time the page refreshes. This leads to a situation where a message handler is called multiple times when refreshing the page multiple times. Normal navigation is not affected.

Steps to Reproduce:
1. Add a Safari App Extension to a Cocoa App
2. Add a message handler In the Safari Extension's injected script
3. Show an alert in the message handler function for visual feedback
4. In the App Extension, post a message to the injected script when the toolbar item is clicked
5. Run the extension in Safari
6. Visit a website and press the toolbar item. The alert should show once.
7. Refresh the website once or more, then press the toolbar item. The alert should show multiple times.

Expected Results:
The alert should only show once at all times. When an injected script is loaded into a page the old injected script and its message handlers should be completely removed.

Actual Results:
The alert is shown multiple times and the message handlers are not being removed when refreshing.

Version:
10.12.3

Notes:
Since reproducing this bug is not trivial I have made a simple example project with the latest version of Xcode, all ready to be tested. It is available here: https://github.com/JonasGessner/SafariAppExtensionBug

Duplicates

Number Status Originator Product Title
rdar://30618211 Duplicate/30446857 ben macOS + SDK Safari App Extension injected scripts are not unloaded when refreshing page in Safari

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!