Target="_Blank" Link marker & toggler
-
Originally, I made a request over here: https://forum.vivaldi.net/post/354753
Then @barbudo2005 point me to a very useful extension, so I did some more research & hack together a userjs to get the job done the way I wanted it:
// ==UserScript== // @name Popup Link Toggler // @version 0.1 // @description Toggle target="_blank" link with SHIFT key // @include * // @grant none // ==/UserScript== (function(){ var findLink = function($node) { return (!$node || ($node.tagName === 'A')) ? $node : findLink($node.parentNode); }; var toggleLinksTargetBlank = function(isToggled) { var dataAttr = 'data-initial-target-blank'; var $links = document.getElementsByTagName('a'); var clickOpenInCurrentTab = function(clickEvent) { var $link = findLink(clickEvent.target); if ($link) { window.location.href = $link.getAttribute('href'); clickEvent.preventDefault(); } }; for (var i=0; i<$links.length; i++) { var $link = $links[i]; if (isToggled) { $link.setAttribute(dataAttr, $link.getAttribute(dataAttr) || ($link.getAttribute('target') === '_blank')); if ($link.getAttribute(dataAttr) === 'true') { $link.addEventListener('click', clickOpenInCurrentTab); } } if (($link.getAttribute(dataAttr) === 'true') === isToggled) { $link.removeAttribute('target'); } else { $link.setAttribute('target', $link.getAttribute('target') || '_blank'); } if (!isToggled) { if ($link.getAttribute(dataAttr) === 'true') { $link.removeEventListener('click', clickOpenInCurrentTab); } $link.removeAttribute(dataAttr); } } }; var needsToggle = function(e) { return e.shiftKey; }; if (document.documentElement.classList.contains('link-toggler')) { console.log('Behavior already loaded on website'); return; } document.documentElement.classList.add('link-toggler'); document.addEventListener('keydown', function(downEvent){ if (needsToggle(downEvent)) { toggleLinksTargetBlank(true); var onceListener = function(upEvent) { window.removeEventListener('blur', onceListener); document.removeEventListener('keyup', onceListener); toggleLinksTargetBlank(false); }; // Deactivate either on keyup or on blur to go to new window document.addEventListener('keyup', onceListener); window.addEventListener('blur', onceListener); } }); })(); var style = document.createElement('style'); style.innerHTML = "a[target=_blank] { cursor: alias !important; }"; document.head.appendChild(style);
ReadMe
-
This are not my work, I only combined 2 sources & altered 'em a bit. All credits should go to augnustin & koosmann.
-
You need to install Tampermonkey or similar extension to handle UserJS. Then create a new script with above code.
-
Hover pointer over popup link will show an Alias icon to inform you it will open link in new tab, holding SHIFT key will toggle it to open link in current tab instead. You can still open normal link in new tab with SHIFT + Click.
IMHO this is how Open link behavior should be for all modern web browser. Please upvote my request if you like this mod. Thanks for your support! -
-
@dude99 won't work as plain userscript too? Have to test this ^^
-
I used a script with ViolentMonkey to turn target="blank" to target="self", since there is an exploit which can be run on the former to send the browser to a faked version of a page. I can still open a page in a new tab by middle-clicking.
-
Ppafflick moved this topic from Modifications on