如何运行在加载时操作 iframe 内容的代码

how to run code that manipulates an iframe's content when it loads?

本文关键字:iframe 操作 代码 加载 何运行 运行      更新时间:2023-09-26

我试图在加载时隐藏Facebook喜欢按钮的计数值。 它运行在iframe中,如下所示:

<iframe src="//www.facebook.com/plugins/like.php?href='+'facebook.com'+'&amp;send=false&amp;layout=button_count&amp;width=90&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:21px;margin:30px 0px 25px 0px;" allowTransparency="true"> </iframe>

所以我尝试在 iframe 标签中插入一个加载,例如:

onload="
function loadme() { 
    // pluginCountTextConnected is the like text count's classname
    var x = document.getElementsByClassName('pluginCountTextConnected');
    console.log(x); 
} 
loadme();
"

但代码返回一个空列表。 在这种情况下,我假设document引用是指加载 iframe 的文档,而不是 iframe 文档。 我该如何将其指向 iframe 的内容?

小提琴:这里

我应该补充一点,iframe是从Facebook上运行的内容脚本Chrome扩展加载的,这就是为什么我认为这可能不受同源政策的约束。

更新:为什么有人投票关闭?

iframe未托管在您的域上,因此您无法访问它,请查看 http://en.wikipedia.org/wiki/Same_origin_policy