# Web Highlighter Tutorial

{% hint style="info" %}
🍎 Before you begin this tutorial, make sure you've completed the [Developer Quick Start Guide](https://docs.pixiebrix.com/quick-start/mod-developer).
{% endhint %}

Have you ever wanted to **highlight** a word or phrase on a page so you can find it? Probably, and maybe you even wanted to find multiple keywords instead of just using the “find” search in your browser multiple times.

This tutorial will show you how to do that. We're going to use the tech news forum site [Hacker News](https://news.ycombinator.com/) as an example.

Let's assume we want to highlight every time someone mentions the word “**disclosure”** in a comment thread on Hacker News. *(Commenters often include the word “disclosure” when they work for the company that is the subject of a discussion. These commenters can have an insider perspective, or a unique bias.)*

For example:

![A commenter in Hacker News disclosed that they work at the company that’s a subject of the discussion:](https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/c0cf9e0e-a7e1-4992-b2b2-faaee0fe674d/Untitled/w=3840,quality=80)

### 1. Add a Trigger <a href="#block-886d6faeb73c47258aa562a60c3dd136" id="block-886d6faeb73c47258aa562a60c3dd136"></a>

#### Navigate to a Hacker News discussion <a href="#block-05b6fa0548e64fe0967b1476f23ed2c7" id="block-05b6fa0548e64fe0967b1476f23ed2c7"></a>

Start by navigating your browser to this HN discussion page: <https://news.ycombinator.com/item?id=30086553>

#### Open the Page Editor <a href="#block-dcfd530dda174a85b84f6524a32154a9" id="block-dcfd530dda174a85b84f6524a32154a9"></a>

Next, open the PixieBrix Page Editor.

{% hint style="info" %}
📘Need help opening the Page Editor? Follow the steps here: [open-the-page-editor](https://docs.pixiebrix.com/platform-overview/page-editor/open-the-page-editor "mention")
{% endhint %}

#### Enable PixieBrix on the Page <a href="#block-e7978a1dd1f9457d935421e09a011de5" id="block-e7978a1dd1f9457d935421e09a011de5"></a>

The first time you open the Page Editor on a new webpage, you need to grant PixieBrix access to the page.

Grant access to PixieBrix by clicking the Enable PixieBrix on [ycombinator.com](http://ycombinator.com/) button. Chrome will prompt you to accept access.

![image](https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/444546b4-b1fb-4cb7-ae07-3d3cb48da2f7/Untitled/w=750,quality=80)

#### Add a Page Load Trigger <a href="#block-afba1b4b517f40c780ac98cceacfbd00" id="block-afba1b4b517f40c780ac98cceacfbd00"></a>

Use the **New Mod** button on the top left of the Page Editor to add a Trigger.

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2F5f4zVfLFShiZVH2IDoY1%2FCleanShot%202024-10-16%20at%2009.26.20.png?alt=media&#x26;token=6eb99754-8815-4d1e-8415-2d7c43c87281" alt=""><figcaption></figcaption></figure>

The Trigger defaults to the Page Load trigger, which runs when the page first loads or whenever you navigate

* In the Brick Configuration Panel, set the field **Name** field to **`HN highlighter trigger`** or whatever you’d prefer to call it.
* Set the field **Sites** to **`https://news.ycombinator.com/item*`** By specifying a site, you ensure that the trigger only runs on the comment pages on the Hacker News website
  * The `*` in the Site match pattern accepts any text. You can use the `*` in the URL scheme, domains, and at the end of the path

When you’ve done the above steps, your Brick Configuration Panel should look like this:

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/56c56d35-cd0f-4f3a-b071-549145a5dbe1/Untitled/w=3840,quality=80" alt=""><figcaption></figcaption></figure>

## 2. Add and configure the Highlight brick <a href="#block-f43992adbfd4428186561afea69dc29f" id="block-f43992adbfd4428186561afea69dc29f"></a>

Click the **+** button below the trigger brick in the Mod Overview Panel to add a new brick.

In the Add Brick Modal, search for “highlight”, hover your mouse over the Highlight search result, and click **+ Add**

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/0b0c389a-b4ab-4331-b159-bae607dcd313/Untitled/w=1200,quality=80" alt=""><figcaption></figcaption></figure>

In the Brick Configuration Panel, we’ll now tell the Highlight brick which elements to highlight

Click on the entry mode toggle on the **elements field,** and select Array Items

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/1693b9db-cfe7-419c-a3a3-01be1ada4d6e/Untitled/w=1080,quality=80" alt="" width="375"><figcaption></figcaption></figure>

Next, click the blue **Add Item** button to add an array of item

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/73834aca-96a8-4034-8508-f2c3b6edf48d/Untitled/w=3840,quality=80" alt=""><figcaption></figcaption></figure>

Click the comment to select it and exit out of selection mode.

When you select an element, PixieBrix determines a unique selector for that element. However, for our use case, we want to highlight *all elements on the page* that contain the word disclosure

Therefore, we’ll enter our own selector that matches the text using a `contains` selector in the same elements field that was just populated. Provide the following value to the field.

Copy the following:

```
span:contains('Disclosure'), span:contains('DISCLOSURE'), span:contains('disclosure')
```

The `contains` selector is case-sensitive. Therefore, the above selector uses the multiple selector indicated by a comma `,` to match any of the typical spellings of the word “disclosure”

{% hint style="info" %}
*📖 Want to see a full list of selectors supported by PixieBrix? PixieBrix uses the popular jQuery library;* [*you can find the full list of supported selectors here*](https://api.jquery.com/category/selectors/)
{% endhint %}

The elements field should look like this.

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/4d47be09-6b80-441c-94b2-413c2a13b8bc/Untitled/w=3840,quality=80" alt=""><figcaption></figcaption></figure>

Next, we’ll scroll through the page to determine if Hacker News uses any other elements tags to show comments.

To add another element to the elements list to highlight, click the blue **Add Item** button again

Click the arrow button to enter selection mode in the new element row. If you mouse over comments on the page, you’ll see that some comments use the `div` tag.![image](https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/0fc5e39e-39d8-4502-9a9a-901a559f963b/Untitled/w=828,quality=80)

Click anywhere on the page to exit selection mode. As before, we’ll instead enter our own selector to match the keyword we’re highlighting:

Copy the following:

```
 div:contains('Disclosure'), div:contains('DISCLOSURE'), div:contains('disclosure')
```

One final setting to update! In the condition field, select the ABC option at the end of the row, and select the “X” to exclude the conditional criteria.

The Brick Configuration Panel should now look like this:<br>

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/42c7401e-3dba-483c-b4b9-613cde681f0b/Untitled/w=1080,quality=80" alt=""><figcaption></figcaption></figure>

## 3. Run the Trigger <a href="#block-629b62fa6c1348efbee3a6d6acdee8d8" id="block-629b62fa6c1348efbee3a6d6acdee8d8"></a>

It’s time to try your trigger! To run the trigger from the Page Editor, click the Run Trigger button above the Data Panel.

![image](https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/ac6dd79e-bc86-4a64-9e13-3f759aed3417/Untitled/w=640,quality=80)

If you search for the word `disclosure` on the page, you’ll see that the comment is now highlighted in yellow!

![image](https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/c40c8364-3651-4d69-8f57-ef3d74df9051/Untitled/w=3840,quality=80)

Remember to save your trigger by clicking the save icon in the Mod Panel:![image](https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/9087eda5-3e84-4164-a21b-6c5d854e479f/Untitled/w=640,quality=80)

Clicking the Save icon will bring up a modal for [saving-a-mod](https://docs.pixiebrix.com/developing-mods/sharing-mods/saving-a-mod "mention"). Set an alias if you haven't already and give the mod a unique ID and description, then click **Create**.

We can now highlight important keywords on the page load. You can use this method to highlight any keywords with multiple colors — you just need to copy the highlight brick and change the `backgroundColor` field and keywords in the selectors.

🙋Need some help with building? [Head over to the Slack Community](https://slack.pixiebrix.com/), and we’ll gladly help you!<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.pixiebrix.com/tutorials/developer-tutorials/beginner/web-highlighter-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
