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>

特定の URL に制限する必要があります。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

はい、できます。

検索フォーム内で隠しフィールドを使用します。

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

特定の URL に制限するには:

    <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 に答える