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/picture-inserter-content-script.md
+30-17Lines changed: 30 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,7 @@ ms.date: 01/15/2025
10
10
---
11
11
# Sample: Picture inserter using content script
12
12
<!--
13
-
length limit on sample name: in manifest:
14
-
"name": "Picture inserter using content script",
15
-
longer name would get truncated in the Extensions pop-up:
13
+
longer name would get truncated in Extensions pop-up:
16
14
"name": "Picture inserter using a content scr...",
17
15
-->
18
16
@@ -36,7 +34,7 @@ This sample demonstrates the following extension features:
36
34
37
35
You'll install the extension sample by using the browser's **Manage Extensions** tab, click the **Extensions** () button to show the list of installed extensions, and then click this sample extension:
38
36
39
-
<!-- todo: update -->
37
+

40
38
41
39
The extension displays a small HTML webpage in a pop-up, containing a title, an **Insert picture** button, and instructions:
42
40
@@ -253,7 +251,6 @@ This sample includes a content script that's injected into the webpage that's lo
## The content script message listener (`content.js`)
306
302
307
303
Here's the `content-scripts\content.js` file that gets injected into every browser tab page. This file is listed in the `content-scripts` section in `manifest.json`.
sendResponse({ fromcontent:"This message is from content.js" });
324
320
});
325
321
```
326
-
<!-- updated from sample -->
327
322
328
-
This code registers a `listener` by using the `chrome.runtime.onMessage.addListener` Extension API method. This listener waits for messages like the one you sent from the `popup.js`described earlier with the `chrome.tabs.sendMessage` Extension API method.
323
+
`content.js`registers a listener by using the `chrome.runtime.onMessage.addListener` Extension API method. This listener waits for the message that's sent when `popup.js`calls `chrome.tabs.sendMessage`.
329
324
330
-
The first parameter of the `addListener` method is a function whose first parameter, `request`, is the details of the message being passed in.
325
+
In `content.js`, the `addListener` method takes a single parameter that's a function. That function's first parameter, `request`, contains the details of the message that's being passed in.
331
326
332
-
In `popup.js`, for the `sendMessage` method call, the attributes of the second parameter are `url`, `imageDivId`, and `tabId`.
327
+
In `content.js`, when an event is processed by the listener, the listener function that's passed into `addListener` is run. The first parameter of the passed-in listener function is a `request` object that has attributes as assigned by `sendMessage`.
333
328
334
-
<!-- todo: update this text to match the latest code -->
335
-
In `content.js`, when an event is processed by the listener, the function<!-- todo: name of function --> that is the first parameter<!-- todo: name of param --> is run. The first parameter of that function is an object that has attributes as assigned by `sendMessage`. That function processes the three<!-- todo: update --> script lines.
329
+
In `popup.js`, for the `chrome.tabs.sendMessage` method call, the attributes of the second parameter for `sendMessage` are `url`, `imageDivId`, and `tabId`.
336
330
337
-
* The first script line appends an `img` element right below the `body` of the browser tab that has the `slide-image` class assigned as well as the `imageDivId` as the ID of that image element.<!-- todo: update this text to match the latest code -->
331
+
Here's the isolated listener function that's passed into `addListener`:
338
332
339
-
* The second script line dynamically inserts into the DOM header a **\<style\>** section that's assigned as a `slide-image` class to the `img` element.<!-- todo: update this text to match the latest code -->
333
+
`content.js` (portion)
340
334
341
-
* The third script line adds a `click` event that covers the entire image allowing the user to select anywhere on the image and that image is removed from the page (along with it is event listener).<!-- todo: update this text to match the latest code -->
335
+
```javascript
336
+
function(request, sender, sendResponse) {
337
+
constimg=document.createElement("img");
338
+
img.id=request.imageDivId;
339
+
img.src=request.url;
340
+
img.style="height: auto; width: 90vw;";
341
+
document.body.prepend(img);
342
+
343
+
img.addEventListener("click", () => {
344
+
img.remove();
345
+
}, { once:true });
346
+
347
+
sendResponse({ fromcontent:"This message is from content.js" });
348
+
}
349
+
```
350
+
351
+
The first five lines in the listener function append an `img` element immediately below the `body` element in the browser tab.
352
+
353
+
The second line in the listener function, `img.id = request.imageDivId;`, sets the ID of the `img` element to the `imageDivId` of the passed-in request.
354
+
355
+
In the listener function, the `addEventListener` call adds a `click` event listener function that covers the entire image, allowing the user to click anywhere on the image. When you click the inserted image, the image is removed from the current webpage by the line `img.remove();`, and the event listener is also removed, by specifying `{ once: true }`.
@@ -195,7 +194,6 @@ Every extension package must have a `manifest.json` file at the root. The manif
195
194
The `/icons/` directory contains the icon image files. The icons are used as the background image for the button that you click to launch the extension:
196
195
197
196

198
-
<!-- ~~ -->
199
197
200
198
When the extension is running, one of the icons is displayed on the toolbar, next to the Address bar:
201
199
@@ -228,8 +226,6 @@ Recommendations for icons:
228
226
</body>
229
227
</html>
230
228
```
231
-
<!-- updated from sample -->
232
-
233
229
234
230
The pop-up webpage (`popup.html`) is registered as the `"default_popup"` in `manifest.json`, in the `action` key section:
0 commit comments