Greasemonkey,覆盖网站功能
我已经读了很多书,并且已经尝试了大约 5 个小时来完成这项工作...所以
我想编写一个脚本来重写函数 dummy() {$.ajax(...) }; 在网站上。
这是我尝试执行此操作的方法
unsafeWindow.dummy = function(data){differantFunction(); $.ajax(...);};
function differantFunction(){
...
}
,但是原本会被调用以在原始页面上执行某些操作的虚拟函数...现在什么也不执行。
//更新
我尝试运行该函数,我试图覆盖地址栏以查看出了什么问题:(javascript:dummy("..");)
,我收到一条错误消息,告诉我 $ 未定义,但我打开了 jquery网站和用户脚本中......我现在很迷失
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为脚本在 GM 范围内运行。
如果您不使用任何 GM 函数(例如
GM_setValue
或GM_xmlhttpRequest
),我建议您执行以下操作:将代码编写为普通脚本,而不是 GM 脚本.
我的意思是,删除所有
unsafeWindow
引用和相关内容。这将使脚本在正确的范围内运行。
但是如果您使用 GM 函数,那么您需要在正常范围内的每个变量(例如
$
)之前添加unsafeWindow
或执行类似以下操作并祈祷使其正常工作:PS.: E4X 的多行字符串 是不再支持。其他一些选项是:
1)将代码添加到函数中,然后使用
Function.prototype.toString< br>
2) 将代码创建为单独的文件,然后将其添加为
资源
3) 在每个末尾添加一个反斜杠
线
This happens because the script is running in GM scope.
If you don't use any GM function (like
GM_setValue
orGM_xmlhttpRequest
), I recommend you to do the following: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: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