14 votes

Open external links on ~ in new tabs

I was missing this feature from Reddit and saw others were as well so I thought I would share a user script I created to solve this issue until it's added (if it's added)

// ==UserScript==
// @name         Tildes.net: Open external links in new tab
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Opens external links on tildes.net in a new tab
// @author       SleepyGary
// @match        https://tildes.net/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.querySelectorAll('a').forEach(el => {
        if (!el.href.includes('tildes.net') && el.href !== '') {
            el.target = "_blank";
        }
    });
})();

5 comments

  1. [2]
    Fires
    Link
    Very cool, thanks !

    Very cool, thanks !

    1 vote
    1. SleepyGary
      Link Parent
      FYI I had was missing a * in the @match condition that prevent it from working on anything but the frontpage.

      FYI I had was missing a * in the @match condition that prevent it from working on anything but the frontpage.

  2. [3]
    Dot
    Link
    Can someone walk me through how this works? Would love to learn! Is this javascript? and would I put this in an HTML file? Any explanation would be terrific.

    Can someone walk me through how this works? Would love to learn! Is this javascript? and would I put this in an HTML file? Any explanation would be terrific.

    1 vote
    1. tildez
      Link Parent
      it finds all links on the page document.querySelectorAll('a') then loops through them .forEach(el => { checks if the link's url isn't a tildes.net and not empty if (!el.href.includes('tildes.net')...

      it finds all links on the page

      document.querySelectorAll('a')

      then loops through them

      .forEach(el => {

      checks if the link's url isn't a tildes.net and not empty

      if (!el.href.includes('tildes.net') && el.href !== '') {

      if the statement is true (it is not a tildes and not an empty url), it sets the target to _blank (this tells your browser to open a new tab)

      el.target = "_blank";

      You'd use this in a userscript which is something used by a browser add-on that will have your browser run scripts on webpages. AFAIK Greasemonkey is the most popular tool to use for this.

      4 votes
    2. SleepyGary
      Link Parent
      In addition to what @tildez said, the stuff at the top in a comment is metadata about the script and the browser extension you use to run it also uses it to know when/how to run. @match...

      In addition to what @tildez said, the stuff at the top in a comment is metadata about the script and the browser extension you use to run it also uses it to know when/how to run. @match https://tildes.net/* is the pattern the extension matches against your current webpage, so this will only work on tildes pages. @grant none means the script is requesting no special privileges to run.

      3 votes