Skip to content

Commit b091a40

Browse files
committed
update code desc
1 parent 624d101 commit b091a40

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

microsoft-edge/extensions-chromium/getting-started/picture-inserter-content-script.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ ms.date: 01/15/2025
1010
---
1111
# Sample: Picture inserter using content script
1212
<!--
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:
1614
"name": "Picture inserter using a content scr...",
1715
-->
1816

@@ -36,7 +34,7 @@ This sample demonstrates the following extension features:
3634

3735
You'll install the extension sample by using the browser's **Manage Extensions** tab, click the **Extensions** (![Extensions icon](./picture-inserter-content-script-images/extensions-icon.png)) button to show the list of installed extensions, and then click this sample extension:
3836

39-
![Clicking the extension's icon to open the extension](./picture-inserter-content-script-images/open-the-extension.png)<!-- todo: update -->
37+
![Clicking the extension's icon to open the extension](./picture-inserter-content-script-images/open-the-extension.png)
4038

4139
The extension displays a small HTML webpage in a pop-up, containing a title, an **Insert picture** button, and instructions:
4240

@@ -253,7 +251,6 @@ This sample includes a content script that's injected into the webpage that's lo
253251
</body>
254252
</html>
255253
```
256-
<!-- updated from sample -->
257254

258255

259256
<!-- ====================================================================== -->
@@ -298,15 +295,14 @@ if (sendMessageId) {
298295
};
299296
}
300297
```
301-
<!-- updated from sample -->
302298

303299

304300
<!-- ====================================================================== -->
305301
## The content script message listener (`content.js`)
306302

307303
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`.
308304

309-
`content.js`:
305+
`content.js` (complete):
310306

311307
```javascript
312308
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
@@ -323,22 +319,40 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
323319
sendResponse({ fromcontent: "This message is from content.js" });
324320
});
325321
```
326-
<!-- updated from sample -->
327322

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`.
329324

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.
331326

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`.
333328

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`.
336330

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`:
338332

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)
340334

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+
const img = 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 }`.
342356

343357

344358
<!-- ====================================================================== -->
@@ -404,7 +418,6 @@ The sample creates and inject the content page that's embedded on the current ac
404418
]
405419
}
406420
```
407-
<!-- updated from sample -->
408421

409422

410423
<!-- ====================================================================== -->

microsoft-edge/extensions-chromium/getting-started/picture-viewer-popup-webpage.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ Every extension package must have a `manifest.json` file at the root. The manif
186186
}
187187
}
188188
```
189-
<!-- updated from sample -->
190189

191190

192191
<!-- ====================================================================== -->
@@ -195,7 +194,6 @@ Every extension package must have a `manifest.json` file at the root. The manif
195194
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:
196195

197196
![The extension's icon in the Extensions pop-up list](./picture-viewer-popup-webpage-images/extensions-popup-with-launch-icon.png)
198-
<!-- ~~ -->
199197

200198
When the extension is running, one of the icons is displayed on the toolbar, next to the Address bar:
201199

@@ -228,8 +226,6 @@ Recommendations for icons:
228226
</body>
229227
</html>
230228
```
231-
<!-- updated from sample -->
232-
233229

234230
The pop-up webpage (`popup.html`) is registered as the `"default_popup"` in `manifest.json`, in the `action` key section:
235231

0 commit comments

Comments
 (0)