[英]Need help with modifying an existing regex search extension
I would like to ramp on extension development by modifying an existing extension. 我想通过修改现有扩展来扩展扩展。 I have zero experience with JavaScript, but i do have experience with C, C++, Java and Python.
我对JavaScript的使用经验为零,但对C,C ++,Java和Python却确实有经验。 I chose the Regular Expression Search extension by bizsimon.
我选择了bizsimon的正则表达式搜索扩展。 Here is the JavaScript code of the content script which i am trying to understand.
这是我试图理解的内容脚本的JavaScript代码。
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { sendResponse(chrome_regex_search(request.exp)); });
function chrome_regex_search(exp) {
var tw=document.createTreeWalker(document.getElementsByTagName("body")[0], NodeFilter.SHOW_TEXT, null, false);
while (node = tw.nextNode()) {
node.parentNode.innerHTML=node.parentNode.innerHTML.replace(/<font class="chrome_search_highlight"[^>]*>(.+)<\/font>/igm, '$1');
}
try {
var pattern=eval("/(>[^<]*)("+exp+")([^<]*<)/igm");
var tw=document.createTreeWalker(document.getElementsByTagName("body")[0], NodeFilter.SHOW_TEXT, null, false);
while(node=tw.nextNode()) {
node.parentNode.innerHTML=node.parentNode.innerHTML.replace(pattern, '$1<font class="chrome_search_highlight" style="background: yellow">$2</font>$3');
}
return {"count": document.getElementsByClassName("chrome_search_highlight").length};
} catch(e) {
return {"count": 0};
}
}
And here are my questions: 以下是我的问题:
What does this code do? 这段代码有什么作用?
node.parentNode.innerHTML=node.parentNode.innerHTML.replace(/]*>(.+)</font>/igm, '$1'); node.parentNode.innerHTML = node.parentNode.innerHTML.replace(/]*>(.+)</ font> / igm,'$ 1');
for your question #1: That code looks like it's trying to strip a <font>
tag from HTML, eg change <font ...>real content here</font>
to real content here
. 对于您的问题1:该代码看起来像是试图从HTML中剥离
<font>
标记,例如,将<font ...>real content here</font>
更改为real content here
。
just a side comment: prefer using new Regexp(somestring)
to eval("/"+somestring+"/")
, as the eval can lead to a possible security hole. 只是一个侧面的注释:与
eval("/"+somestring+"/")
,更喜欢使用new Regexp(somestring)
eval("/"+somestring+"/")
,因为eval可能导致可能的安全漏洞。 (see MDC docs for syntax particulars) (有关语法的详细信息,请参阅MDC文档 )
<font>
tag: specifically those that are of the "chrome_search_highlight" class. <font>
标记:特别是那些属于“ chrome_search_highlight”类的标记。 In other words, it's walking the node tree of the body element and removing previous search hit highlights. (>[^<]*)
group before, and similar group after, the regexp is there to help ensure that you're matching actual page text, not the name of an HTML element or an attribute name or value. (>[^<]*)
组,之后是类似的组,以帮助确保您匹配的是实际的页面文本,而不是HTML元素的名称或属性名称或值。 Ie the regexp search hit must be preceded by a >
that is not followed by a <
until after the search hit. >
之前,然后是<
直到搜索命中之后。 Off to bed... 去睡觉了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.