-
Notifications
You must be signed in to change notification settings - Fork 550
Update extension tutorial to use Manifest V3 #2014
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
Changes from all commits
cb1dda0
ad716f6
fdc6c28
b7fe1e3
ed86ec3
dd549aa
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 |
|---|---|---|
|
|
@@ -5,11 +5,11 @@ author: MSEdgeTeam | |
| ms.author: msedgedevrel | ||
| ms.topic: conceptual | ||
| ms.prod: microsoft-edge | ||
| ms.date: 01/07/2021 | ||
| ms.date: 06/16/2022 | ||
| --- | ||
| # Create an extension tutorial, part 2 | ||
|
|
||
| [Completed extension package source for this part](https://github.com/MicrosoftEdge/MicrosoftEdge-Extensions-Demos/tree/master/extension-getting-started-part2/extension-getting-started-part2)<!-- changing master to main doesn't work 5/19/2022 --> | ||
| To see the completed extension package source for this part of the tutorial, go to [MicrosoftEdge-Extensions-Demos repo > extension-getting-started-part2](https://github.com/MicrosoftEdge/MicrosoftEdge-Extensions-Demos/tree/master/extension-getting-started-part2/extension-getting-started-part2).<!-- changing master to main doesn't work 5/19/2022 --> The source code has been updated from Manifest V2 to Manifest V3. | ||
mikehoffms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| <!-- ====================================================================== --> | ||
|
|
@@ -85,6 +85,8 @@ In that message, you must include the URL to the image you want to display. Als | |
|
|
||
| The following code outlines the updated code in `popup/popup.js`. You also pass in the current tab ID, which is used later in this article. | ||
|
|
||
| #### [Manifest V2](#tab/v2) | ||
|
|
||
| ```javascript | ||
| const sendMessageId = document.getElementById("sendmessageid"); | ||
| if (sendMessageId) { | ||
|
|
@@ -112,7 +114,38 @@ if (sendMessageId) { | |
| } | ||
| ``` | ||
|
|
||
| 4. Make your stars.jpeg available from any browser tab | ||
| #### [Manifest V3](#tab/v3) | ||
|
|
||
| ```javascript | ||
| const sendMessageId = document.getElementById("sendmessageid"); | ||
| if (sendMessageId) { | ||
| sendMessageId.onclick = function() { | ||
| chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { | ||
| chrome.tabs.sendMessage( | ||
| tabs[0].id, | ||
| { | ||
| url: chrome.runtime.getURL("images/stars.jpeg"), | ||
| imageDivId: `${guidGenerator()}`, | ||
| tabId: tabs[0].id | ||
| }, | ||
| function(response) { | ||
| window.close(); | ||
| } | ||
| ); | ||
| function guidGenerator() { | ||
| const S4 = function () { | ||
| return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); | ||
| }; | ||
| return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()); | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| 4. Make your `stars.jpeg` available from any browser tab | ||
|
|
||
| You're probably wondering why, when you pass the `images/stars.jpeg` must you use the `chrome.extension.getURL` chrome Extension API instead of just passing in the relative URL without the extra prefix like in the previous section. By the way, that extra prefix, returned by `getUrl` with the image attached looks something like the following. | ||
|
Collaborator
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. This V2-specific word|sentence|paragraph needs techreview in future PR. chrome.extension.getURL is V2-specific. @ashishnirvana1 @eclipse439 |
||
|
|
||
|
|
@@ -124,18 +157,35 @@ The reason is that you're injecting the image using the `src` attribute of the ` | |
|
|
||
| Add another entry in the `manifest.json` file to declare that the image is available to all browser tabs. That entry is as follows (you should see it in the full `manifest.json` file below when you add the content script declaration coming up). | ||
|
|
||
| #### [Manifest V2](#tab/v2) | ||
|
|
||
| ```json | ||
| "web_accessible_resources": [ | ||
| "images/*.jpeg" | ||
| ] | ||
| ``` | ||
|
|
||
| #### [Manifest V3](#tab/v3) | ||
|
|
||
| ```json | ||
| "web_accessible_resources": [ | ||
| { | ||
| "resources": ["images/*.jpeg"], | ||
| "matches": ["<all_urls>"] | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| You've now written the code in your `popup.js` file to send a message to the content page that is embedded on the current active tab page, but you haven't created and injected that content page. Do that now. | ||
|
|
||
| 5. Update your manifest.json for content and web access | ||
| 5. Update your `manifest.json` for content and web access | ||
|
|
||
| The updated `manifest.json` that includes the `content-scripts` and `web_accessible_resources` is as follows. | ||
|
|
||
| #### [Manifest V2](#tab/v2) | ||
|
|
||
| ```json | ||
| { | ||
| "name": "NASA picture of the day viewer", | ||
|
|
@@ -165,6 +215,42 @@ The updated `manifest.json` that includes the `content-scripts` and `web_accessi | |
| } | ||
| ``` | ||
|
|
||
| #### [Manifest V3](#tab/v3) | ||
|
|
||
| ```json | ||
| { | ||
| "name": "NASA picture of the day viewer", | ||
| "version": "0.0.0.1", | ||
| "manifest_version": 3, | ||
| "description": "An extension to display the NASA picture of the day.", | ||
| "icons": { | ||
| "16": "icons/nasapod16x16.png", | ||
| "32": "icons/nasapod32x32.png", | ||
| "48": "icons/nasapod48x48.png", | ||
| "128": "icons/nasapod128x128.png" | ||
| }, | ||
| "action": { | ||
| "default_popup": "popup/popup.html" | ||
| }, | ||
| "content_scripts": [ | ||
| { | ||
| "matches": [ | ||
| "<all_urls>" | ||
| ], | ||
| "js": ["lib/jquery.min.js","content-scripts/content.js"] | ||
| } | ||
| ], | ||
| "web_accessible_resources": [ | ||
| { | ||
| "resources": ["images/*.jpeg"], | ||
| "matches": ["<all_urls>"] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| The section you added is `content_scripts`. The `matches` attribute is set to `<all_urls>`, which means that all files in `content_scripts` are injected into all browser tab pages when each tab is loaded. The allowed files types that can be injected are JavaScript and CSS. You also added `lib\jquery.min.js`. You're able to include that from the download mentioned at the top of the section. | ||
|
|
||
| 6. Add jQuery and understanding the associated thread | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.