2013-03-26 74 views
1

場景:在BBC直播體育報道中編寫一個Greasemonkey腳本,用於隱藏Twitter評論(由class class爲TWEET的列表元素表示)。頁面刷新後Greasemonkey快照過期了嗎?

我已經成功地得到一些東西,大致的工作原理(下警告),使用waitForKeyElements:

// ==UserScript== 
// @name  myName 
// @namespace none 
// @description myDescription 
// @include  http://www.bbc.co.uk/* 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js 
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js 
// @version  2.0 
// @grant  none 
// ==/UserScript== 

function doHousekeeping(jNode) { 

//Find all tweet items within the commentary, and hide them 
var snapResults = document.evaluate(".//div[@id='live-event-text-commentary']/ol/li[contains(@class,'class-TWEET')]", document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
for (var i = snapResults.snapshotLength - 1; i >= 0; i--) { 
     snapResults.snapshotItem(i).style.display="none"; 
    } 
} 

// Cleanup on initial load 
doHousekeeping(); 

// Repeat cleanup whenever the main body changes 
waitForKeyElements ("#live-event-text-commentary", doHousekeeping); 

問題:當頁面刷新,doHousekeeping 執行(和隱藏的違規物品);然而,最近的一些違規項目仍然保留在頁面頂部。

爲什麼它沒有隱藏所有這些?我的懷疑是文檔/快照在某種程度上在主體更改時未被刷新。另外我是jQuery的新手,所以我知道我的管家功能既是老派又是次優;任何幫助清潔,全功能的實施將不勝感激。

回答

1

問題似乎是您要隱藏的內容與給予waitForKeyElements的選擇器之間存在不匹配。

在這種情況下使用waitForKeyElements正確的方法是這樣的:

// ==UserScript== 
// @name  myName 
// @namespace none 
// @description myDescription 
// @include  http://www.bbc.co.uk/* 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js 
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js 
// @version  2.0 
// @grant  GM_addStyle 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
    introduced in GM 1.0. It restores the sandbox. 
*/ 

//-- Find all tweet items within the commentary, and hide them. 
waitForKeyElements (
    "#live-event-text-commentary ol li.class-TWEET", 
    doHousekeeping 
); 

function doHousekeeping (jNode) { 
    jNode.hide(); 
} 


另外,不要使用@grant none,因爲這可能會導致片狀的副作用。

+0

完美,謝謝!我知道我在這個問題上投入了太多的複雜性。 – dustlined 2013-03-27 08:15:46