If you want to create your own AI Copilot, you can customize our template to build one to fit your...
[Guide] Check for match in highlighted text
If you need to search for a specific word or phrase in a block of text, PixieBrix has a mod that can help.
Activate the mod here and follow along to see it in action and learn what's happening under the hood.
match, PixieBrix,mod
making it easy to test on this very page!See it in action
Select some text on this page (could be a word, a paragraph, or even the whole text on the page).
You'll see a menu bar of emojis appear. Click the eyes 👀 to search the selected text for a match.
If any of the words you provided in the configuration options appear, you'll get an alert that says "Text was found".
Pretty magical, eh?
How does this work?
Let's look at what's happening under the hood so you can customize this component to incorporate something like this into your mods.
🟣 Open the Page Editor
To view the bricks we used in this mod, you'll need to open the PixieBrix Page Editor.
- Right-click anywhere on the page and choose Inspect
- Position the Dev Tools dock at the bottom of the page (click the three dots in the top right)
- Find the PixieBrix tab on the toolbar
🟣 Viewing bricks in a mod
With the Page Editor open, the first panel on the left shows a list of all your activated mods. Mod packages are listed with their components nested underneath them.
🖱️ Click the Check selected text for match mod package, and then click the Check for string mod component underneath it.
Once you click a mod component, you'll see the Brick Actions panel appear next to the Mod Listings Panel.
Mods are made up of bricks that perform specific actions, like functions. Piecing these actions together is what creates automated workflows.
The first brick is a starter brick, and it must always be one of the five bricks used to launch a mod.
🟣 Launching the mod
The first brick in this mod is a Context Menu brick. This means this mod will be available to run from a menu when text is selected or when a user right-clicks on a page.
You can launch a mod in other ways, such as with keyboard shortcuts or triggers like page loads or when elements are clicked.
🟣 Bricks that make up the actions
Below the starter brick is a series of bricks that do the actions required to search a provided piece of text and then return a message when it's found.
Click on a brick in the Brick Actions Panel to view the configuration options available for that brick.
PixieBrix's JavaScript brick helps you transform data and perform various functions to create the output you need. In this case, we're transforming the string of comma-separated words and phrases that were provided when the mod was activated and converting those into strings in an array so we can check for each one.
We start with a Try-Except brick that works much like Try-Catch in JavaScript, allowing you to gracefully handle when there is an error. We do this so that when a text match is found, the mod stops searching for information.
The For-Each Loop brick runs for each item in your array, performing the actions inside it. (Ie, if you had 3 words to search, this loop will run 3 times.)
Inside the For-Each Loop, we have a Search Text brick that searches for the specific text in the provided text.
The Cancel current action brick will throw an error, which when wrapped in a Try-Except and For-Loop bricks will understand means "stop running and let the user know we found something".
The important thing we are doing in this brick is indicating when this brick should run by using Conditions in the Advanced Options.
By creating a statement in the Condition field, we are telling this brick to only run if it finds a match for the given text that was searched in the previous Search text brick.
When the error is thrown (ie, match was found), we have a Window Alert brick in the "error" section of the Try-Except brick that lets the user know a match was found.
Building onto this mod
You could add other bricks to this mod to add more functionality.
If you wanted to check for text automatically on page load, you could add a brick that grabs the text from the entire page and replace the selected text with that. You'd also want to create a new mod with a trigger for Page Load and copy these bricks to that mod.
You might also want to know which phrase was found or where it was found in the selected text. You could create Mod Variables to keep track of which query was found when searching in the For-Loop and access the output from the search brick to point out the location of the text that was found.
This is just the beginning—your customization options are virtually endless. If you want to try it out, start a free trial of PixieBrix or chat with our team to discuss workflows you might use on your teams.