0

我的网站上安装了 Google 自定义搜索引擎。我想修改搜索结果。我知道这可能会损害 ToS。

所以,首先,我想从每个搜索结果中删除一些字符串。这是我之前写的函数:

<script type="text/javascript">
setInterval("pakeisti()",100)
function pakeisti()
  {
    elem = document.getElementById("searchas");
    y = elem.getElementsByTagName("div");
    for (i=0; i< y.length; i++)
     {
          str = y[i].className;
      if (str.search("gs-title") != 0 ) {
        var newHTML = y[i].innerHtml;
        newHTML = newHTML.replace('STRING - ',' k');
        newHTML = newHTML.replace('<a','<p');
        newHTML = newHTML.replace('</a>','</p>');
        y[i].innerHtml = newHTML; }
     }
  }
</script>

早些时候这个脚本有效,但现在它给了我错误: Uncaught TypeError: Cannot call method 'replace' of undefined

4

1 回答 1

0

That error is occurring because y[i].innerHtml is undefined.

To avoid the problem, change the if statement to this:

if (str.search("gs-title") != 0 && y[i].innerHtml != null) {

You are probably experiencing this because you're using String.search() incorrectly in your if statement. If the search string does not appear in the target string, .search() will return -1, not 0. (A response of 0 means that the string appears at the first character in the target.) So you probably meant:

if (str.search("gs-title") != -1 ...
于 2013-07-18T20:09:07.830 回答