Greasemonkey,覆盖网站功能 [英] Greasemonkey, overriding website functions

查看:115
本文介绍了Greasemonkey,覆盖网站功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读很多东西,并且一直试图把它完成大约5个小时......所以这里是

我想写一个脚本,它将覆盖函数dummy(){$ .ajax(...)};
在一个网站上。



这里是我如何做到这一点

  unsafeWindow.dummy = function(data){differantFunction(); $阿贾克斯(...);}; 

函数differantFunction(){
...
}

,但是虚拟函数会被调用来在原始页面上执行某些操作......现在只是没有任何操作。



// update

>

我试着运行那个函数,我试图重写槽的adres栏,看看有什么不对:(javascript:dummy(..);)



我得到一个错误告诉我$是未定义的,但我在网站和用户脚本中有jquery ...我现在很迷路

如果您不使用任何GM函数(如 GM_setValue 或 GM_xmlhttpRequest ),我建议您执行以下操作:

  var script = document.createElement('script'); 
script.type =text / javascript;
script.innerHTML =(<<![CDATA [

//您的代码开到这里

]]>< /> )的ToString();
document.getElementsByTagName('head')[0] .appendChild(script);

将代码写成普通脚本,而不是GM脚本。

我的意思是,删除所有 unsafeWindow 引用和相关内容。

这将使脚本运行在正确的范围内。



但是如果你使用GM函数,那么你需要在正常范围内的每个变量之前添加 unsafeWindow (比如
$ b pre $ code $ $ unsafeWindow。$ / code $) $;
// ...

PS。: 多行字符串与E4X 不再支持。其他一些选项是:

1)将您的代码添加到函数中,然后使用
2)创建你的代码作为一个单独的文件,然后将其添加为
资源


3)在每个
行的末尾添加反斜杠


i've been reading a lot and have been trying to get this done for about 5 hours now... so here it is

I want to write a script that will override a function dummy() {$.ajax(...)}; on a website.

here is how i'm trying to do it

unsafeWindow.dummy = function(data){differantFunction(); $.ajax(...);};

function differantFunction(){
...
}

but the dummy function that would have been called up to do something on the original page... now just does nothing.

//update

I tried running that function i'm trying to override trough the adres bar to see what's wrong: (javascript:dummy("..");)

and I get an error telling me $ is undefined but I have jquery on the website and in the userscript... i'm so lost right now

解决方案

This happens because the script is running in GM scope.
If you don't use any GM function (like GM_setValue or GM_xmlhttpRequest), I recommend you to do the following:

var script = document.createElement('script'); 
script.type = "text/javascript"; 
script.innerHTML = (<><![CDATA[

// YOUR CODE GOES HERE

]]></>).toString();
document.getElementsByTagName('head')[0].appendChild(script);

Write the code as a normal script, not a GM script.
I mean, remove all unsafeWindow references and related stuff.
This will make the script to run in the correct scope.

BUT if you use GM functions, then you will need to add unsafeWindow before every variable in normal scope (like $) or do something like the following and pray to make it work:

$ = unsafeWindow.$;
//...

PS.: Multiline string with E4X is not supported anymore. Some other options are:
1) add your code into a function and then use Function.prototype.toString
2) create your code as a separate file and then add it as a resource
3) add a backslash at the end of each line

这篇关于Greasemonkey,覆盖网站功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆