Chrome扩展程序iframe dom参考信息 [英] Chrome Extension iframe dom reference disqus

查看:111
本文介绍了Chrome扩展程序iframe dom参考信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得对跨域iframe/框架的dom的引用?

How do I get a reference to the dom of a cross domain iframe/frame?

我想做一些事情来打扰带有扩展名的评论.

I want to do some stuff to disqus comments with an extension.

我的清单包含以下内容:

My manifest has the following:

"all_frames": true,
"matches": ["*://*.disqus.com/*","*://disqus.com/*", "http://somesite.com"]

我不是试图在框架外进行通信-这是js会完成工作,而无需告诉"我任何事情.

I am not trying to communicate outside of the frame - that is the js will take care of the work without needing to 'tell' me anything.

all_frames应该将列出的js文件注入每一帧,不是吗?

all_frames should inject the listed js files into every frame, no?

当我这样做时:

if (window != window.top){
  alert('In an IFRAME: ' + window.location.href);
}

...我得到了预期的Disqus URL.

...I get the expected disqus URLs.

但是当我这样做时:

var btnCommentBlock = document.getElementsByClassName('dsq-comment-buttons');
alert('btnCommentBlock length: ' + $(btnCommentBlock).length);

...我的长度为0.

...I get 0 for length.

推荐答案

糟糕!我在github上找到了答案:

Woohoo! I found the answer on github:

https://gist.github.com/471999

工作代码为:

$(document).ready(function() {
  window.disqus_no_style = true;

  $.getScript('http://sitename.disqus.com/embed.js', function() {
    var loader = setInterval(function() {
      if($('#disqus_thread').html().length) {
        clearInterval(loader);
        disqusReady();
      }
    }, 1000);
  });

  function disqusReady() {
    //whatever you can imagine
  }
});

我将其放在disqusReady()函数中:

I put this in the disqusReady() function:

var aTestHere = document.getElementsByClassName('dsq-comment-body');
alert(aTestHere[0].innerHTML);

...然后按预期方式获取了innerHTML.

...and got back the innerHTML as expected.

穆罕默德(Mohamed),非常感谢您抽出宝贵的时间来回答我的问题.如果您尚未将该链接发布到github,则不会告诉我什么时候知道它或找到其他代码.

Mohamed, I'd really like to thank you for taking the time to interact with my question. If you hadn't posted that link to github there's no telling when if ever I'd have figured it out or found the other code.

经过几分钟的试验,看来没有必要调用getScript,因此您应该可以对此进行注释.

edit: After a few minutes of experimenting it looks like it is not necessary to call getScript so you should be able to comment that out.

window.disqus_no_style也是不必要的,所以我也将其注释掉了.

Also unnecessary is window.disqus_no_style so I commented that out too.

我将做更多实验,以后再更新答案.这两件事之一使我无法在我使用的Disqus网站上发表评论.//他们仍然允许访问dom和发布功能.

I'll experiment some more and update the answer later. One of those two things prevented me from being able to actually post a comment at the disqus site I use. //them out still allows access to the dom and the ability to post.

这篇关于Chrome扩展程序iframe dom参考信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆