Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
highlight-search-term
Advanced tools
Highlight search term in a page. Vanilla JS, compatible with frontend frameworks (React, Vite, Angular, etc).
Highlight search term in a page. Vanilla JS, compatible with frontend frameworks (React, Vite, Angular, etc).
This library does not modify the DOM. It relies on the browser's CSS Custom Highlight API (not supported by Firefox yet). See the motivation and implementation challenges in the blog post "Highlight Search Terms In Page Content".
Note: This is not a syntax highlighter library. It highlights text in a page based on a search term.
npm install highlight-search-term
The library exports a single function that expects a search term and a CSS selector of the element to search in.
highlightSearchTerm({ search: search.value, selector: ".content" });
This creates a highlight range named "search" that you can highlight with CSS, e.g.:
::highlight(search) {
background-color: yellow;
color: black;
}
You can use it directly in your HTML:
<script type="module">
import { highlightSearchTerm } from "https://cdn.jsdelivr.net/npm/highlight-search-term@1.0.0/src/index.js";
const search = document.getElementById("search");
search.addEventListener("input", () => {
highlightSearchTerm({
search: search.value,
selector: ".content",
});
});
</script>
Or, if you use a bundler like Vite, you can import it in your JavaScript:
import { highlightSearchTerm } from "highlight-search-term";
const search = document.getElementById("search");
search.addEventListener("input", () => {
highlightSearchTerm({
search: search.value,
selector: ".content",
});
});
If you use a frontend framework like React, you can use it in a useEffect
hook:
import { useEffect, useState } from "react";
import { highlightSearchTerm } from "highlight-search-term";
export default function App() {
const [search, setSearch] = useState("");
useEffect(() => {
highlightSearchTerm({ search, selector: ".content" });
}, [search]);
return (
<div>
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
<div className="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
);
}
MIT, courtesy of Marmelab
FAQs
Highlight search term in a page. Vanilla JS, compatible with frontend frameworks (React, Vite, Angular, etc).
The npm package highlight-search-term receives a total of 1,180 weekly downloads. As such, highlight-search-term popularity was classified as popular.
We found that highlight-search-term demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.