拡張機能の例
拡張機能の開発方法を分かりやすく説明するため、シンプルな拡張機能のサンプルを集めたリポジトリ https://github.com/mdn/webextensions-examples を公開しています。この記事では、そのリポジトリで使われている WebExtension API について説明します。
これらのサンプルは Firefox Nightly で動作します。ほとんどのものがより早期の Firefox でも動作しますが、拡張機能の manifest.json に指定されている strict_min_version キーを確認してください。
警告: いくつかの例では、特定のドメインまたはページでのみ動作します。制限事項の詳細は、各サンプルの readme ファイルに記載されています。どの例も既定ではプライベートブラウジングウィンドウでは動作しません。詳細は「プライベートブラウジングでの拡張機能」をご覧ください。
これらの例を試してみる場合は、リポジトリをクローンしてから下記のようにしてください。
- 一時的なアドオンを読み込む (Load Temporary Add-on) 機能を使用する。拡張機能は Firefox を再起動するまで読み込まれたままになります。
- コマンドラインで拡張機能のソースフォルダーを開き、 web-ext を使用して拡張機能を実行する。拡張機能は Firefox を再起動するまで読み込まれたままになります。
- Firefox で ファイル > ファイルを開く を使用し、 build フォルダーにある例を探す。
buildフォルダーにはビルドされ署名されたバージョンのすべての例が置いてあります。これは例を永続的にインストールします。
警告: この例の WebExtension を addons.mozilla.org (AMO) に投稿しないでください、WebExtension の例を実行するのに署名をする必要はありません。単に上記の手順に従ってください。
リポジトリに貢献したい方は、是非とも pull request を送ってください!
| Name | Description | JavaScript APIs |
|---|---|---|
| annotate-page | Displays a sidebar that lets you take notes on web pages. | storage.localtabs.onActivatedtabs.onUpdatedtabs.querytabs.Tabwindows.getCurrentwindows.Window |
| apply-css | Adds a page action to the toolbar. Click the button to apply a red border using injected CSS. Click the button again to remove the CSS. | pageAction.getTitlepageAction.onClickedpageAction.setIconpageAction.setTitlepageAction.showtabs.insertCSStabs.onUpdatedtabs.querytabs.removeCSStabs.Tab |
| beastify | Adds a browser action icon to the toolbar. Click the button to choose a beast. The active tab's body content is then replaced with a picture of the chosen beast. | extension.getURLruntime.onMessagetabs.executeScripttabs.insertCSStabs.querytabs.removeCSStabs.sendMessagetabs.Tab |
| bookmark-it | Adds a bookmark button to the toolbar. Click the button to toggle a bookmark for the current page. | bookmarks.createbookmarks.onCreatedbookmarks.onRemovedbookmarks.removebookmarks.searchbrowserAction.onClickedbrowserAction.setIconbrowserAction.setTitletabs.onActivatedtabs.onUpdatedtabs.querytabs.Tabwindows.onFocusChanged |
| borderify | Adds a solid red border to all webpages matching mozilla.org. | |
| chill-out | Show a page action after a period of inactivity. Show cat gifs when the page action is clicked. | alarms.clearAllalarms.createalarms.onAlarmpageAction.hidepageAction.onClickedpageAction.showtabs.gettabs.onActivatedtabs.onUpdatedtabs.querytabs.update |
| commands | Demonstrates using the commands API to set up a keyboard shortcut. The shortcut created is accessed using Ctrl+Shift+U (Command+Shift+U on a Mac). | commands.getAllcommands.onCommandcommands.resetcommands.updatetabs.create |
| content-script-register | Illustrates how an extension can register URL-matching content scripts at runtime. | contentScripts.registerruntime.onMessageruntime.sendMessage |
| context-menu-copy-link-with-types | Add a context menu option to links to copy the link to the clipboard, as plain text and as a link in rich HTML. | contextMenus.createcontextMenus.onClickedtabs.executeScript |
| contextual-identities | List, create, and remove contextual identities. | contextualIdentities.querytabs.createtabs.querytabs.remove |
| cookie-bg-picker | Allows the user to customize the background color and tiled pattern on sites they visit, and also saves their preferences via a cookie, reapplying them whenever they revisit a site they previously customized. | cookies.getcookies.onChangedcookies.removecookies.setextension.getURLruntime.onMessagetabs.onActivatedtabs.onUpdatedtabs.querytabs.Tabtabs.sendMessage |
| devtools-panels | Demonstrates some of the devtools APIs. | devtools.inspectedWindowdevtools.panelsruntime.getURLruntime.onMessageruntime.sendMessagetabs.executeScript |
| discogs-search | Demonstrates adding a custom search engine with the chrome_settings_overrides key. | |
| dnr-block-only | Demonstrates how to block network requests without host permissions using the declarativeNetRequest API with the `declarative_net_request` manifest key. | declarativeNetRequest.RuledeclarativeNetRequest.RuleActiondeclarativeNetRequest.RuleCondition |
| dnr-dynamic-with-options | Demonstrates a generic way to request host permissions and register declarativeNetRequest rules to modify network requests, without any install-time permission warnings. The options_ui page offers a way to request permissions and register DNR rules. | declarativeNetRequest.RuledeclarativeNetRequest.getDynamicRulesdeclarativeNetRequest.getSessionRulesdeclarativeNetRequest.updateDynamicRulesdeclarativeNetRequest.updateSessionRulespermissions.getAllpermissions.removepermissions.request |
| dnr-redirect-url | Demonstrates multiple ways to redirect requests using the declarativeNetRequest API through the `declarative_net_request` manifest key. Demonstrates aspects of Manifest Version 3 (MV3): action, host_permissions, and web_accessible_resources, and includes a comparison with Manifest Version 2 (MV2). | declarativeNetRequest.RedirectdeclarativeNetRequest.RuledeclarativeNetRequest.RuleActiondeclarativeNetRequest.RuleConditiondeclarativeNetRequest.URLTransformpermissions.containspermissions.removepermissions.request |
| dynamic-theme | Dynamic theme example | alarms.createalarms.onAlarmtheme.update |
| emoji-substitution | Replaces words with emojis. | |
| eslint-example | Demonstrates how to configure an extension with eslint. | |
| export-helpers | Demonstrates how to use export helpers like cloneInto to share objects with page scripts. | notifications.createruntime.onMessageruntime.sendMessage |
| favourite-colour | An example options page, letting you store your favourite colour. | browserAction.onClickedruntime.openOptionsPagestorage.managedstorage.sync |
| find-across-tabs | Demonstration of the find API. | browserAction.onClickedextension.getBackgroundPagefind.findfind.highlightResultsruntime.getURLruntime.onMessageruntime.sendMessagetabs.createtabs.querytabs.Tab |
| firefox-code-search | Demonstrates how to use the omnibox API. | omnibox.onInputChangedomnibox.onInputEnteredomnibox.setDefaultSuggestiontabs.createtabs.update |
| forget-it | Demonstrates how to use the browsingData API. | browserAction.onClickedbrowsingData.removenotifications.createstorage.local |
| google-userinfo | Demonstrates how to use the identity API. | browserAction.onClickedidentity.getRedirectURLidentity.launchWebAuthFlownotifications.create |
| history-deleter | History API demo: deletes history items for a given domain | history.deleteUrlhistory.searchpageAction.showtabs.onUpdatedtabs.query |
| http-response | Demonstrates how to rewrite HTTP responses using the webRequest.filterResponseData() API. | webRequest.filterResponseDatawebRequest.onBeforeRequest |
| imagify | Using a sidebar, illustrates the use of file picker and drag and drop. A content script replaces the current page content with the chosen image. | extension.getURLruntime.onMessagetabs.executeScripttabs.querytabs.sendMessage |
| latest-download | Shows the last downloaded item, and lets you open or delete it. | downloads.erasedownloads.getFileIcondownloads.opendownloads.removeFiledownloads.search |
| list-cookies | This extensions list the cookies in the active tab. | cookies.getAlltabs.query |
| menu-accesskey-visible | Shows how to set up a single letter access key for a menu item. | i18n.getMessagemenus.updatemenus.createmenus.onClicked |
| menu-demo | Demonstrates adding and manipulating menu items using the menus API. | i18n.getMessagemenus.createmenus.onClickedmenus.removemenus.updateruntime.lastErrortabs.executeScript |
| menu-labelled-open | Shows how an extension can listen for the display of a menu and then add, remove, or update its menu items. | menus.createmenus.onClickedmenus.onShownmenus.refreshmenus.updatetabs.update |
| menu-remove-element | Shows how to detect the page element at the cursor position and remove that element, or a parent element, from the page. This example includes use of the polyfill, illustrating cross-browser extension development. | menus.createmenus.onClickedmenus.getTargetElementpageAction.openPopuppageAction.showtabs.executeScript |
| menu-search | Illustrates how to retrieve a list of search engines and issue a search request, using search engine details added to the context menu for selected text. | search.searchsearch.getmenus.createmenus.onClicked |
| mocha-client-tests | This example shows two methods of testing an extension: running tests from within the extension, and running tests from the command line using Karma. | runtime.onMessageruntime.sendMessage |
| native-messaging | Example of native messaging, including a Python application and an extension which exchanges messages with it. | browserAction.onClickedruntime.connectNative |
| navigation-stats | Demonstration of the webNavigation API, showing basic stats about which pages you've visited. | storage.localwebNavigation.onCommittedwebNavigation.onCompleted |
| notify-link-clicks-i18n | Shows a localized notification when the user clicks on links. | extension.getURLi18n.getMessagenotifications.createruntime.onMessageruntime.sendMessage |
| open-my-page-button | Adds a browser action icon to the toolbar. When the browser action is clicked, the add-on opens a page that was packaged with it. | browserAction.onClickedtabs.create |
| page-to-extension-messaging | Demonstrates how a web page and a content script can exchange messages. Visit https://mdn.github.io/webextensions-examples/content-script-page-script-messaging.html for the demo. | |
| permissions | Demonstrates optional permissions using the permissions API. | browserAction.onClickedpermissions.getAllpermissions.removepermissions.requestruntime.getURLtabs.create |
| private-browsing-theme | Example dynamic theme: sets a dark theme for private windows. | theme.resettheme.updatewindows.getAllwindows.onCreated |
| proxy-blocker | Uses the proxy API to block requests to hosts specified on a list. | extension.getURLproxy.onRequestproxy.onErrorstorage.localstorage.onChanged |
| quicknote | Allows the user to make quick notes by clicking a button and entering text into the resulting popup. The notes are saved in storage. | storage.local |
| root-cert-stats | Shows how to get details about a request's TLS connection. | webRequest.getSecurityInfo |
| runtime-examples | Demo of various runtime APIs. | browserAction.onClickednotifications.createruntime.getManifestruntime.onInstalledruntime.reload |
| selection-to-clipboard | Demonstrates how to write to the clipboard from a content script | |
| session-state | Demonstrates how to retrieve extension-defined state from restored tabs. | menus.createmenus.onClickedsessions.getTabValuesessions.setTabValuetabs.insertCSStabs.onCreatedtabs.onUpdatedtabs.query |
| store-collected-images | Demonstrates how to use the idb-file-storage library to store and manipulate files in an extension. | browserAction.onClickedcontextMenus.createcontextMenus.onClickedruntime.onMessageruntime.sendMessagetabs.createwindows.create |
| stored-credentials | Performs basic authentication by supplying stored credentials. | storage.localwebRequest.onAuthRequiredwebRequest.onCompletedwebRequest.onErrorOccurred |
| tabs-tabs-tabs | Demonstrates tab manipulation: opening, closing, moving, zooming tabs. | browserAction.setBadgeBackgroundColorbrowserAction.setBadgeTexttabs.createtabs.duplicatetabs.getZoomtabs.movetabs.onCreatedtabs.onMovedtabs.onRemovedtabs.querytabs.reloadtabs.removetabs.setZoomtabs.Tabtabs.update |
| theme-integrated-sidebar | A sidebar that integrates with the current theme. | theme.getCurrenttheme.onUpdatedwindows.getCurrent |
| theme-switcher | An example of how to use the management API for themes. | management.getAllmanagement.setEnabled |
| themes | A collection of themes illustrating:
| |
| top-sites | Demonstration of the topSites API. | topSites.get |
| user-agent-rewriter | Demonstrates using the webRequest API to rewrite the User-Agent HTTP header. | extension.getBackgroundPagewebRequest.onBeforeSendHeaders |
| user-script-register | Illustrates how an extension can register URL-matching user scripts at runtime. | userScripts.registerruntime.onMessageruntime.sendMessage |
| webpack-modules | Demonstrates how to use webpack to package npm modules in an extension. | runtime.onMessageruntime.sendMessage |
| window-manipulator | Demonstrates how to manipulate windows: opening, closing, resizing windows. | windows.createwindows.getAllwindows.getCurrentwindows.removewindows.updatewindows.Window |
| open-irc-links | Demonstrates the use of protocol handlers. |