How to prevent a new page from opening in a new tab?

General project discussion.
Use this as a last resort if your topic does not fit in any of the other boards but it still on-topic.
Forum rules
This General Discussion board is meant for topics that are still relevant to Pale Moon, web browsers, browser tech, UXP applications, and related, but don't have a more fitting board available.

Please stick to the relevance of this forum here, which focuses on everything around the Pale Moon project and its user community. "Random" subjects don't belong here, and should be posted in the Off-Topic board.
User avatar
Piotr Kostrzewski
Lunatic
Lunatic
Posts: 259
Joined: 2018-08-14, 15:08
Contact:

How to prevent a new page from opening in a new tab?

Unread post by Piotr Kostrzewski » 2023-05-16, 11:05

Hi. I checked the settings but all the time when I click on a result in Bing it opens as a new tab and I would like it to stay as one tab. I would like to know which option should be selected in the settings.
Thank you very much.
Piotr

User avatar
jars_
Lunatic
Lunatic
Posts: 397
Joined: 2016-12-27, 00:12

Re: How to prevent a new page from opening in a new tab?

Unread post by jars_ » 2023-05-16, 11:31

Piotrek Ślusarz wrote:
2023-05-16, 11:05
... which option should be selected in the settings.
There is no such option. You need Greasemonkey extension and script Open links in current tab

Code: Select all

// ==UserScript==
// @name          Open links in current tab
// @author        wOxxOm
// @description   Open links in current tab regardless of _target or site's preferences.
// @namespace     http://target._blank.is.retarded
// @version       2.2.7
// @include       *
// @exclude             http*coub.com/*
// @exclude             http*vk.com/*
// @exclude             http*.youtube.com/*
// @run-at        document-start
// @grant         GM_openInTab
// ==/UserScript==

//
// Ctrl-click: background tab,
// Ctrl-Shift-click: foreground tab,
// Shift-click: new window, 
// Alt-click: force open in current tab
//

if (window == top) {
    window.addEventListener('message', function(e) {
        // some stupid sites choke on object data
        if (!/^\{/.test(e.data)) return;
        let data = tryParse(e.data);
        if (data.name == GM_info.script.name) navigate(data.url);
     });
   }

   let suppressing, clickedElement;

window.addEventListener('mousedown', function(e) { clickedElement = e.target; }, true);

window.addEventListener('mouseup', function(e) {
//
// Убрал действие на СКМ только ПКМ отслеживается.
//    if (e.button > 1 || e.target != clickedElement) return;
        if ( !e.button || e.target != clickedElement ) return;

    let btn = e.button, mod = (e.ctrlKey || e.altKey || e.shiftKey ) ? true : false,
         link = pierceShadow(e) ? pierceShadow(e) : false,
         linkjs = link ? (link.getAttribute('href') || '').match(/^(javascript|#|$)/) : false;

        if ( !link || linkjs || !document.contains(link) ) return;


   if (btn == 1 && mod ) GM_openInTab(link.href);
    else if (window.chrome && b === 0 && mod) link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
      else if (!mod) {
        if (link.target == '_blank') {
            link.target = '';
            blockWindowOpenAndMutations(link);
            return; 
          } else return;

    suppressing = true;
    prevent(e);
    }

 }, true);

window.addEventListener('click', prevent, true);
window.addEventListener('auxclick', prevent, true);

function prevent(e) {
    if (!suppressing) return;
    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();
    setTimeout(function() {
        suppressing = false;
    }, 100);
}

function blockWindowOpenAndMutations(link) {
    let observer = new MutationObserver(function() {
        if (link.target == '_blank') {
            link.removeAttribute('target');
            console.log('[Open links in current tab] prevented dynamic target=_blank for', link.href);
            navigate(link.href);
        }
    });
    observer.observe(link, {attributes:true, attributeFilter:['target'], characterData:true});

    let _open = unsafeWindow.open;
    let timeout = setTimeout(function() {
        unsafeWindow.open = _open;
        observer.disconnect();
    }, 50);

    unsafeWindow.open = exportFunction(function(url, name, features) {
        if (!features) {
            console.log('[Open links in current tab] prevented window.open for', url);
            navigate(link.href);
        } else {
              _open(url, name, features);
              unsafeWindow.open = _open;
              clearTimeout(timeout);
             }
    }, unsafeWindow);
}


function pierceShadow(e) {
    let el = e.target;
       while (el.shadowRoot) el = el.shadowRoot.elementFromPoint(e.clientX, e.clientY);
    return el.closest('a');
   }

function navigate(url) {
    if (window == top) {
        link = document.createElement('a');
        link.href = url;
        link.dispatchEvent(new MouseEvent('click'));
    } else { top.postMessage(JSON.stringify({name: GM_info.script.name, url: url}), '*'); }
}

function tryParse(str) {
    try { return JSON.parse(str); }
    catch(e) {}
}






Blacklab
Board Warrior
Board Warrior
Posts: 1080
Joined: 2012-06-08, 12:14

Re: How to prevent a new page from opening in a new tab?

Unread post by Blacklab » 2023-05-16, 12:04

Perhaps worth having a look at Bing settings too? Never 'signed-into' Bing here... so unsure if you can configure settings further within?

In DuckDuckGo there is an 'Open Links in New Tab - On/Off' option under 'All Settings > General'...which can then be variously saved in cloud, as a cookie, as a bookmarklet, or most simply as a URL string. :)

User avatar
Piotr Kostrzewski
Lunatic
Lunatic
Posts: 259
Joined: 2018-08-14, 15:08
Contact:

Re: How to prevent a new page from opening in a new tab?

Unread post by Piotr Kostrzewski » 2023-05-16, 12:14

Blacklab wrote:
2023-05-16, 12:04
Perhaps worth having a look at Bing settings too? Never 'signed-into' Bing here... so unsure if you can configure settings further within?

In DuckDuckGo there is an 'Open Links in New Tab - On/Off' option under 'All Settings > General'...which can then be variously saved in cloud, as a cookie, as a bookmarklet, or most simply as a URL string. :)
You're right, I didn't look into Bing settings, somehow I didn't think about it. By the way, do you recommend DuckDuckGo?

Blacklab
Board Warrior
Board Warrior
Posts: 1080
Joined: 2012-06-08, 12:14

Re: How to prevent a new page from opening in a new tab?

Unread post by Blacklab » 2023-05-16, 12:20

Personally, DuckDuckGo (DDG) suits me... I've used it since it first began... so Yes, I would happily recommend it. :D

I particularly like being able to save my own DDG search settings as a simple URL string... which I save as my homepage on Pale Moon (and all other browsers I use). I also like the DDG option to have 'continuous/infinite scroll' for all the results pages. The search privacy aspect is also very important to me.

DDG uses a lot of Bing results AFAIAA... and other sources... but if looking for something very obscure or very recent I still check Google as the 'evil one' probably has the best crawlers in the business... so may have crawled more recently than other search engines?

User avatar
Piotr Kostrzewski
Lunatic
Lunatic
Posts: 259
Joined: 2018-08-14, 15:08
Contact:

Re: How to prevent a new page from opening in a new tab?

Unread post by Piotr Kostrzewski » 2023-05-16, 12:33

Thank you very much, I will try DDG :)

Locked