Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用自定义代码修改Google自定义搜索结果_Javascript_Google Search - Fatal编程技术网

Javascript 使用自定义代码修改Google自定义搜索结果

Javascript 使用自定义代码修改Google自定义搜索结果,javascript,google-search,Javascript,Google Search,我的网站上安装了一个谷歌定制搜索引擎。我想修改搜索结果。我知道这会伤害到ToS 因此,首先,我想从每个搜索结果中删除一些字符串。以下是我之前编写的函数: <script type="text/javascript"> setInterval("pakeisti()",100) function pakeisti()   { elem = document.getElementById("searchas"); y = elem.getElementsByTagName

我的网站上安装了一个谷歌定制搜索引擎。我想修改搜索结果。我知道这会伤害到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>

setInterval(“pakeisti()”,100)
函数pakeisti()
  {
elem=document.getElementById(“searchas”);
y=元素getElementsByTagName(“div”);
对于(i=0;inewHTML=newHTML.replace(“发生此错误是因为
y[i].innerHtml
未定义

为避免出现问题,请将
if
语句更改为:

if (str.search("gs-title") != 0 && y[i].innerHtml != null) {
您可能会遇到这种情况,因为您在
if
语句中错误地使用了
String.search()
。如果搜索字符串未出现在目标字符串中,
.search()
将返回
-1
,而不是0。(0的响应表示字符串出现在目标字符串的第一个字符处。)你可能是说:

if (str.search("gs-title") != -1 ...