Highlight keyword, search the full html body.
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
// the highlightStartTag and highlightEndTag parameters are optional
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = "<font style='color:red; background-color:white;'>";
highlightEndTag = "</font>";
}
// find all occurences of the search term in the given text,
// and add some "highlight" tags to them (we're not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags and script blocks, so
// we have to do a little extra validation)
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();
while (bodyText.length > 0) {
i = lcBodyText.indexOf(lcSearchTerm, i+1);
if (i < 0) {
newText += bodyText;
bodyText = "";
} else {
// skip anything inside an HTML tag
if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
// skip anything inside a <script> block
if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
bodyText = bodyText.substr(i + searchTerm.length);
lcBodyText = bodyText.toLowerCase();
i = -1;
}
}
}
}
return newText;
}
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
// if the treatAsPhrase parameter is true, then we should search for
// the entire phrase that was entered; otherwise, we will split the
// search string so that each word is searched for and highlighted
// individually
if (treatAsPhrase) {
searchArray = [searchText];
} else {
searchArray = searchText.split(" ");
}
if (!document.body || typeof(document.body.innerHTML) == "undefined") {
if (warnOnFailure) {
alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
}
return false;
}
var bodyText = document.body.innerHTML;
for (var i = 0; i < searchArray.length; i++) {
bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
}
document.body.innerHTML = bodyText;
return true;
}
function callOnLoad(){
highlightSearchTerms("nwt");
}
分享到:
相关推荐
vue-highlight-words来自react-highlight-words Vue组件的简单端口,用于突出显示较大文本正文中的单词。 演示为什么? 它使用render t vue-highlight-words一个来自react-highlight-words Vue组件的简单端口来突出...
import Highlighter from "react-highlight-words" ;ReactDOM . render ( < Highlighter highlightClassName = "YourHighlightClass" searchWords = { [ "and" , "or" , "the" ] } autoEscape = { true } ...
颜色突出显示多个关键字 该插件用于突出显示多个关键字。 主要功能指的是Multi-highlight插件,但修复了Multi-highlight插件中的一些未知错误。 支持语言:English (United States)
Vue Words Highlight 用于高亮关键词的vue组件,可以用于类似于搜索的功能。 Installation npm install --save vue-words-highlight # or yarn add vue-words-highlight Usage Use Directive import { highlight...
名称:Cross Words Highlight 划词高亮 -------------------- 版本:1.1.3 作者:1776243356a 分类:其他 -------------------- 概述:网页划词高亮,随时随地添加批注,记录笔记,方便快捷的管理正在阅读的网页文档...
Highlight Plus v9.0
vscode中的语法高亮和语义高亮 Vscode Syntax Highlight Vscode Semantic Highlight
非常好用的highlight插件 highlight demo 可供学习使用
Highlight 3.9 发布,该版本改进了插件接口用于支持输出文本的操作,增加一些插件用来在 HTML、LaTeX 和 RTF 输出中插入超链,改进了 Perl 和 N3 语言定义文件,修复了一些小bug。 Highlight 用来对源码进行语法...
unity高光、unity描边,unity描边高光、unity highlight
Highlight Plus可以为场景中的任何3D对象添加轮廓,发光,叠加,透视和其他效果。 内置两个包,支持默认材质以及URP渲染管线 ** Built-in pipeline ** - Import the package from the folder /Builtin. - Requires ...
PanGu.HighLight.dll 文件
该高亮插件是在Multi-highlight的基础上进行了升级,优化了文本输入的布局,并支持关键词分类固定颜色,且增加对应分类独立开关,弥补了Multi-highlight的不足。
Unity Highlight Plus v7.7.2
Highlight 用来对源码进行语法着色,并输出到HTML 或者是 DOC 文档。 Coloured output in HTML, XHTML, RTF, TeX, LaTeX, SVG and XML format 支持超过120种编程语言 包含40种颜色风格 平台无关 ...Highlight 截图
从网上找到的GNU Source-highlight的windows版本下载不下来 自己就把GNU Source-highlight源代码在windows下用mingw编译了一遍 相当好用的代码高亮软件
Highlight Plus 为场景中的所有对象都添加了轮廓、发光、 覆盖及其他特效。 HP 在设计时考虑到了平台兼容性和性能。 ** 视觉特征 ** - 轮廓颜色、α 和宽度 - 外部发光颜色、宽度、α、动画速度、抖动、多色彩 - ...
前端开源库-angular2-highlight-jsAngular2 Highlight JS,Highlight.JS与Angular 2集成
highlight.js独立的JS文件,下载highlight.js后可以直接使用...