2014-02-25 23 views
4

news site(监护人)具有显示在页面的右下角的一个小框,使用如下代码:如何使用Greasemonkey和JQuery来隐藏某个类的HTML元素?

<div class="initially-off social-cta-overlay"> 

    <div class="social-cta-overlay-content"></div> 

</div> 

我躲在内div的内部内容,因为他们不是重要。我写了一个使用JQuery的,因为它出现在每一页上隐藏这个框的Greasemonkey脚本:我安装脚本

// ==UserScript== 
// @name  hide-guardian-ad 
// @namespace guardian 
// @description Hides the Guardian social media frame 
// @version  1 
// @grant  none 
// ==/UserScript== 

// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js 

$(".social-cta-overlay").hide() 

,但没有任何反应和箱仍然存在。

回答

9

该脚本有3个问题(首先是最差):

  1. @require指令是外the metadata block。这意味着Greasemonkey忽略它只是一个普通的javascript评论。
  2. 没有@include@match指令。这意味着脚本将在每个站点的每个页面和iframe上触发! (破坏其中的一些,并咀嚼资源。)
  3. @grant none意味着the script will interfere with and crash all or part of many sites

此脚本工作:

// ==UserScript== 
// @name  hide-guardian-ad 
// @include  http://www.theguardian.com/* 
// @description Hides the Guardian social media frame 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js 
// @grant  GM_addStyle 
// ==/UserScript== 

$(".social-cta-overlay").hide() 
+1

非常好,谢谢。我认为'GM_addStyle'的确如此:给脚本权限添加样式*仅* *? (我在[文档](http://wiki.greasespot.net/@grant)中找不到该特定选项)。 – Jacob

+1

是的。这里是[GM_addStyle的文档](http://wiki.greasespot.net/GM_addStyle)。在几乎所有情况下授予它都是无害的,并且这样做会重新打开沙盒。 –

+0

最后不应该有分号吗?无论如何,这对我来说不适用于theguardian.com的顶部横幅:/我还没有找到任何方法来摆脱它。 – paradroid

0

试试这个:

$(document).find(".social-cta-overlay").hide(); 
+2

你可以从对方的回答看,这是不正确的,因为我的剧本有不少问题。 – Jacob

相关问题