0

I am writing a userscript for a website to change link targets from "_blank" to "_self".

The website's links are all handled by an EventListener on Click action, which reviews the id attribute for every <a> element to set the click URL and load it in a new tab. None of the <a> elements have a href or target attribute.

I would like all clicked links to load in the same tab (rather than a new tab).

Is there any way to modify link targets set by an event listener, or to simply set/override the default link target for all links on a webpage?

jm92
  • 137
  • 10
  • 1
    Anchors _wouldn't_ have a `src` attribute. `href` maybe? – Andy Nov 17 '22 at 15:04
  • Sorry about that, fixed. Yes, `href` not `src` (`href` is just set to `javascript:void` for every link on the page) – jm92 Nov 17 '22 at 15:07
  • Can we see some code so I can understand your case? – Moussa Bistami Nov 17 '22 at 15:10
  • The website's code is sandboxed, which is what's making this pretty difficult. But I was able to use inspector to see an EventListener on Click, which is what's setting the link behavior for the page. – jm92 Nov 17 '22 at 15:19

3 Answers3

1

Provided you want to override the behavior of links that open in a new tab, you could override window.open as such:

window.open = (open => (href => open.call(window, href, '_self')))(window.open);

For anchor tags with an explicit target of _blank, you could theoretically override the click method to force the target:

window.HTMLAnchorElement.prototype.click = function() { window.open(this.href, '_self') };

However, this is in fact a hacky solution and might not be the best idea to use in production.

skara9
  • 4,042
  • 1
  • 6
  • 21
  • The first line is exactly what I would be looking to do (override the default behavior of window.open). Hacky is fine, as this is just for myself and a few colleagues. But even after applying the code, the links still open in a new tab. I tried it on a vanilla page with just a link and eventListener, and it worked totally fine (opened in self), so I'm guessing the Click eventListener on this other website must call some custom link opening function? – jm92 Nov 17 '22 at 16:55
  • @jm92 Look at the page's js code and ensure it's using `open`. If it indeed is, you should verify that the `window.open` in the page's context is your overriden version of the method. – skara9 Nov 17 '22 at 17:17
  • Marked your answer correct, as it's exactly what I needed. The JS code is massive, but is there any way I can use inspector/console to display which function is called upon click? I'm having trouble deciphering that by reading through EventListeners in inspector. – jm92 Nov 17 '22 at 17:20
  • @jm92 Maybe look at https://stackoverflow.com/questions/446892 – skara9 Nov 17 '22 at 19:07
  • Thank you! I found another question, which combined with your answer, solved the issue. Userscripts are separated from the target page, so re-declaring the open function was not enough to overwrite the default global open function. The function from this answer allowed me to inject it into the page: https://stackoverflow.com/questions/21271997/how-to-overwrite-a-function-using-a-userscript – jm92 Nov 18 '22 at 13:25
0

you can do select your element with document.querySelector().target = "_self"

Farbod Shabani
  • 422
  • 4
  • 9
0

EDIT Jan 12, 2023: there is a much better and easier way to do this, if you're using Tampermonkey.

GM_addElement allows you to do exactly what I described in the request, to insert code into actual page:

https://www.tampermonkey.net/documentation.php#api:GM_addElement

I used the script version, to add a function (you have to enclose it in quotes, like text).

This solution works much better with stricter websites and browsers, so you won't get CSP errors.

OLD ANSWER:

The answer to this question solved my issue:

How to overwrite a function using a userscript?

I re-wrote the open function from skara9's answer, and then inserted it with the addJS_Node function from the above question.

Userscripts are separated from the target page, hence why simply re-declaring the function was not overwriting the default global open function.

jm92
  • 137
  • 10