用于隐藏跨域iframe的子节点的用户脚本 [英] Userscript to hide a child node of a cross-domain iframe
问题描述
我想通过(Tampermonkey)用户脚本隐藏带有一些单词的注释.例如,我尝试应用脚本
I want to hide the comments with some words inside by a (Tampermonkey) user script. As an example, I tried to apply a script
// ==UserScript==
// @name Hide CNN
// @match http://www.cnn.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_addStyle
// @run-at document-end
// ==/UserScript==
$('div.post-body:contains("Abbas")').hide()
转到页面 http ://www.cnn.com/2014/07/08/world/meast/mideast-tensions/index.html?hpt = hp_t1 ,其中包含以下帖子代码
to the page http://www.cnn.com/2014/07/08/world/meast/mideast-tensions/index.html?hpt=hp_t1 with the following code for posts
<div class="post-body">
<header>
...
</header>
<div class="post-body-inner">
<div class="post-message-container" data-role="message-container">
<div class="publisher-anchor-color" data-role="message-content">
<div class="post-message " data-role="message" dir="auto">
<p>Abbas is nothing but puppet dog of Western savages and Nazis who want to enslave entire world.</p>
</div>
...
</div>
但是该脚本似乎没有执行任何操作.我究竟做错了什么?是否可以使用用户脚本过滤网页的动态加载部分?
But the script does not seem to do anything. What am I doing wrong? Is it possible at all to filter a dynamically-loaded part of a webpage with a user script?
UPD:问题在于注释是从另一个域的iframe中加载的.如何使用Tampermonkey隐藏此类iframe的子节点?我需要以某种方式使用GM_xmlhttpRequest
吗?
UPD: The problem is that comments are loaded in the iframe from another domain. How do I hide a child node of such an iframe with Tampermonkey? Do I need to use GM_xmlhttpRequest
somehow?
推荐答案
Disqus 支持的注释通常加载在< iframe>中.因此,您无需将脚本设置为在主站点上运行,而是将其设置为在iframe src
URL上运行.在这种情况下,http://disqus.com/embed/comments/...
Disqus-powered comments are typically loaded in an <iframe>. So rather than set your script to run on the main site, you set it to run on the iframe src
URL. In this case, http://disqus.com/embed/comments/...
这些注释也是AJAX驱动的.因此,您必须使用精通AJAX的技术.
Also, these comments are AJAX driven. So you must use AJAX savvy techniques.
这样的脚本应该可以工作(选择器可能需要调整):
A script like this should work (selector might need tuning):
// ==UserScript==
// @name Hide select CNN comments
// @match http://disqus.com/embed/comments/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//--- Only run on comments for CNN pages
if (/&f=cnn&/i.test (location.search) ) {
waitForKeyElements ('li.post:contains("Abbas")', hideComment);
}
function hideComment (jNode) {
jNode.hide ();
}
这篇关于用于隐藏跨域iframe的子节点的用户脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!