繁体   English   中英

Greasemonkey快照在页面刷新后过时了吗?

[英]Greasemonkey snapshot outdated after page refresh?

场景:编写一个Greasemonkey脚本,在BBC直播体育报道中隐藏Twitter注释(由类别为“ class-TWEET”的列表元素指示)。

我已经成功地使用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的新手,所以我知道我的整理功能既过时又不太理想。 对于更清洁,功能齐全的实现的任何帮助,将不胜感激。

问题似乎是您要隐藏的内容和指定给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 ,因为这会导致片状副作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM