# "Open all external links in a new tab" does not work when you select some text

**URL:** https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703
**Category:** Bug
**Tags:** pr-welcome
**Created:** [December 22, 2022, 10:46pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703 "2022-12-22T22:46:41Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [December 22, 2022, 10:46pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/1 "2022-12-22T22:46:41Z")

</div>

1. Make sure that you have _ **Open all external links in a new tab** _ option checked in `/my/preferences/interface` _Other_ interface options (It seems to be the default setting, anyway)
2. Find any post with an external link – random examples in [meta](https://meta.discourse.org/t/add-a-table-to-your-post-using-markdown/66544/4) and in [try](https://try.discourse.org/t/favorite-tv-show-scene/267?u=jesus2099)
3. **Click the first external link you find in that post** (preferably the same)

It opens in a new tab, good. :+1:

**BUT:**

1. Make sure that you have _ **Open all external links in a new tab** _ option checked in `/my/preferences/interface` _Other_ interface options (It seems to be the default setting, anyway)
2. Open same post as previous test
3. **Select any text** in this post (1+ letter, 1+ word, any selection will do) – You should see a floating _ **Quote** _ button
4. **Click the first external link you find in that post**

:no_entry: :see_no_evil: :warning: **Now the external link open in current tab, instead of new tab.** :warning: :see_no_evil: :no_entry:

* * *

:information_source: Actually you can simply try with this post here:

1. Click [THIS EXTERNAL LINK](https://discourse.org) – OK good :+1: new tab
2. Select THIS TEXT
3. Click the step 1 external link, again – NG :no_entry: current tab

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 22, 2022, 11:13pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/2 "2022-12-22T23:13:31Z")

</div>

I can confirm. I’m not entirely sure it’s worth fixing, as it seems like an edge case, where the user is asking for trouble.

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [December 22, 2022, 11:33pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/3 "2022-12-22T23:33:10Z")

</div>

Because you think it might be tricky to fix?

I often click. I often select text.  
So it bugs me very often.

I did mention this bug long time ago but I did not explain it as _well_ as this time. :wink:

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 24, 2022, 9:40am UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/4 "2022-12-24T09:40:31Z")

</div>

> [@jesus2099](#):
>
> it bugs me very often.

That is annoying.

If you don’t have the skills to submit a PR (I don’t think I do) then just using control-click to open in a new tab will work, isn’t hard to automatize, and will solve the problem on every site, even those that don’t open links in new tabs.

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [December 25, 2022, 1:10am UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/5 "2022-12-25T01:10:12Z")

</div>

I would use Ctrl+click but we have this nice option, called _Open **all** external links in a new tab_.

But what this option relay does is _Open **random** external links in a new tab_.

It took me months, if not years, to pinpoint a reproduction scheme.  
It felt really random to me.

Maybe remove this option, for a consistent behaviour?

I remember trying to setup a Discourse dev environment for a simple CSS issue PR.  
But, if I remember correctly, I gave up because my PC was too old and I could not install or run all requirements.

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [December 26, 2022, 11:45am UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/6 "2022-12-26T11:45:17Z")

</div>

I found back my previous [failed attempt at installing a Discourse dev environment](https://meta.discourse.org/t/easy-development-environment-creation/43306).  
My personal Linux PC was too old and my professional Windows laptop had no _hyper threading_ activated.

My new professional laptop is now apparently _hyper threading_\* so I will see if I can follow [the new instructions](https://meta.discourse.org/t/discourse-development-contributor-guidelines/3823), without admin rights.

\* `wmic CPU Get NumberOfCores,NumberOfLogicalProcessors` shows more logical cores than physical cores.

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [December 28, 2022, 8:00pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/7 "2022-12-28T20:00:24Z")

</div>

Hello,

I think it can _fixable_ with CSS.

```scss
.cooked a {
  @include user-select(text);
}

```

I hope it helps, I haven’t really tested it yet but seems works fine. :slightly_smiling_face:

Edit: `user-select: text` should added to the link because not only the `.cooked` selected text affect it but every selected text on the site. I changed the code.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 28, 2022, 8:40pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/8 "2022-12-28T20:40:56Z")

</div>

That looks like magic to me. I’d like to know how it works, can you explain this trick? :hugs:

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [December 29, 2022, 1:41am UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/9 "2022-12-29T01:41:56Z")

</div>

Awesome sleuthing Don, would you like to submit a PR?

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [December 29, 2022, 12:49pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/10 "2022-12-29T12:49:20Z")

</div>

Thanks Sam, I just testing it more closely before submitting and it seems this is not enough unfortunately. This solution only works with Chrome.

---

<div class="post-metadata">

### Author: ![Aleksey\_Bogdanov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aleksey_bogdanov/32/288040_2.png) [@Aleksey\_Bogdanov](https://meta.discourse.org/u/Aleksey_Bogdanov)
#### Post date: [January 9, 2023, 1:28pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/11 "2023-01-09T13:28:45Z")

</div>

Here’s the piece of code causing the bug (from app/assets/javascripts/discourse/app/lib/click-track.js)

```javascript
    // Cancel click if triggered as part of selection.
    const selection = window.getSelection();
    if (selection.type === "Range" || selection.rangeCount > 0) {
      if (selectedText() !== "") {
        return true;
      }
    }

```

Removing this code solves the problem and doesn’t appear to break tests (I have some tests failing before and after the change for some reason but no new ones apparently).  
So I’m wondering whether this code does anything useful because one thing it sure doesn’t do is what it says it does, otherwise the bug reporter would experience no link follow at all upon ‘select + click’ but that’s not his complaint.  
Can someone share what this code is supposed to accomplish?

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [January 9, 2023, 2:07pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/12 "2023-01-09T14:07:42Z")

</div>

Maybe it’s made to prevent opening links in new tabs when just selecting some text to quote our copy?

When the code you found is deactivated and when you have the “Open all external links in a new tab” option checked,

Could you try to select (mouse down, drag, mouse up) some text that includes links to see if it does not open any of them?

Try with a selection that surrounds links and also try with a selection that ends in the middle of a link, maybe.

The selection should always just select, not open links.

---

<div class="post-metadata">

### Author: ![Aleksey\_Bogdanov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aleksey_bogdanov/32/288040_2.png) [@Aleksey\_Bogdanov](https://meta.discourse.org/u/Aleksey_Bogdanov)
#### Post date: [January 9, 2023, 2:27pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/13 "2023-01-09T14:27:21Z")

</div>

Yes, such selections work as expected: without following links or opening them in new tabs.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [January 10, 2023, 4:13am UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/14 "2023-01-10T04:13:43Z")

</div>

I vaguely recall there was a workaround for a specific browser, maybe try in Safari/Firefox/Chrome with a selection to see if any trigger it?

---

<div class="post-metadata">

### Author: ![Aleksey\_Bogdanov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aleksey_bogdanov/32/288040_2.png) [@Aleksey\_Bogdanov](https://meta.discourse.org/u/Aleksey_Bogdanov)
#### Post date: [January 10, 2023, 10:07am UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/15 "2023-01-10T10:07:03Z")

</div>

Works on Firefox, Chrome. How should I try Safari on Ubuntu?

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [January 10, 2023, 12:06pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/16 "2023-01-10T12:06:12Z")

</div>

> [@Aleksey\_Bogdanov](#):
>
> Works on Firefox, Chrome […] on Ubuntu

Is your dev server accessible on your wi-fi so that you could also test with your smartphone?

Maybe the touch screen finger drag events are triggering the open links.

You could add this smartphone browser (Firefox, Chromium-based or Safari) to your tested browser list. :smiling_face_with_sunglasses:

And, but the way, thank you so very much for your work on this bug!!

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [January 10, 2023, 12:12pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/17 "2023-01-10T12:12:50Z")

</div>

I found this reason for this cancel code, apparently:

[https://github.com/discourse/discourse/pull/2795](https://github.com/discourse/discourse/pull/2795)

> Prevent a click if the user select content in a topic and **release the mouse over a link.**
> 
> [https://meta.discourse.org/t/selecting-a-link-results-in-it-being-opened/14846](https://meta.discourse.org/t/selecting-a-link-results-in-it-being-opened/14846)  
> [deleted topic]

[Interesting comment](https://github.com/discourse/discourse/pull/2795#issuecomment-55485349):

> I only have access to windows platform for testing.
> 
> this fix can be tested easily as the file can be pasted as a whole into the console.

@sam you know which file we should paste in the console after loading a topic page?  
@Aleksey_Bogdanov could give that file and I could paste it in my Windows Vivaldi, Firefox, Android 9 Kiwi browser consoles, and you could paste it in your iOS, Mac OS Safari.

---

<div class="post-metadata">

### Author: ![Aleksey\_Bogdanov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aleksey_bogdanov/32/288040_2.png) [@Aleksey\_Bogdanov](https://meta.discourse.org/u/Aleksey_Bogdanov)
#### Post date: [January 10, 2023, 1:19pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/18 "2023-01-10T13:19:47Z")

</div>

> [@jesus2099](#):
>
> this fix can be tested easily as the file can be pasted as a whole into the console.

I don’t think it works like that any more.

---

<div class="post-metadata">

### Author: ![Aleksey\_Bogdanov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aleksey_bogdanov/32/288040_2.png) [@Aleksey\_Bogdanov](https://meta.discourse.org/u/Aleksey_Bogdanov)
#### Post date: [January 10, 2023, 3:11pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/19 "2023-01-10T15:11:27Z")

</div>

No problem detected in Chrome and Firefox on Android although I struggle to select in Firefox as liberally.

---

<div class="post-metadata">

### Author: ![jesus2099](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jesus2099/32/119720_2.png) [@jesus2099](https://meta.discourse.org/u/jesus2099)
#### Post date: [January 10, 2023, 4:05pm UTC](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703/20 "2023-01-10T16:05:17Z")

</div>

> [@Aleksey\_Bogdanov](#):
>
> I struggle to select in Firefox as liberally.

It was the same before your change, right?

[Next page](https://meta.discourse.org/t/open-all-external-links-in-a-new-tab-does-not-work-when-you-select-some-text/249703.md?page=2)
