简体   繁体   中英

Prevent opening links in a new window (stay in current)

I need to force all links on a page to open in the same window. Is there a reliable userscript that does this?

It would be applied to a web app that handles links using JS. The code is complex (frameworks + proprietary) and I am not proficient enough to follow it and write a tailor-made script that achieves this.

Already searched for solutions in SO, script reposetories and Chrome plugins, but all I could find is popup blockers. However, I need to open the page and keep it in the same window ( target="_self" if it was plain HTML)

If they aren't explicitly setting a target="_blank" , they're probably using window.open() . You could try overriding this.

First, check if this is what they're doing by doing this:

(() => {
  const originalWindowOpen = window.open;
  window.open = (...args) => {
    console.log('window.open called with', args);
    originalWindowOpen.apply(window, args);
  };
})();

If you get something spit out in your console, it means that's what they're using, and you should be able to use this to do what you want (double check the first parameter output in the args is the URL, it should be):

(() => {
  const originalWindowOpen = window.open;
  window.open = (...args) => {
    console.log('window.open called with', args);
    location.href = args[0];
  };
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM