0

我正在开发一个浏览器扩展程序,它应该允许我访问文本框中的评论/帖子。现在很多网站都使用 Disqus 作为评论的一种方式,但是当正在输入文本时,我无法找到访问 Disqus 评论框的方法(Disqus API 也没有提供太多信息)。

有人知道访问它的方法吗?

4

1 回答 1

2

解决这个问题的最好方法是开始分析 Disqus API 如何处理他们的评论系统。此时,您最好的朋友是 Google Chrome 附带的 Inspector(开发者工具)。

当您分析 DOM(右键单击并定位该评论文本区域)时,您会注意到它是一个 iframe。您应该想到,这是对 Discus 域的跨域请求,以获取该评论框插件的信息。您可以通过查看标签看到,它有一个指向 domain.disqus.com 的 href,其中 domain 是您正在查看的网站。

例如,当您访问 TechCrunch 时,iframe 将指向注入评论框的http://techcrunch.disqus.com

您可以使用Content-Scripts来读取和操作那些注入的页面,因为 Content-Scripts 也可以通过全帧清单名称注入到 IFrames 中。!

例如,要设置内容脚本,您需要清单文件中的 content_scripts 部分:

"content_scripts": [
  {
    "matches": ["http://*/*"],
    "js": ["cs.js"],
    "run_at": "document_end",
    "all_frames": true
  }

然后,在您的 cs.js(内容脚本文件)中,您可以通过搜索给定的 iframe 找到注释框。

// 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!';
  }
}

最后,您会看到“Google Chrome Injected”的精彩字样!

希望这能推动您创建很棒的 Chrome 扩展 :) 上面的代码有效,因为我在本地对其进行了测试。

于 2010-12-24T03:29:49.393 回答