gpt4 book ai didi

javascript - 在调用之前从外部脚本覆盖对象函数

转载 作者:行者123 更新时间:2023-11-30 18:10:26 25 4
gpt4 key购买 nike

我正在尝试编写 Greasemonkey 脚本或 Userscript 来修复网站中的错误,该错误处理对象的错误功能。我可以通过在生成函数原型(prototype)的代码行添加断点并使用 js 控制台手动覆盖函数原型(prototype)来手动修复问题。但是,我认为没有任何方法可以用代码来做到这一点。外部脚本加载在 html 主体的末尾,但问题代码在同一脚本中执行。

有什么方法可以在页面中注入(inject)javascript代码,这样当这个Ft.prototype.J被创建时,我可以立即修改它吗?问题是代码也被混淆和缩小了,所以我不确定它的一半是做什么的。

这是代码的基本概要:

//What does this do?????
function A(a, b) {
function c() {}
c.prototype = b.prototype;
a.f = b.prototype;
a.prototype = new c;
a.prototype.constructor = a
}

function Ft(a, b) {
$.call(this, b);
//some stuff
}
//doing something with jQuery?
A(Ft, $);
Ft.prototype.J = function (a) {
//modifies the DOM content
};
//Code soon after that calls some object.J

如果我将代码行 Ft.prototype.J = function() {}//my own function 添加到我的 greasemonkey 脚本中,它会按预期返回错误, Ft 未定义。但是,如果我在加载结束时执行该行,损坏的函数已经运行,并且 DOM 已经被感染。

谢谢。

最佳答案

我认为您可以使用 getter/setter 和 Object.defineProperty 来施展魔法,因为该函数是全局声明的:

(function() {
var Ft;
function myJ() {
// do whatever YOU want to do
}
Object.defineProperty(window, "Ft", { // use unsafeWindow in GM?
configurable: true,
enumerable: true,
get: function() { return Ft; },
set: function(n) {
// Hah, we've catched the function declaration!
Ft = n;

// just making it non-writable would lead to an exception
Object.defineProperty(Ft.prototype, "J", {
get: function() { return myJ; },
set: function() { /* ignore it! */ }
});
}
});
})();

现在,如果有人执行您在问题中发布的代码, setter 将被调用,您可以根据需要使用这些值。

关于javascript - 在调用之前从外部脚本覆盖对象函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14727262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com