将执行 document.write 的函数替换为另一个函数
我有以下脚本标记,其中函数 A() 执行 document.write()。我无权访问 A(),因为它来自第三方脚本。
//block1
<script type="text/javascript">
A();
</script>
我没有任何 id/class 钩子到 block1,但我可以像这样在 A() 之前引入函数调用。
<script type="text/javascript">
B();
A();
</script>
我希望 B() 函数将 block1 替换为
<script type="text/javascript">
C();
</script>
// or keep as is
<script type="text/javascript">
A();
</script>
这可能吗?我应该如何处理?
I have following script tag where a function A() does a document.write(). I don't have access to A() since it's from third part script.
//block1
<script type="text/javascript">
A();
</script>
I don't have any id/class hook to block1 but I can introduce a function call before A() like this.
<script type="text/javascript">
B();
A();
</script>
I want B() function to replace block1 to
<script type="text/javascript">
C();
</script>
// or keep as is
<script type="text/javascript">
A();
</script>
Is this possible and how should I go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以在定义
A()
之后、调用它之前插入代码(可能不可能),那么您可以像定义任何代码一样重新定义A()
JavaScript 中的其他函数。如果不可能,您可以在调用
A()
之前重新定义document.write
,然后在调用A()
后撤消该操作。例如:If you can insert code after
A()
is defined, but before it is called (might not be possible), then you can redefineA()
like you would define any other function in JavaScript.If that is not possible, you can redefine
document.write
beforeA()
gets called, and then undo that afterA()
gets called. For example:也许你可以给 A 打猴子补丁?
这会将所有以后对 A 的调用更改为调用 C。这可能是也可能不是您想要的。
Perhaps you can monkey-patch A instead?
This will change all future calls to A to call C instead. This might or might not be what you want.