Safari Reference Library Apple Developer
Search

SafariExtension Class Reference

Technology area
Safari Extensions
Availability
Available in Safari 5.0 and later.

Overview

The SafariExtension class represents your extension outside of the web content; an instance of the class is accessible as safari.extension. Its counterpart class for scripts running within the web content is SafariContentExtension.

Adding and removing style sheets and scripts. Adding or removing a content style sheet applies to pages immediately. Adding or removing a content script applies only to pages opened or reloaded after that point. Removing a style sheet or a script that is in the Info.plist file only removes it from the current browser session.

Whitelists and blacklists. A content script or style sheet may specify a blacklist and a whitelist; both are optional. See “The Extension Builder Interface” in Safari Extensions Development Guide for a description of the pattern format. The script or style sheet will be applied to a page only in the following cases:

Tasks

Working with Extensions

Adding and Removing Scripts and Style Sheets

Storing Settings

Properties

bars

All of the extension’s bars.

readonly attribute array bars
Discussion

If there are no bars, the array is empty.

If multiple windows are open, there might be duplicates. Each bar appears separately for every window it appears in. When updating a bar, you should usually update it in every window.

Availability

baseURI

The URI that corresponds to the root of the extension’s bundle.

readonly attribute DOMString baseURI
Availability

globalPage

The extension’s global page, or null if the extension doesn’t have a global page.

readonly attribute SafariExtensionGlobalPage globalPage
Availability

secureSettings

A storage object for the extension’s secure settings.

readonly attribute SafariExtensionSecureSettings secureSettings
Discussion

Use settings for normal settings.

Availability

settings

A storage object for the extension’s normal settings.

readonly attribute SafariExtensionSettings settings
Discussion

Do not store sensitive information with this method, such as passwords or credit card numbers. Use secureSettings instead.

Availability

toolbarItems

All of the extension’s toolbar items.

readonly attribute array toolbarItems
Discussion

If there are no toolbar items, this is an empty array.

If multiple windows are open, there might be duplicates. Each toolbar item appears separately for every window it appears in. When updating a toolbar item, you should usually update it in every window.

Availability

Methods

addContentScript

Adds a content script from a string.

DOMString addContentScript (in DOMString source, in array whitelist, in array blacklist, in boolean runAtEnd);

Parameters
source

The source code of the script.

whitelist

A list of patterns matching URLs of pages that the script should be run on.

blacklist

A list of patterns matching URLs of pages that the script should not be run on.

runAtEnd

If true, the script waits until the page is fully loaded before running.

Return Value

If the script was successfully added, a generated URL that can be used to remove the script; otherwise, null.

Discussion

If runAtEnd is true, the script is run as soon as the page has completely finished loading. Otherwise, it is run as soon as the DOM is ready, before loading subresources such as images and the contents of frames, which allows you to block resources from being loaded.

Availability
  • Available in Safari 5.0 and later.

addContentScriptFromURL

Adds a content script from a URL.

DOMString addContentScriptFromURL (in DOMString url, in array whitelist, in array blacklist, in boolean runAtEnd);

Parameters
url

The URL of the script.

whitelist

A list of patterns matching URLs of pages that the script should be run on.

blacklist

A list of patterns matching URLs of pages that the script should not be run on.

runAtEnd

If true, the script waits until the page is fully loaded before running.

Return Value

If the script was successfully added, the supplied URL; otherwise, null.

Discussion

If runAtEnd is true, the script is run as soon as the page has completely finished loading. Otherwise, it is run as soon as the DOM is ready, before loading subresources such as images and the contents of frames, which allows you to block resources from being loaded.

Availability
  • Available in Safari 5.0 and later.

addContentStyleSheet

Adds a content style sheet from a string.

DOMString addContentStyleSheet (in DOMString source, in array whitelist, in array blacklist);

Parameters
source

The source code of the style sheet.

whitelist

A list of patterns matching URLs of pages that the script should be applied to.

blacklist

A list of patterns matching URLs of pages that the script should not be applied to.

Return Value

If the style sheet was successfully added, a generated URL that can be used to remove it; otherwise, null.

Discussion

The style sheet behaves like a user-level style sheet. It is loaded after user-level style sheets, so it can override them, as well as overriding page-level style sheets.

Pages that are already loaded are updated to use the style sheet after it is added.

Availability
  • Available in Safari 5.0 and later.

addContentStyleSheetFromURL

Adds a content style sheet from a URL.

DOMString addContentStyleSheetFromURL (in DOMString url, in array whitelist, in array blacklist);

Parameters
url

The URL of the style sheet.

whitelist

A list of patterns matching URLs of pages that the script should be applied to.

blacklist

A list of patterns matching URLs of pages that the script should not be applied to.

Return Value

If the style sheet was successfully added, the supplied URL; otherwise, null.

Discussion

The style sheet behaves like a user-level style sheet. It is loaded after user-level style sheets, so it can override them, as well as overriding page-level style sheets.

Pages that are already loaded are updated to use the style sheet after it is added.

Availability
  • Available in Safari 5.0 and later.

removeContentScript

Removes the specified content script.

void removeContentScript (in DOMString url);

Parameters
url

The URL of the script being removed.

Discussion

Content scripts specified in the Info.plist are removed only for the current browser session.

Availability
  • Available in Safari 5.0 and later.

removeContentScripts

Removes all content scripts added by this extension.

void removeContentScripts (void);

Discussion

Content scripts specified in the Info.plist are removed only for the current browser session.

Availability
  • Available in Safari 5.0 and later.

removeContentStyleSheet

Removes the specified content style sheet.

void removeContentStyleSheet (in DOMString url);

Parameters
url

The URL of the style sheet being removed.

Discussion

Content style sheets specified in the Info.plist are removed only for the current browser session.

Availability
  • Available in Safari 5.0 and later.

removeContentStyleSheets

Removes all content style sheets added by this extension.

void removeContentStyleSheets (void);

Discussion

Content style sheets specified in the Info.plist are removed only for the current browser session.

Availability
  • Available in Safari 5.0 and later.



Last updated: 2010-08-03

Did this document help you? Yes It's good, but... Not helpful...