Search and Highlight Words on a Page
Last updated
Was this helpful?
Last updated
Was this helpful?
This tutorial should take about 15 minutes. If you have any questions, click the purple live chat button in the bottom left corner or .
If you get stuck and cannot move forward, to see the final solution. Follow these docs if you need help Activating Mods.
Before starting this tutorial, please make sure you have:
a PixieBrix account ()
the Page Editor open ()
completed the Basic Translation Tutorial
At the end of each step, you'll see a 🏁 Checkpoint to know if you're ready to move to the next step. A screenshot will indicate how you should have configured the brick for that step.
You're ready to start when you have the Page Editor open, and your screen looks something like this:
This video walks through the steps on this page to give you a visual walkthrough of completing this tutorial.
If you prefer the text over the video, keep reading!
To find starter bricks, you'll click the New Mod button in the top left corner, next to the purple Home icon.
Choose Trigger, and you'll see your Page Editor fill a new mod in the first panel (Mod Listing Panel), a brick in the second panel ( Brick Actions Panel), and configuration options for the selected brick in the third panel (Brick Configuration Panel).
We'll want to make a few changes to the Trigger brick to be clear about what's happening and run most optimally.
Follow this table to see the fields we're updating and why:
Name
Search for words on a page
This name is what appears in the Mod Listing Panel, helping you identify which mod you're working on.
Sites
Click All URLs and Remove the standalone site by clicking ABC then X Remove
This allows the mod to run on every site. If you only want a mod to run on specific sites or domains, you can set those values here.
You're ready to move forward when your Trigger brick looks like this:
Next, we'll want to get the text from the page to run against our search words.
Click the + button below the Trigger brick and search for Extract from Page
in the modal that appears.
To scrape all the text from a given page, we'll rename the property to text
and set the Selector field to body
.
2.3 Run the mod by clicking Run Trigger to check your output in the Data Panel
Click the Run Trigger button in the top right corner of the Page Editor. Your mod will run the bricks, showing a green check mark on each brick with a successful run.
You're ready to move to the next step when your Extract from Page brick produces output with the text on the page, like in the above screenshot.
Next, we'll want to specify the keywords or phrases we want PixieBrix to search for on the given page. We could hard code those words, but to make this mod dynamic for each user, we'll create configuration options that each user can set when they activate the mod.
This means that when I activate the mod, I can set terms like pixiebrix,tutorials
and you can set something like learning,games
and the mod will work for both of us without requiring any changes in the Page Editor!
Technically what we're working on thus far is a mod component. You can have multiple mod components work together and put them in a mod package.
In the modal that appears, select the Create new mod... option from the dropdown, then click the blue Move button.
If this is your first time packaging a mod, you might be asked to set an alias. You can think of this as a username. It's what will appear at the beginning of your mod ID.
Once you've set an alias, you'll see a Create new mod modal. Feel free to adjust these fields, or leave them as is.
You can't change a mod's ID after it's been created, but you can change the Name and Description and increment the version later.
Once you're happy with the settings, click the blue Create button.
Once you clicked Create, you might have noticed that you now have a folder in your Mod Listing Panel, with a mod nested underneath it.
To configure settings and details for the mod package, you'll need to select the top-level mod (look for the folder icon!).
Once selected, you can use the tabs in the middle panel to make edits to the mod metadata, or set configuration options by going to Input Form.
The Input Form section allows you to set configuration data that must be set when someone activates the mod. This allows you to do anything from specify a Google Sheet to fetch data from, choose a database to store information in, or in this case, set a string of text that will be used throughout the mod.
You can think of these as constants that are set for each user who will be using the mod, and the user only has to set the option when they first activate the mod.
We want to add a field to capture a comma-separated string of any words or phrases a user may want to search on a page.
Click the + Add new field button and set the following values:
Name
words
This is how you'll refer to the value inside the mod. For instance, using @options.words
will return the value of whatever the current user has set in this field when they activated the mod.
Label
Keywords
A user-friendly description of what this option is used for. It will appear above the field.
Field Description
Add keywords and phrases separated by commas, no spaces needed.
More details about what to put in this field. Can be helpful to give more details to users, and accepts Markdown if you want to link to additional documentation or examples.
Input Type
Single line text (default)
You can choose from a variety of inputs, including dates, dates with time, and even files and Google Sheets. Check the dropdown to explore the options.
Default Value
pixiebrix,PixieBrix
Setting a default value can help users quickly activate a mod and see how it works if they don't understand a field. The user can always re-activate a mod and replace the default to their own text.
You're ready to move to the next step when your Input Form looks like this:
Before we go back into the mod component, we want to set our current input of text so we have something to work with inside the mod.
Make sure you still have the top-level mod selected (confirm this in the Mod Listing Panel), and choose the Current Inputs tab in the middle panel.
Click the Keywords field and it will suddenly populate with the default values we set (pixiebrix,PixieBrix
).
You can leave this as is or type something else.
Click the Save icon on the Mod Package in the first panel to save the mod changes, including the Current Inputs.
You'll see a modal asking you to confirm. Click Save.
You're ready to move to the next step when you go to your Current Inputs tab and see text in the Keywords field.
Now it's time to ask JavaScript to help us transform some data. Specifically, we'll want to take the string from the configuration options we set and create an array of the words and terms that were provided between commas.
Go back into the Search for words on a page mod inside the mod package. (You'll know you're back in the mod when the selected mod is the one with a lightning bolt, which is the icon for the Trigger brick.)
Click the + button under the Extract from Page brick, and search for JavaScript
then choose the Run JavaScript Function brick.
Replace the text inside the function field with the following:
This is a function that will take a string (words
) and split it based on the comma, and create an array of strings, removing any whitespace.
For example, if words
is "pixiebrix,PixieBrix"
this function will return ["pixiebrix","PixieBrix"]
, which will allow us to iterate over each word in future steps.
Below the function, you'll pass arguments that you access inside the function. In this example, we need to pass the words
argument.
Replace the default argument x
with words
and set the value to @options.words
. This will reference the configuration setting that a user has set when activating the mod.
(Scroll to the screenshot in the 🏁 checkpoint section below if you need a visual of what this will look like.)
To make it easier to reference this array in other bricks, update the Output field at the top of the JavaScript brick to words
instead of output
.
Click the Run Trigger button in the top right of the Page Editor to run the mod and confirm the output of your function is an array of the strings.
You're ready to move to the next step when your Run JavaScript Function brick looks like this:
Now we'll use the Search Text brick to check the extracted body of text again the array with words.
Click the + button below the Run JavaScript Function and search for the Search Text
brick. Hover over the brick and click the Add button to add it to your pipeline.
The Text field is where you specify what text is to be searched. In this case, we want to share the scraped body text.
Update the Text field to @data.text
, which will reference the text PixieBrix scraped from the page body in the Extract from Page brick.
The Query field specifies what is being searched. In this case, we want to search multiple words or phrases, which we processed in Step 5 with the JavaScript brick.
Update the Query field with @words
Once more, click the Run Trigger button at the top right corner of the Page Editor, and look in the Output tab. You should see an object called @matches
and inside of that is an array with an object for each match.
🏁 Checkpoint
You're ready to move to the next step when your Search Text brick looks like this:
Now we will use Control Flow to perform different actions whether there is a match or not.
Click the + button below the Search Text brick and add the If-Else brick.
Although not required, setting the Step Name can clarify the action for future developers who may edit this mod.
We'll update the Step Name to Alert if matches found
.
The Condition field evaluates a true or false statement. If the value returns true, any bricks added to the if
pipeline will run.
When the condition is not met and is not true, any bricks added to the else
pipeline.
Update the Condition field to {{ true if @matches.matches | length > 0 }}
, which will check if any items are in the matches array, meaning one of the words or phrases was found on the page. It will then execute any bricks in the if
pipeline.
If the array is empty (has a length of 0), then this will evaluate false and execute any bricks added in the else
pipeline.
Click the + button just below the if
and above the else
in the Brick Actions Panel.
Search for the Window Alert brick, and add it.
Update the Alert message to say Matches found
.
Optional: Change the alert type to success
to change the styling of the alert.
While the Window Alert brick is selected, click the copy button at the top of the Brick Actions Panel.
You'll see the Brick Actions Panel add paste buttons between the existing bricks. Click the paste button inside the else
pipeline to paste the brick in there.
Since this brick is in the else
pipeline, this message will be displayed when no matches are found.
Adjust the text in the Alert message to No matches found
.
Optional: change the Alert type to warning
.
Whether your terms show up on the page or not, you should see an alert indicating whether they exist.
Click the Run Trigger button in the top right of the Page Editor, and confirm the browser displays an alert.
You're ready to move to the next step when your Brick Actions Pipeline contains bricks in the if
and else
pipelines.
For the last building step, we'll add a brick that interacts with the page to highlight the specified words if they appear on the page.
Click the + button below the If-Else brick.
Search for the For-Each Loop brick, hover over it, and click Add.
Since we updated the If-Else brick's name to explain what was happening, let's do that for this brick as well.
Replace the Step Name field at the top of the brick with For each word in the list, highlight all instances on a page
The Elements field determines the number of times you iterate. This brick will run as many times as items are in the array. (If there are 3 items, the brick will run 3 times.)
Click the a,b,c at the end of the Elements field and choose @
Set the value to @words
The key is what you'll use to reference the specific item in the array, in this case, the word.
Set it to word
so it's clear.
When adding a For Each Loop brick, you'll see another pipeline appear in your Brick Actions Panel, this one called body
. Any bricks added to the body will execute for each item in the provided array.
Click the + button inside this pipeline and add the Highlight Text brick.
Set the Pattern field to @word
which is the current string in the iteration.
Optional: change the color to another option in the dropdown list.
Run the mod once more by clicking the Run Trigger button in the top right corner.
You should see your words highlighted on the page!
Time to save the mod, and share with a friend.
Click the save icon in the Mod Listings Panel, and close the Page Editor.
You can now use your mod on any webpage and view alerts and highlighted text based on your configured words.
Click the PixieBrix icon in your URL bar and then click the gear icon in the top right of the sidebar.
You'll arrive at the Mods page when you go to the Extension Console.
Select the Active filter if it's not already set, and you should see a list of any mods you have activated, such as the one you just built.
Click the three dots at the end of the row to open an action menu. Choose the Share with Teams action.
Copy the link at the bottom of the modal, which you'll send to your team to activate and set their configuration options.
Finally, click the purple Save and Close button.
When you send that activation link to your team, they'll activate and set their keywords when activating the mod.
Once they've set keywords, they'll click Activate, and be able to use this mod without ever opening the Page Editor.
You did it! You've completed the tutorial if you see your keywords highlighted on a page and a window alert letting you know if those words are found on the page.
Every mod starts with a , which determines how and when the mod is launched or initiated. For this tutorial, we'll use the Trigger brick because we want to take action every time a page loads.
Hover over the brick and click the blue Add button. You'll see the brick appear in your .
You'll also notice that a new variable appears in the Output tab of the last panel, the . Click the caret in front of the object to expand and preview the scraped text from the page.
To add a mod to a package, click the three dots on the Search for words on a page
mod in the .
You might have noticed when you added the If-Else brick, subsections were added to your . (We call these subsections pipelines.)
You'll need to go to the to share the mod.
In the modal that appears, select a team from the dropdown, or if you haven't already.
If it's not working, you can to see how it should be structured, and compare your build to the template mod. You can also or click the live chat button in the bottom left corner of this page.