无论出于何种原因,这并不重要,我正在尝试通过 iframe 将 google shopping 与另一个页面结合起来。
我已经尝试了建议的方法 here ,包括在 iframe 中嵌入谷歌自定义搜索查询,但谷歌自定义搜索不允许访问购物标签。
我想,如果你不能嵌入谷歌,那就把自己嵌入其中。所以我开始在页面中注入(inject)一些 jQuery
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type.
jQuery.noConflict();
将 google shopping 搜索结果页面清理成我需要的,即 div#search 中的 html
jQuery(function($) {$('#search').show().parentsUntil('body').andSelf().siblings().hide();});
创建一个 iframe 并注入(inject)它:
var iframe = document.createElement('iframe')
iframe.src="http://example.com"
iframe.width="100%";
iframe.height="500";
iframe.style="visibility:visible";
document.body.appendChild(iframe)
唯一的问题是 iframe 没有加载页面内容,而是空白。如果您在任何其他页面中尝试上面的代码片段,它就会起作用。谷歌似乎阻止了 iframe 的加载。我该如何解决这个问题?
最佳答案
Google 似乎无法使用 iframe 工作...即使您不使用 JS。 你应该使用的是 Google Custom Search API ,它允许您创建自定义搜索引擎。 您只需输入一个示例网站,将选项更改为搜索所有网络。并再次删除您输入的网站。 要创建自定义搜索引擎,您需要一个 Google 帐户。 开始here .
关于javascript - 如何在 Google 搜索结果页面中嵌入 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36217601/