Copy link to text in web page [URL Fragment Text Directives]

There seems to be a chrome feature for copying a link to a specific excerpt of a web page: How to Create a Link to Selected Text in Chrome

The format of the link seems to be:

https://example.com/foo#:~:text=URL:%20encoded%20text

real example https://www.howtogeek.com/726473/how-to-create-a-link-to-selected-text-in-chrome/#:~:text=When%20the%20recipient%20clicks%20the%20link

The URL construction looks straightforward to guess but I wonder if there are any gotchas.

I wonder if there is a standard for this or is it or Chrome specific? Since it’s done with a fragment it’s likely harmless to any browsers that don’t support it.

A PopClip extension for this might be handy. (tweet)

1 Like

Extension snippet, seems to work!

// #popclip
// name: Copy Excerpt Link
// icon: square E
// language: javascript
const link = popclip.context.browserUrl + '#:~:text=' + encodeURIComponent(popclip.input.text);
popclip.copyText(link);

(The above block is an extension snippet - select it to install the extension with PopClip)

2 Likes

Didn’t understand the above, and this my fault. Can’t code. So, dumb question, if I use this safari what will I be able to do?

1 Like

This technique doesn’t seem to work in Safari on iOS (at least on my iPhone) — your example URL just opens the page at the top.

But combined with Choosy — or even just a chrome:// url handler, if that works out of the box — this would be amazing.

To try to explain: If you use Chrome, and you add something to end of any URL in Chrome, you can link not just to the top of the page, but to the place on the page where certain text appears. The part you add to the end of the URL is the text you’re linking to (but “encoded,” so spaces and other characters that don’t work on URLs are turned into %20 or something similar).

1 Like

The feature is unique to Chrome:

https://www.howtogeek.com/658842/how-to-use-google-chromes-new-deep-linking-feature/#:~:text=Chrome%2080%20makes%20that%20possible,Google%20Chrome%2080%20for%20now.

However there is a movement underway to develop a universal method to link among apps

3 Likes

Thanks for that @rkaplan and everyone! :sunglasses:
I hadn’t heard of this Chrome feature till yesterday when I saw a tweet asking for a PopClip extension to do the same thing. Glad I used the forum to flesh it out
 I like how it can be a kind of interactive notebook.

1 Like

Hi Nick,

Thank you for sharing this extension snippet. Unfortunately, it seems that the popclip.context.browserUrl feature does not work with the ARC Browser. I tried using the provided extension snippet, and while it works perfectly with other supported browsers, it does not fetch the URL when using ARC Browser.

Is there any possibility to add support for ARC Browser in the future? It is my preferred browser for various reasons, and having PopClip fully functional with it would greatly enhance my workflow.

Thank you for your continuous efforts in developing and improving PopClip. It is a fantastic tool that has been incredibly helpful in my daily tasks.

1 Like

Unfortunately, unlike Chrome, Safari and some other browsers, Arc does not offer an AppleScript interface that allows PopClip to find out the URL of the current foreground tab. If Arc were to add a suitable interface then I could add support for it in PopClip.
Update 23 Oct 2025: Works in Arc now with PopClip 2025.9.1.

Hello,

I am looking for a way to associate a link with a keyword of text. This is a feature available in some browsers, but not yet universal, although this is democratizing.

Here is the link to the specification : URL Fragment Text Directives

The easiest way is to add to the URL: #:~:text=, followed by the selected text.

I’ve tried different methods, but it doesn’t always work or it gets very complex. I guess this could be done easily?

Thank you for your help.


Bonjour,

Je recherche un moyen d’associer un lien Ă  un surlignage de texte. Il s’agit d’une fonctionnalitĂ© disponible dans certains navigateurs, mais pas encore universel, bien que cela se dĂ©mocratise.

Voici le lien vers la spécification : URL Fragment Text Directives

Le plus simple consiste Ă  ajouter Ă  l’URL : #:~:text=, suivi du texte sĂ©lectionnĂ©.

J’ai essayĂ© diffĂ©rentes mĂ©thodes, mais cela ne fonctionne pas toujours ou cela devient trĂšs complexe. J’imagine que cela pourrait ĂȘtre rĂ©alisĂ© facilement ?

Merci pour votre aide.

Take a look here!: Copy link to text in web page [URL Fragment Text Directives]

1 Like

I didn’t search thoroughly, I apologize and thank you very much!

It would be cool if it also copied the link, so you only have to paste it where you want.

1 Like

It does copy the link – you should see “Copied” when you invoke the extension on a selection on a web page.

On LibreWolf, it doesn’t work.
It pastes only a fragment with code.
But on safari is ok.

It won’t wortk in LibreWolf.

I have a page about browser compatibility with PopClip. It needs the “Page Info” green tick for this to work.

In summary:

:white_check_mark: Will work in:

Arc, Brave, Chromium, Google Chrome, Microsoft Edge, Helium*, Opera, Orion, Safari, Sidekick, Sigma OS, Thorium, Vivaldi

* Helium support requires latest PopClip beta version

:cross_mark: Won’t work in:

ChatGPT Atlas, Comet, DEVONagent Pro, Dia, DuckDuckGo, LibreWolf, Mozilla Firefox, Mullvad, Quark, Tor Browser, Waterfox, Zen

Okay, I understand.
That’s why when asking an AI, it resorted to AppleScript to retrieve the URL.

Here’s what it tells me; the comments are in French, but I imagine the code is understandable in all languages ^^.


#!/bin/bash

# --- 1ïžâƒŁ RĂ©cupĂ©ration du texte sĂ©lectionnĂ©
text="$POPCLIP_TEXT"

# --- 2ïžâƒŁ RĂ©cupĂ©ration de l'URL courante
url="$POPCLIP_BROWSER_URL"

# Si PopClip ne fournit pas l'URL, on tente via AppleScript
if [ -z "$url" ]; then
    url=$(osascript <<'APPLESCRIPT'
        tell application "System Events"
            set frontApp to name of first process whose frontmost is true
        end tell
        if frontApp is "Safari" then
            tell application "Safari" to return URL of front document
        else if frontApp is "Google Chrome" then
            tell application "Google Chrome" to return URL of active tab of front window
        else if frontApp is "Brave Browser" then
            tell application "Brave Browser" to return URL of active tab of front window
        else if frontApp is "Microsoft Edge" then
            tell application "Microsoft Edge" to return URL of active tab of front window
        else if frontApp is "Arc" then
            tell application "Arc" to return URL of active tab of front window
        else if frontApp is "Firefox" then
            tell application "Firefox" to activate
            tell application "System Events"
                keystroke "l" using {command down}
                keystroke "c" using {command down}
                delay 0.1
                set theURL to the clipboard
            end tell
            return theURL
        else
            return ""
        end if
APPLESCRIPT
)
fi

# --- 3ïžâƒŁ Encodage du texte pour l’URL
encoded_text=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$text'''))")

# --- 4ïžâƒŁ Construction du lien
if [ -z "$url" ]; then
    echo "Impossible de rĂ©cupĂ©rer l’URL de la page ❌" >&2
    exit 1
fi

link="${url}#:~:text=${encoded_text}"

# --- 5ïžâƒŁ Copie dans le presse-papiers
echo -n "$link" | pbcopy

# --- 6ïžâƒŁ Affichage pour PopClip
echo "✅ Lien copiĂ© : $link"

PopClip already uses AppleScript similar to this internally to retrieve the URLs from browsers.

But the unsupported browsers don’t have an AppleScript interface, which is why they are unsupprted.

(PopClip doesn’t use the the ⌘L ⌘C method for Firefox or LibreWolf though – that is nasty! OK for a rough DIY script but having the URL bar getting selected all the time would not fly with users)

I don’t know anything about it, but of course I suspect that the code isn’t the best. :slight_smile:
We need to find a solution for FF, that would be cool :slight_smile: Then I have a plugin on FF that allows me to do it but I don’t know if it’s really a good thing