Greasemonkey,每秒替换每次出现的字符串

时间:2013-10-03 22:21:56

标签: javascript greasemonkey

我试图找出一个greasemonkey脚本,它用ondblclick替换站点上的每个onmousedown。我想让它不断更新,就像每1.5秒一样,因为页面使用AJAX刷新。

这是我提出的脚本,但它似乎不起作用。

window.setInterval(document.body.innerHTML= document.body.innerHTML.replace('onmousedown','ondblclick');,1500);

它应该使用的页面仅供内部使用。但是一个很好的例子就是google搜索,其中onmousedown用于结果的链接,在你点击它之前换掉URL。

我还在document.body.innerHTML.replace之后没有使用分号进行了尝试。

我是JavaScript的新手,但由于我是公司中唯一一个可以编码的人,所以这个问题一直困扰着我。

任何帮助都将不胜感激。

另外,一个小的“边题” 我是否必须使用@exclude,或者只使用@include internal.companysite.tld *它是否只适用于此网站?

1 个答案:

答案 0 :(得分:-1)

直接回答:你需要为setInterval提供一个函数 - 最好设置一个变量,以便稍后可以使用clearInterval()取消它。

function myF(){document.body....;}
var myIntv = setInterval(myF, 1500);

你也可以在一行中使用匿名函数来做这件事......这样做:

var myIntv = setInterval(function(){document.body....;}, 1500);

我不建议将此作为解决问题的方法。你想要做的是操纵活跃的DOM - 而不是真正改变UI。你可能需要这样的东西:

var objs = document.getElementsBy__(); // ById/ByName/etc - depends on which ones you want
for (var i in objs){objs[i].ondblclick = objs[i].onmousedown;objs[i].onmousedown = undefined;} // just an example - but this should convey the basic idea

更好的是,如果你可以使用jQuery,那么你将能够更容易地选择合适的节点并以更易于管理的方式操纵事件处理程序:

$(".class.for.example").each(function(){this.ondblclick = this.onmousedown;this.onmousedown = undefined;}); // just an example - there are multiple ways to set and clear these