-
Notifications
You must be signed in to change notification settings - Fork 2.7k
MDN get started tutorial examples to manifest V3 #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
da2172c
8b43ca9
325f8cb
0892301
313fb9a
e3206f1
4a5d71e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,41 @@ | ||
| # beastify | ||
|
|
||
| This extension used a tootlbar button to enable the section of beast that replaces the content of the active web page. | ||
|
|
||
| ## What it does ## | ||
|
|
||
| The extension includes: | ||
|
|
||
| * a browser action with a popup including HTML, CSS, and JS | ||
| * a content script | ||
| * three images, each of a different beast, packaged as web accessible resources | ||
| * An action with a popup that includes HTML, CSS, and JavaScript. | ||
| * A content script. | ||
| * Three images, each of a beast, packaged as web accessible resources. | ||
|
|
||
| When the user clicks the browser action button, the popup is shown, enabling | ||
| the user to choose one of three beasts. | ||
| When the user clicks the action (toolbar button), the popup displays, enabling the user to choose one of three beasts. | ||
|
|
||
| When it is shown, the popup injects a content script into the current page. | ||
| When it's displayed, the popup injects a content script into the active page. | ||
rebloor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| When the user chooses a beast, the extension sends the content script a message containing | ||
| the name of the chosen beast. | ||
| When the user chooses a beast, the extension sends the content script a message containing the name of the chosen beast. | ||
|
|
||
| When the content script receives this message, it replaces the current page | ||
| content with an image of the chosen beast. | ||
| When the content script receives this message, it replaces the active page content with an image of the chosen beast. | ||
|
|
||
| When the user clicks the reset button, the page reloads, and reverts to its original form. | ||
| When the user clicks the reset button, the page reloads and reverts to its original form. | ||
|
|
||
| Note that: | ||
|
|
||
| * if the user reloads the tab, or switches tabs, while the popup is open, then the popup won't be able to beastify the page any more (because the content script was injected into the original tab). | ||
| * If the user reloads the tab, or switches tabs, while the popup is open, then the popup can't beastify the page (because the content script was injected into the original tab). | ||
|
|
||
| * by default [`tabs.executeScript()`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript) injects the script only when the web page and its resources have finished loading. This means that clicks in the popup will have no effect until the page has finished loading. | ||
| * By default, [`scripting.executeScript()`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/scripting/executeScript) injects the script only when the web page and its resources have finished loading. This means that clicks in the popup have no effect until the page has finished loading. | ||
|
|
||
| * it's not possible to inject content scripts into certain pages, including privileged browser pages like "about:debugging" and the [addons.mozilla.org](https://addons.mozilla.org/) website. If the user clicks the beastify icon when such a page is loaded into the active tab, the popup displays an error message. | ||
| * It isn't possible to inject content scripts into certain pages, including privileged browser pages such as "about:debugging" and the [addons.mozilla.org](https://addons.mozilla.org/) website. If the user clicks the beastify icon on one of these pages, the popup displays an error message. | ||
rebloor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## What it shows ## | ||
|
|
||
| * write a browser action with a popup | ||
| * how to have different browser_action images based upon the theme | ||
| * give the popup style and behavior using CSS and JS | ||
| * inject a content script programmatically using `tabs.executeScript()` | ||
| * send a message from the main extension to a content script | ||
| * use web accessible resources to enable web pages to load packaged content | ||
| * reload web pages | ||
| In this eample, you see how to: | ||
|
|
||
| * Write an action (toolbar button) with a popup. | ||
| * Display action (toolbar button) icons based on the browser theme. | ||
| * Give a popup style and behavior using CSS and JavaScript. | ||
| * Inject a content script programmatically using `scripting.executeScript()`. | ||
| * Send a message from the main extension to a content script. | ||
| * Use web accessible resources to enable web pages to load packaged content. | ||
| * Reload web pages. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| { | ||
|
|
||
| "description": "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. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#beastify", | ||
| "manifest_version": 2, | ||
| "manifest_version": 3, | ||
| "name": "Beastify", | ||
| "version": "1.0", | ||
| "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/beastify", | ||
|
|
@@ -10,10 +10,20 @@ | |
| }, | ||
|
|
||
| "permissions": [ | ||
| "activeTab" | ||
| "activeTab", | ||
| "scripting" | ||
| ], | ||
|
|
||
| "browser_action": { | ||
| "browser_specific_settings": { | ||
| "gecko": { | ||
| "id": "[email protected]", | ||
| "data_collection_permissions": { | ||
| "required": ["none"] | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| "action": { | ||
| "default_icon": "icons/beasts-32.png", | ||
| "theme_icons": [{ | ||
| "light": "icons/beasts-32-light.png", | ||
|
|
@@ -25,7 +35,10 @@ | |
| }, | ||
|
|
||
| "web_accessible_resources": [ | ||
| "beasts/*.jpg" | ||
| { | ||
| "resources": [ "beasts/*.jpg" ], | ||
| "matches": [ "*://*/*" ] | ||
| } | ||
| ] | ||
|
|
||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revise to use async/await instead of Promise chains. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * CSS to hide everything on the page, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * except for elements that have the "beastify-image" class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * except for elements that have the ".beastify-image" class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hidePage = `body > :not(.beastify-image) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display: none; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rebloor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -12,9 +12,8 @@ const hidePage = `body > :not(.beastify-image) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function listenForClicks() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.addEventListener("click", (e) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Given the name of a beast, get the URL to the corresponding image. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Given the name of a beast, get the URL for the corresponding image. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function beastNameToURL(beastName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (beastName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,33 +28,45 @@ function listenForClicks() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Insert the page-hiding CSS into the active tab, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * then get the beast URL and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * get the beast URL, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * send a "beastify" message to the content script in the active tab. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function beastify(tabs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function beastify(tabs) { | |
| async function beastify(tabs) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revise the function to use async/await.
Warning
This function now takes a single Tab rather than an array of Tabs. Call sites must be updated.
| function beastify(tabs) { | |
| browser.tabs.insertCSS({code: hidePage}).then(() => { | |
| const url = beastNameToURL(e.target.textContent); | |
| browser.tabs.sendMessage(tabs[0].id, { | |
| command: "beastify", | |
| beastURL: url | |
| const tabId = tabs[0].id; | |
| browser.scripting | |
| .insertCSS({ | |
| target: { tabId }, | |
| css: hidePage, | |
| }) | |
| .then(() => { | |
| const url = beastNameToURL(e.target.textContent); | |
| browser.tabs.sendMessage(tabId, { | |
| command: "beastify", | |
| beastURL: url, | |
| }); | |
| }); | |
| }); | |
| } | |
| async function beastify(tab) { | |
| await browser.scripting.insertCSS({ | |
| target: { tabId: tab.id }, | |
| css: hidePage, | |
| }); | |
| const url = beastNameToURL(e.target.textContent); | |
| browser.tabs.sendMessage(tabId, { | |
| command: "beastify", | |
| beastURL: url, | |
| }); | |
| } |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revise the function to use async/await.
Warning
This function has also been revised to expect a single tab rather than an array of tabs. As such, call sites will also need to be updated.
| function reset(tabs) { | |
| browser.tabs.removeCSS({code: hidePage}).then(() => { | |
| browser.tabs.sendMessage(tabs[0].id, { | |
| command: "reset", | |
| const tabId = tabs[0].id; | |
| browser.scripting | |
| .removeCSS({ | |
| target: { tabId }, | |
| css: hidePage, | |
| }) | |
| .then(() => { | |
| browser.tabs.sendMessage(tabId, { | |
| command: "reset", | |
| }); | |
| }); | |
| }); | |
| } | |
| async function reset(tab) { | |
| await browser.scripting.removeCSS({ | |
| target: { tabId: tab.id }, | |
| css: hidePage, | |
| }); | |
| browser.tabs.sendMessage(tabId, { command: "reset" }); | |
| } |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revise the if block to use async/await conventions.
Since the query for the current tab is common to both blocks, we can move it above the if statements. Similarly, since the reportError error handler is common across both branches of the if statement, we can move it into a catch that wraps the entire if/else block.
| if (e.target.type === "reset") { | |
| browser.tabs.query({active: true, currentWindow: true}) | |
| browser.tabs | |
| .query({ active: true, currentWindow: true }) | |
| .then(reset) | |
| .catch(reportError); | |
| } else { | |
| browser.tabs.query({active: true, currentWindow: true}) | |
| browser.tabs | |
| .query({ active: true, currentWindow: true }) | |
| .then(beastify) | |
| .catch(reportError); | |
| } | |
| const [tab] = await browser.tabs.query({ active: true, currentWindow: true }); | |
| try { | |
| if (e.target.type === "reset") { | |
| await reset(tab); | |
| } else { | |
| await beastify(tab); | |
| } | |
| } catch (error) { | |
| reportError(error); | |
| } |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| browser.tabs | |
| .query({ active: true, currentWindow: true }) | |
| .then(([tab]) => | |
| browser.scripting.executeScript({ | |
| target: { tabId: tab.id }, | |
| files: ["/content_scripts/beastify.js"], | |
| }) | |
| ) | |
| .then(listenForClicks) | |
| .catch(reportExecuteScriptError); | |
| (function runOnPopupOpened() { | |
| try { | |
| const [tab] = browser.tabs.query({ active: true, currentWindow: true }); | |
| await browser.scripting.executeScript({ | |
| target: { tabId: tab.id }, | |
| files: ["/content_scripts/beastify.js"], | |
| }); | |
| listenForClicks(); | |
| } catch(e) { | |
| reportExecuteScriptError(e); | |
| } | |
| })(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| # borderify | ||
|
|
||
| **This add-on injects JavaScript into web pages. The `addons.mozilla.org` domain disallows this operation, so this add-on will not work properly when it's run on pages in the `addons.mozilla.org` domain.** | ||
| This add-on injects JavaScript into selected web pages. | ||
rebloor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## What it does | ||
|
|
||
| This extension just includes: | ||
| This extension includes a content script, "borderify.js", that is injected into any pages | ||
| under "mozilla.org/" or any of its subdomains. | ||
|
|
||
| * a content script, "borderify.js", that is injected into any pages | ||
| under "mozilla.org/" or any of its subdomains | ||
| **The `addons.mozilla.org` domain doesn't allow scripts to be injected into its pages. Therefore, this extension doesn't work on pages in the `addons.mozilla.org` domain.** | ||
|
|
||
| The content script draws a border around the document.body. | ||
|
|
||
| ## What it shows | ||
|
|
||
| * how to inject content scripts declaratively using manifest.json | ||
| From this example, you see how to inject content scripts declaratively using manifest.json. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| /* | ||
| Just draw a border round the document.body. | ||
| Draw a border round the document.body. | ||
| */ | ||
| document.body.style.border = "5px solid red"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| { | ||
|
|
||
| "description": "Adds a solid red border to all webpages matching mozilla.org. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#borderify", | ||
| "manifest_version": 2, | ||
| "manifest_version": 3, | ||
| "name": "Borderify", | ||
| "version": "1.0", | ||
| "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/borderify", | ||
| "icons": { | ||
| "48": "icons/border-48.png" | ||
| }, | ||
|
|
||
| "browser_specific_settings": { | ||
| "gecko": { | ||
| "id": "[email protected]", | ||
| "data_collection_permissions": { | ||
| "required": ["none"] | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| "content_scripts": [ | ||
| { | ||
| "matches": ["*://*.mozilla.org/*"], | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.