隐藏Google自定义搜索元素API 2.0的搜索引擎ID
Hide Search Engine ID for Google Custom Search Element API 2.0
我正在ASP.NET MVC项目中实现Google Custom Search Element API 2.0
。在文档中,他们要求在视图中包含以下javascript,并在其后包含<gcse:search>
元素。
<script>
(function() {
var cx = 'xxxxxxxx:yyyyyyyy';
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>
然而,搜索引擎id在以下行中可见:
var cx = 'xxxxxxxx:yyyyyyyy';
在浏览器中,选择View Source
(或类似内容)允许用户查看脚本和搜索引擎id。
我如何确保没有人能看到我的身份证?
我找到了解决这个问题的方法。通过将谷歌脚本移动到其自己的函数,该函数将搜索引擎Id作为参数:
function buildSearchElement(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);
};
然后,我在控制器上调用一个方法,该方法在页面加载时返回搜索引擎Id。完成后,回调为buildSearchElement
函数:
(function ()
{
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function()
{
if (httpRequest.readyState === 4 && httpRequest.status === 200)
{
buildSearchElement(httpRequest.responseText);
}
}
httpRequest.open("GET", "/GetSearchElementId");
httpRequest.send();
})();
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- Javascript复选框函数:;缺少:在属性id之后"
- Twitter引导程序Typeahead-Id&标签
- 如何与特定ID交谈
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 如何在速度模板中获取LiferayPortlet实例id
- 实现一个建立在google.com之上的自定义搜索引擎
- 隐藏Google自定义搜索元素API 2.0的搜索引擎ID