External Links In New Tab

License Latest Stable Version Total Downloads Donate here

A Flarum extension. Opens all external links in a new tab

Adds target="_blank" rel="noopener noreferrer" to every external link on the forum frontend (excluding the admin frontend). As of version 1.1.0, it no longer overrides existing rel attributes, but adds the noopener noreferrer if not found in them (Thanks to @clarkwinkelmann for pointing that out here).

Installation

Install with composer:

composer require walsgit/external-links-in-new-tab:"*"

Updating

composer update walsgit/external-links-in-new-tab:"*"
php flarum migrate
php flarum cache:clear

Links

    Walid I checked out your code and have a remark and a few suggestions.

    First, I see the logic checks for the hostname being anywhere in the URL, so this means a link like https://external.tld/page?domain=flarum.tld will not open externally. You could use the URL javascript object to parse the href value and compare if the .host or .origin values match instead of String.contains().

    Second I saw some redundant code. The check for /admin should not be necessary because you only add the code to the forum bundle. Same for element instanceof HTMLAnchorElement, I'm pretty sure it's redundant because the query selector a already enforces it.

    And a note to users of the extension, the way it's implemented will take precedence over anything defined by another extension. Any extension defining a target or rel attribute at the TextFormatter level will be overridden. The extension will also strip the rel=ugc attribute that Flarum sets by default on user-generated content.

      clarkwinkelmann Thank You for your feedback! I'll try to correct/improve this asap.
      For the /admin part... yeah I should have thought of that 🤦‍♂️ 😆 .

      clarkwinkelmann

      Version 1.1.0

      • no longer overrides rel attribute but adds noopener noreferer to its values only if it doesn't find them
      • refactored code & removed redundancies

      Thanks again! hope it's better now.

      4 months later

      I find this extension ignores sub directories and focuses on root domain, as opposed to flarum's internal pages. For example, my flarum forum is on mydomain.com/discussion. My phpbb forum is on mydomain.com/community. Attempts to open new tab on "community" fail.

      Any way to address this?