javascript 如何覆盖 window.open 功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9172505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:49:37  来源:igfitidea点击:

How to override the window.open functionality?

javascripthtml

提问by Sidharth Panwar

Let's say I have window.open (without name parameter), scattered in my project and I want to change the implementation so that wherever name is not specified I'll specify a default name.

假设我有 window.open(没有 name 参数),分散在我的项目中,我想更改实现,以便在未指定名称的任何地方指定默认名称。

What I want to do about this is hook my own method to window.open so that whenever window.open runs it'll internally call my own method which will then call window.open (with the name parameter).

我想要做的是将我自己的方法挂接到 window.open 以便每当 window.open 运行时,它都会在内部调用我自己的方法,然后调用 window.open (带有 name 参数)。

Is that possible through Javascript? Will there be any circular dependency issues in this i.e. window.open calling my custom function which in turn calling the window.open function again?

这可能通过Javascript吗?在这个 ie window.open 调用我的自定义函数然后又调用 window.open 函数时会不会有任何循环依赖问题?

P.s. In simple terms what I want to do is override the window.open functionality.

Ps 简单来说,我想做的是覆盖 window.open 功能。

回答by Ates Goral

To avoid circular calls, you need to stash away the original window.openfunction in a variable.

为避免循环调用,您需要将原始window.open函数隐藏在变量中。

A nice way (that doesn't pollute the global namespace) is to use a closure. Pass the original window.openfunction to an anonymous function as an argument (called openbelow). This anonymous function is a factory for your hook function. Your hook function is permanently bound to the original window.openfunction via the openargument:

一个不错的方法(不会污染全局命名空间)是使用闭包。将原始window.open函数作为参数传递给匿名函数(open在下面调用)。这个匿名函数是你的钩子函数的工厂。您的钩子函数window.open通过open参数永久绑定到原始函数:

window.open = function (open) {
    return function (url, name, features) {
        // set name if missing here
        name = name || "default_window_name";
        return open.call(window, url, name, features);
    };
}(window.open);

回答by mpd

I know this reply is a little late, but I felt that a more general solution may be helpful for other people (trying to override other methods)

我知道这个回复有点晚了,但我觉得更通用的解决方案可能对其他人有帮助(试图覆盖其他方法)

function wrap(object, method, wrapper){
    var fn = object[method];

    return object[method] = function(){
        return wrapper.apply(this, [fn.bind(this)].concat(
            Array.prototype.slice.call(arguments)));
    };
};

//You may want to 'unwrap' the method later 
//(setting the method back to the original)
function unwrap(object, method, orginalFn){
    object[method] = orginalFn;
};

//Any globally scoped function is considered a 'method' of the window object 
//(If you are in the browser)
wrap(window, "open", function(orginalFn){
    var originalParams = Array.prototype.slice.call(arguments, 1);
    console.log('open is being overridden');
    //Perform some logic
    //Call the original window.open with the original params
    orginalFn.apply(undefined, originalParams); 
});

回答by fardjad

P.s. In simple terms what I want to do is override the window.open functionality.

Ps 简单来说,我想做的是覆盖 window.open 功能。

var orgOpen = window.open;

window.open = function (...args) {
    alert("Overrided!"); 
    return orgOpen(...args); 
}

window.open("http://www.stackoverflow.com");