用Javascript来访问Disqus评论文本框? [英] Javascript to access Disqus comment textbox?
问题描述
我正在研究浏览器扩展,它允许我访问文本框内的评论/帖子。很多网站现在使用Disqus作为评论的一种方式,但是我无法找到一种方法来访问Disqus评论框(Disqus API并没有多少说明),因为正在输入文本。
任何人都知道访问它的方法吗?
解决这个问题的最好方法是开始分析Disqus API如何执行其评论系统。您最好的朋友是Google Chrome自带的Inspector(开发者工具)。
当您分析DOM时(右击并查找该评论文本区域),你会注意到它是一个iframe。您应该想到,它是Discus域的一个跨域请求,以获取该评论框插件的信息。你可以看到,通过查看标签,它有一个指向domain.disqus.com的href,其中domain是你正在查看的网站。
例如,当您访问TechCrunch,iframe将指向注入评论框的 http://techcrunch.disqus.com 。
您可以使用内容脚本来读取和操作这些注入的页面,因为Content-Scripts也可以通过全帧
content_scripts:[
{
matches:[http:// * / * ],
js:[cs.js],
run_at:document_end,
all_frames:tr然后,在你的cs.js(内容脚本文件)中,你会发现你的cs.js(内容脚本文件)搜索给定的iframe的评论框。 //我们只需要检查IFrame的来源是否来自discus.com
if(location.hostname.indexOf('。disqus.com')!= -1){
//提取textarea(必须只有一个)
var commentBox = document.querySelector ('#评论');
if(commentBox){
//注入一些文本!
commentBox.innerText ='Google Chrome Injected!';
}
}
最后,您会看到美妙的单词 Google Chrome Injected!
希望您能够推动创建出色的Chrome扩展程序:)上面的代码正常工作,因为我在本地进行了测试。
I am working on a browser extension which should allow me to access comments/posts inside textboxes. A lot of sites now use Disqus as a way to comment, but I can't figure out a way to access the Disqus comment box (the Disqus API doesn't tell much either) as text is being entered.
Anyone know of a way to access it?
解决方案 The best way to figure it out is to begin analyzing how Disqus API does their comment system. Your best friend at this point is the Inspector (Developer Tools) that comes with Google Chrome.
When you analyze the DOM (right clicking and locating that comment text area), you will notice that it is an iframe. It should come to your mind that it is a cross-origin request to Discus domain to get the information for that comment box plugin. You can see that by looking at the tag, it has a href that points to domain.disqus.com where domain is the website your looking at.
For example, when you visit TechCrunch, the iframe will point to http://techcrunch.disqus.com that injects the comment box.
You can use Content-Scripts to read and manipulate those injected pages, because Content-Scripts can inject into IFrames too via all-frames manifest name.!
As an example, to setup a Content-Script, you need the content_scripts portion in the manifest file:
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["cs.js"],
"run_at": "document_end",
"all_frames": true
}
Then, within your cs.js (content script file), you find the comment box from searching the given 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!';
}
}
At the end, you will see the wonderful words "Google Chrome Injected!"
Hope that gives you a push creating awesome Chrome Extensions :) The code above works, since I tested it locally.
这篇关于用Javascript来访问Disqus评论文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!