开发者

Chrome Extension iframe dom reference disqus

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"]

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 should inject the listed js files into every frame, no?

When I do this:

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

...I get the expected disqus URLs.

But when I do this:

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

...I get 0 for length.


I updated my answer to Javascript to access Disqus comment textbox?

Basically, Disqus changed the selector. They no longer use textarea, they use contenteditable divs.

Something like this should work:

// We just need to check if the IFrame origin is from discus.com
if (location.hostname.indexOf('.disqus.com') != -1) {
  // Extract the textarea (there must be exactly one)
  var commentBox = document.querySelector('#comment');
  if (commentBox) {
    // Inject some text!
    commentBox.innerText = 'Google Chrome Injected!';
  }
}

Source Code: https://gist.github.com/1034305


Woohoo! I found the answer on github:

https://gist.github.com/471999

The working code is:

$(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
  }
});

I put this in the disqusReady() function:

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

...and got back the innerHTML as expected.

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.


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.

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜