2

I am developing an Safari App Extension and I would like to identify opened tab. I found no id or no way to do that.

Chrome has an awesome API for doing that: https://developer.chrome.com/extensions/tabs. How can I do it with Safari App Extension (in Swift)?

Safari tab API is very poor and does not contain ID (https://developer.apple.com/documentation/safariservices/sfsafaritab)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tchoupinax
  • 235
  • 3
  • 12

3 Answers3

1

You are right. The API is not easy to use. However, there is a way to get the ID. The SFSafariPage itself.

The SFSafariPage adopt NSSecureCoding. You can use savedPage.isEqual(newPage) to compare if the same page you get from getActiveTab or something. The page is a pointer or proxy. But the content is unique.

P.S You should very careful about the SFSafariExtensionHandler. The handler is always a new object. Use the singleton model save page index could make sure data won't lost.

user25917
  • 797
  • 7
  • 18
0

A little more detail would be helpful. What do you mean 'ID'? What do you actually want to do? You can get access to tabs in your extension handler using SFSafariApplication API. I've not tested it myself, but it may look something like this:

SFSafariApplication.getActiveWindow { (window) in
    window?.getActiveTab { (tab) in
       ...do something with the tab...
    }
}
quin
  • 276
  • 1
  • 9
  • Hello, By getting active tab, i have no idea which tab it is. You can look at Chrome documentation (https://developer.chrome.com/extensions/tabs), i can identify a tab by an id. If i open a tab, i can reach it anymore from background, i can't said "it is the same tab that i opened". For my example, i want to open two tab and when i receive request, be able to know "It is tab number one, it it tab number two" – Tchoupinax Aug 02 '19 at 07:25
  • Ah I see. I wonder if you might be able to use some state in javascript? For example, when you open a new tab, send a message that will set some identifier that is managed by your extension. – quin Aug 16 '19 at 11:15
0

Could you please elaborate a bit what you are trying to achieve?

I suppose, at the worst case, you can inject a script in every page, where you will generate a random ID, send it to the host app, which will store them all into UserDefaults. Then later, when you need to retrieve a tab, you'd iterate over all tabs and establish some communication with the one you are targeting with dispatchMessageToScript.

SFSafariTab inherits hashValue from NSObject. This could allow you to identify your tab.

Greg de J
  • 320
  • 1
  • 2
  • 8
  • Hello, Thank you for your suggestion but it does not work. Indeed, SFSafariTab is not a class that represent the tab object but it is a *proxy* for this tab. ("A proxy for a tab in a Safari window." https://developer.apple.com/documentation/safariservices/sfsafaritab). Hash or object memory reference change for each call. :/ – Tchoupinax Aug 02 '19 at 07:20
  • Sorry for my initial answer, you are absolutely right. I edited my answer. – Greg de J Aug 16 '19 at 14:14
  • Haha I just realized quin basically recommended the same thing :) – Greg de J Aug 16 '19 at 14:15
  • According to your new description, i image this example: You have two tabs (google.com and ebay.com). You generate random ID (1 and 2) and you store in the UserDefaults like you suggested. If i close ebay tab, how you know it ? :) – Tchoupinax Aug 26 '19 at 07:44