2

我有一个 Google 自定义搜索(将来会是一个站点搜索),我想使用 Google 提供的代码段:

<script>
  (function() {
    var cx = '...';
    var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
  })();
</script>

<gcse:search></gcse:search>

我需要将其限制为某些网址。使用xml api时,可以使用参数as_sitesearch来指定这个过滤器。有没有办法用上面的代码做到这一点?

有了这个,我现在可以在加载搜索之前访问 google.search.cse 对象.. 但我仍然不知道参数:

<script>


var myCallback = function() {
  if (document.readyState == 'complete') {
    // Document is ready when CSE element is initialized.
    // Render an element with both search box and search results in div with id 'test'.
    google.search.cse.element.render(
        {
          div: "test",
          tag: 'search'
         });
  } else {
    // Document is not ready yet, when CSE element is initialized.
    google.setOnLoadCallback(function() {
       // Render an element with both search box and search results in div with id 'test'.
        google.search.cse.element.render(
            {
              div: "test",
              tag: 'search'
            });
    }, true);
  }
};

// Insert it before the CSE code snippet so that cse.js can take the script
// parameters, like parsetags, callbacks.
window.__gcse = {
  parsetags: 'explicit',
  callback: myCallback
};


  (function() {

    var cx = '007407192365638902354:eyxoavi7oa0';
    var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
  })();
</script>



<style>
.gsc-control-cse * {
box-sizing:content-box;
}
</style>
4

3 回答 3

0

我也有同样的问题。我能够将搜索限制在 Google Site Search 版本 2 中的特定文件夹的唯一方法是将其硬编码到元素中:

<gcse:search as_sitesearch="www.mydomain.com/site1"></gcse:search>

如果有一种方法可以通过 JavaScript 添加 as_sitesearch,就像版本 1 一样。

于 2014-06-26T15:55:18.860 回答
0

是的你可以。

在搜索表单中使用隐藏字段:

<input type="hidden" name="as_sitesearch" value="your_url_here/directory">
于 2013-01-30T14:29:21.760 回答
-1

限制某些网址:

    <gcse:searchbox-only as_sitesearch="mydomain.com" resultsUrl="http://mydomain.com/search-results/" enableAutoComplete="false"></gcse:searchbox-only>

请务必将 mydomain.com 替换为您要限制的相应域。

于 2014-04-16T23:15:52.337 回答