2014-02-25 19 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

相關問題