You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: microsoft-edge/extensions-chromium/getting-started/part2-content-scripts.md
+94-52Lines changed: 94 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
---
2
2
title: "Sample: Insert an image in the webpage"
3
-
description: Dynamically inserting a NASA picture below the page bodytag by using content scripts.
3
+
description: Uses JavaScript to insert a .png file below the <body> start-tag of the current webpage.
4
4
author: MSEdgeTeam
5
5
ms.author: msedgedevrel
6
6
ms.topic: conceptual
7
7
ms.service: microsoft-edge
8
8
ms.subservice: extensions
9
-
ms.date: 10/04/2024
9
+
ms.date: 12/03/2024
10
10
---
11
11
# Sample: Insert an image in the webpage
12
12
13
-
The [Part 2 sample: Use JavaScript to insert an image in the webpage](https://github.com/microsoft/MicrosoftEdge-Extensions/tree/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2)uses JavaScript code to insert the `stars.jpeg` image into the top of the currently opened webpage. The extension's pop-up contains a title and an HTML button that's labelled **Display**. When you click the **Display** button, JavaScript sends a message from the extension icon's pop-up, and dynamically inserts JavaScript running as content in the browser tab.
13
+
This sampleuses JavaScript code to insert the `stars.jpeg` image into the top of the current webpage, below the `<body>` start-tag. The extension's pop-up contains a title and an HTML button that's labelled **Display**. When you click the **Display** button, JavaScript sends a message from the extension icon's pop-up, and dynamically inserts JavaScript running as content in the browser tab.
14
14
15
-
The Part 2 sample uses the following extension technologies:
15
+
This sample uses the following extension technologies:
16
16
* Injecting JavaScript libraries into an extension.
17
17
* Exposing extension assets to browser tabs.
18
18
* Including content webpages in existing browser tabs.
19
19
* Having content webpages listen for messages from pop-ups and respond.
20
20
21
-
You'll open the Part 2 extension from the **Extensions** () button after installing the extension via the **Manage Extensions** tab:
21
+
As a preview of the sample: You'll open the Part 2 extension from the **Extensions** () button after installing the extension via the **Manage Extensions** tab:
22
22
23
23

24
24
@@ -30,32 +30,99 @@ When you click the **Display** button, JavaScript code temporarily inserts `star
30
30
31
31

32
32
33
-
If you want to immediately install and run the completed extension, or view its finished code, either:
34
-
* Clone the [MicrosoftEdge-Extensions](https://github.com/microsoft/MicrosoftEdge-Extensions) repo to your local drive. Use repo directory `/extension-getting-started-part2/`.
35
-
* Download the source code from the [/extension-getting-started-part2/](https://github.com/microsoft/MicrosoftEdge-Extensions/tree/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2) folder of the MicrosoftEdge-Extensions repo.
33
+
Obtain and run the sample as follows.
36
34
37
-
Then you can install and run the finished extension that's in the repo, per [Sideload an extension to install and test it locally](extension-sideloading.md). The tab must contain a webpage before opening the extension.
You can use various tools to clone a GitHub repo. You can download a selected directory, or clone the entire repo. These instructions use GitHub Desktop to clone the repo and switch to a working branch.
40
+
41
+
To clone the `MicrosoftEdge-Extensions` repo to your local drive:
42
+
43
+
1. If not done already, install GitHub desktop: go to [https://github.com/apps/desktop](https://github.com/apps/desktop), and then click the **Download now** button.
44
+
45
+
1. Go to [MicrosoftEdge-Extensions](https://github.com/microsoft/MicrosoftEdge-Extensions).
46
+
47
+
1. Click the **Code** button, and then select **Open with GitHub Desktop**.
48
+
49
+
A dialog opens, saying **This site is trying to open GitHubDesktop.exe.**
50
+
51
+
1. Click the **Open** button.
52
+
53
+
GitHub Desktop opens, with the **MicrosoftEdge-Extensions** repo selected in the upper left dropdown list.
54
+
55
+
1. In GitHub Desktop, click the **Branch** menu, and then click **New branch**.
56
+
57
+
The **Create a branch** dialog opens.
58
+
59
+
1. In the **Name** text box, enter a branch name, such as **ext-sample-1**, and then click the **Create branch** button.
60
+
61
+
In the upper middle and lower left of GitHub Desktop, the current branch is shown, such as **ext-sample-1**.
62
+
63
+
You are now free to modify the code in your working branch, without altering the code that's in the "main" branch of the repo.
64
+
65
+
<!-- See also: -->
66
+
<!-- * [Step 2: Create a branch](https://docs.github.com/en/get-started/start-your-journey/hello-world#step-2-create-a-branch) in _Hello World_. -->
Do the steps in [Sideload an extension to install and test it locally](extension-sideloading.md) to locally update the extension and then run the extension. The tab must contain a webpage before opening the extension.
82
+
83
+
A pop-up opens, containing a small HTML page with a title, instructions, and a **Display** button:
84
+
85
+

86
+
87
+
88
+
1. Install or update the extension, in the Manage Extensions page; see [Sideload an extension to install and test it locally](extension-sideloading.md).
89
+
90
+
1. Go to a webpage, such as [Microsoft.com](https://www.microsoft.com), in a new window or tab. The tab must not be empty, and must not be the Manage Extensions page.
91
+
92
+
1. Click the **Extensions** () button, next to the Address bar. Or, select **Settings and more** (...) > **Extensions**.
93
+
94
+

95
+
96
+
1. Click the icon or name of the **NASA picture of the day viewer** extension.
97
+
98
+
The extension's pop-up opens:
99
+
100
+

101
+
102
+
1. Click the **Display** button. `stars.jpeg` is inserted into the top of the current webpage in the current tab, pushing the webpage's content down below the image:
103
+
104
+

105
+
106
+
1. Click the `stars.jpeg` image that's filling the top of the webpage. That image element is removed from the DOM tree and webpage, and the current webpage is restored, shifting its content back up to the top of the tab.
107
+
108
+
The extension sends a message from the extension icon's pop-up, and dynamically inserts JavaScript running as content in the browser tab. The injected content sets the image element to display `stars.jpeg` in the top of the current webpage, and then removes the image when you click the image.
To use this article, first do the steps in [Sample: Display an image in a pop-up](./part1-simple-extension.md); that is, clone the MicrosoftEdge-Extensions repo, install the Part 1 demo, and run that demo.
44
114
45
-
This step has already been done in [popup.html](https://github.com/microsoft/MicrosoftEdge-Extensions/blob/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2/popup/popup.html) in the MicrosoftEdge-Extensions repo.
46
115
47
-
The hypothetical starting state for this Part 2 article, if you were to manually build-up the sample to add JavaScript, is that a directory exists for the part 2 code, containing the same directories and files that are created in the Part 1 article. These can be sibling directories for Part 1 and Part 2, as done in the MicrosoftEdge-Extensions repo.
It's possible to install the completed Part 1 and Part 2 demos from the MicrosoftEdge-Extensions repo, side-by-side. We recommend first cloning the repo and installing and running the demos, before (or instead of) starting with an empty directory and then manually creating directories, creating files, and pasting code into the files.
50
119
51
-
To manually expand the completed Part 1 demo to create the Part 2 demo, you would need to do the following:
52
-
1. In the folder that contains the `popup.html` file for Part 2 (initially a copy of the Part 1 file), add tagging that displays a title with a button.
53
-
1. Include a reference to a JavaScript file, `popup.js`.
## The default initial popup webpage (`popup.html`)
55
122
56
-
Below is the sample HTML file:
123
+
`popup.html` is specified in the manifest file as the initial, default webpage to display in the extension popup. This webpage contains a title, instructions, and a **Display** button.
57
124
58
-
`/popup/popup.html` (complete):
125
+
`/popup/popup.html`:<!-- (complete listing) -->
59
126
60
127
```html
61
128
<!DOCTYPE html>
@@ -85,12 +152,6 @@ Below is the sample HTML file:
85
152
</html>
86
153
```
87
154
88
-
Do the steps in [Sideload an extension to install and test it locally](extension-sideloading.md) to locally update the extension and then run the extension. The tab must contain a webpage before opening the extension.
89
-
90
-
After updating and opening the extension, a pop-up opens, containing a small HTML page with a title, instructions, and a **Display** button:
91
-
92
-

## Step 2: Update the webpage to insert the image at the top
@@ -234,11 +295,13 @@ The `matches` attribute is set to `<all_urls>`, which means that all files in `c
234
295
<!-- ------------------------------ -->
235
296
#### Add jQuery
236
297
237
-
This step has already been done in [jquery.min.js](https://github.com/microsoft/MicrosoftEdge-Extensions/blob/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2/lib/jquery.min.js) in the MicrosoftEdge-Extensions repo.
298
+
`jquery.min.js` is a predefined, minified file.
299
+
300
+
[jquery.min.js](https://github.com/microsoft/MicrosoftEdge-Extensions/blob/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2/lib/jquery.min.js) in the MicrosoftEdge-Extensions repo.
238
301
239
-
In the content scripts that you're injecting, plan on using jQuery (`$`). You added a minified version of jQuery and put it in your extension package as `lib\jquery.min.js`.
302
+
In the content scripts that you're injecting, it's common to use jQuery (`$`). This sample includes a minified version of jQuery, residing in the extension package<!-- todo: check 'package' --> as `lib\jquery.min.js`.
240
303
241
-
These content scripts run in individual sandboxes, which means that the jQuery injected into the `popup.js` page isn't shared with the content.
304
+
A content script runs in an individual sandbox, which means that the jQuery that's injected into the `popup.js` page isn't shared with the current webpage.
242
305
243
306
244
307
<!-- ------------------------------ -->
@@ -251,11 +314,11 @@ Even if the browser tab has JavaScript running on it on the loaded web page, any
## Step 6: Add the content script message listener
253
316
254
-
To continue manually building-up the Part 2 demo, you would need to create this file. This step has already been done in [content.js](https://github.com/microsoft/MicrosoftEdge-Extensions/blob/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2/content-scripts/content.js) in the MicrosoftEdge-Extensions repo.
317
+
[content.js](https://github.com/microsoft/MicrosoftEdge-Extensions/blob/main/Extension%20samples/extension-getting-started-part2/extension-getting-started-part2/content-scripts/content.js) in the MicrosoftEdge-Extensions repo.
255
318
256
319
Here's the `content-scripts\content.js` file that gets injected into every browser tab page based on the `content-scripts` section in `manifest.json`:
1. Install or update the extension, in the Manage Extensions page; see [Sideload an extension to install and test it locally](extension-sideloading.md).
298
-
299
-
1. Go to a webpage, such as [Microsoft.com](https://www.microsoft.com), in a new window or tab. The tab must not be empty, and must not be the Manage Extensions page.
300
-
301
-
1. Click the **Extensions** () button, next to the Address bar. Or, select **Settings and more** (...) > **Extensions**.
302
-
303
-

304
-
305
-
1. Click the icon or name of the **NASA picture of the day viewer** extension.
306
-
307
-
The extension's pop-up opens:
308
-
309
-

310
-
311
-
1. Click the **Display** button. `stars.jpeg` is inserted into the top of the current webpage in the current tab, pushing the webpage's content down below the image:
312
-
313
-

314
-
315
-
1. Click the `stars.jpeg` image that's filling the top of the webpage. That image element is removed from the DOM tree and webpage, and the current webpage is restored, shifting its content back up to the top of the tab.
316
-
317
-
Congratulations! You've created an extension that sends a message from the extension icon's pop-up, and dynamically inserts JavaScript running as content in the browser tab. The injected content sets the image element to display `stars.jpeg` in the top of the current webpage, and then removes the image when you click the image.
0 commit comments