我有我的Google自定义搜索引擎(GCSE)代码:
<script>
(function() {
var cx = '008589157460623253837:mhsjluzrngo';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
但是我的加速移动页面(AMP)网站不允许我添加JavaScript,而且我也看不到任何允许将GCSE嵌入AMP网站的AMP组件。
如何在不违反AMP合规的情况下在我的网站上获得GCSE?
由于
答案 0 :(得分:1)
你是对的,这还不支持,因为它不在AMP Components list中。尝试提交feature request on their githubpage。
答案 1 :(得分:1)
您可以尝试将该代码放在单独的页面中,然后通过ng new
组件在AMP中包含该页面。您还必须遵守此处描述的约束:https://www.ampproject.org/docs/reference/components/amp-iframe
答案 2 :(得分:1)
答案 3 :(得分:0)
我按照以下步骤,使用amp-form
向我的开源网站(https://csaba.page/)添加了搜索功能:
Search engine ID
,它看起来像“ 013535696182405026577:kmxne16xdtx”。您可以在CS控制台的“基本”标签上看到它,或者,如果您正在查看JavaScript代码,它就是cx
变量。<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
部分中添加head
,其他AMP
包括其中。body
中添加一个amp-form
,您要在其中放置搜索输入并根据需要设置表单样式:<div>
<form method="GET" class="search-form" action="https://www.google.com/cse" target="_top">
<div class="search-form-inner">
<input name="cx" type="hidden" value="013535696182405026577:kmxne16xdtx" />
<input name="ie" type="hidden" value="UTF-8" />
<input type="search" placeholder="Search..." name="q" required>
<input type="submit" value="Go" class="search-button">
</div>
</form>
</div>
还有一些博客文章: