Antrikshy
Antrikshy

Reputation: 3106

How do I only enable a Safari toolbar button extension when the URL meets certain criteria ('validate' check)?

I have a Safari extension that places a simple button in the toolbar. It works fine right now, but I want to handle a 'validate' event in this way:

1. Button is grey/disabled
2. Listen for 'validate' event (already done)
3. Check URL to see if last four letters are .gif (can implement)
4. Enable button

I am working in a Global.html file and I am new to JS and Safari.

Upvotes: 0

Views: 191

Answers (1)

chulster
chulster

Reputation: 2829

Inside your validate event handler, the target property of the event will refer to the UI element that is emitting the event -- in your case, the toolbar button. Toolbar buttons (instances of SafariExtensionToolbarItem) have a disabled property, which you can set to true or false.

Example:

safari.application.addEventListener('validate', function (evt) {
    if (evt.command == 'myToolbarItemCommand') {
        // `toolbarButtonShouldBeEnabled` stands for some test
        if (toolbarButtonShouldBeEnabled) {
            evt.target.disabled = false;
        } else {
            evt.target.disabled = true;
        }
    }
}, false);

Upvotes: 1

Related Questions