简体   繁体   English

在修改现有正则表达式搜索扩展方面需要帮助

[英]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: 以下是我的问题:

  1. 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');

  2. I would like to add navigation buttons which enable the user to move from one search result to another. 我想添加导航按钮,使用户能够从一个搜索结果移至另一搜索结果。 What changes are required in the script? 脚本中需要进行哪些更改? I assume that now i will need to save some state which remembers which search result is currently being visited. 我假设现在我将需要保存一些状态,以记住当前正在访问哪个搜索结果。 How do i make the browser jump from one search result to another? 如何使浏览器从一个搜索结果跳转到另一个?
  3. Any useful comments which would help understand the code or even a code walkthru would be very much appreciated. 我们将不胜感激任何有益的注释,这些注释有助于理解代码,甚至帮助您理解代码。

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文档

  • Question #1: as Jason S said, it's stripping the <font> tag: specifically those that are of the "chrome_search_highlight" class. 问题1:正如Jason S所说,它正在剥离<font>标记:特别是那些属于“ chrome_search_highlight”类的标记。 In other words, it's walking the node tree of the body element and removing previous search hit highlights. 换句话说,它正在遍历body元素的节点树并删除以前的搜索命中重点。
  • Then in the second tree-walking loop, it's adding those same font tags around occurrences of the supplied regexp. 然后在第二个树遍历循环中,在出现的正则表达式周围添加这些相同的字体标签。 The cryptic (>[^<]*) 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. regexp之前是一个神秘的(>[^<]*)组,之后是类似的组,以帮助确保您匹配的是实际的页面文本,而不是HTML元素的名称或属性名称或值。 Ie the regexp search hit must be preceded by a > that is not followed by a < until after the search hit. 也就是说,regexp搜索命中必须在>之前,然后是<直到搜索命中之后。

Off to bed... 去睡觉了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM