我完全不知道如何将 Google 的附加链接搜索框与 Google 的自定义搜索结合使用。
有人知道我是如何开始连接 2 的吗?
Google 为站点链接搜索框提供以下代码:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "https://www.example-petstore.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "https://query.example-petstore.com/search?q={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
然后您就有了 Google 的自定义搜索代码:
<script>
(function() {
var cx = '006674923042857018221:WMX2084923030';
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>
我究竟如何连接 2?希望你能给我指出正确的方向。
最佳答案
在 Google 提供的 Javascript 代码中有一个 var cx = 'YOUR_LONG_CX_KEY
。使用它来填充具有以下 URL 模式的 potentialAction.target
:
"target": "http://www.google.com/cse?cx=YOUR_LONG_CX_KEY&q={search_term_string}",
您可以通过在浏览器中使用您自己的搜索词字符串点击它来确认 URL 是否有效。
但是,我不知道 Google 是否会接受此为 their docs对于“目标”字段说,“这必须是一个 URL 模式,指向与正在搜索的内容位于同一域的地址。”另一方面,同一页面的顶部显示,“您的站点没有搜索引擎?您可以使用 Google 自定义搜索引擎设置一个。”
关于javascript - Google 用于 Google 自定义搜索的附加链接搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26404164/