PopClip Extension that removes Googles tracking info

I’m not a fan of how Google and other companies track every move you make. However, there are ways to bypass that and still use Google to find what you need. I actually made a Keyboard Maestro shortcut for it, but I’m curious if I can achieve the same result with a PopClip Extension. I’ve tried two different approaches, but I’m unsure what I’m overlooking. The PopClip icon is grayed out in each YAML file, so I can’t click on it. I suppose the first step is to copy the URL and then select the link to activate PopClip.
Here is the first sample using the clipboard:

# PopClip extension to clean the URL of the highlighted text
# Removes everything before "https" and replaces some characters
name: Clean URL
identifier: com.popclip.extensions.cleanurl
version: 1.0
icon: symbol:moon.stars # Apple SF Symbols
actions:
  - script: |
      #!/bin/bash
      # Get the highlighted text
      input=$POPCLIP_TEXT
      # Remove everything before "https"
      output=${input#*https}
      # Replace %3A with ":"
      output=${output//%3A/:}
      # Replace %2F with "/"
      output=${output//%2F//}
      # Replace %3F with "?"
      output=${output//%3F/?}
      # Copy the output to the clipboard
      echo "$output" | pbcopy

I would greatly appreciate any assistance or guidance you can provide. Thank you in advance!

Hi @macmarty , welcome to the forum. The snippet format is not correct – there’s no such field as “script”. You need to use “shell script” instead (and also, when inputing script inside YAML, specify the interpreter – although I don’t know why it doens’t pick up the shebang at the top, I think it should!). Here’s a lightly edited version that works:

# PopClip extension to clean the URL of the highlighted text
# Removes everything before "https" and replaces some characters
name: Clean URL
identifier: com.popclip.extensions.cleanurl
version: 1.0
icon: symbol:moon.stars # Apple SF Symbols
actions:
  - interpreter: bash
    shell script: |
      # Get the highlighted text
      input=$POPCLIP_TEXT
      # Remove everything before "https"
      output=${input#*https}
      # Replace %3A with ":"
      output=${output//%3A/:}
      # Replace %2F with "/"
      output=${output//%2F//}
      # Replace %3F with "?"
      output=${output//%3F/?}
      # Copy the output to the clipboard
      echo "$output" | pbcopy

However you can make it a little more comfortable for yourself by using the inverted syntax, so that the whole thing is just a script and you don’t have to worry about the YAML indenting. Here’s how I might format your script:

#!/bin/bash
# # PopClip extension to clean the URL of the highlighted text
# # Removes everything before "https" and replaces some characters
# name: clean url (bash)
# icon: symbol:moon.stars
# after: copy-result

# Remove everything before "https"
output=${POPCLIP_TEXT#*https}
# Replace %3A with ":"
output=${output//%3A/:}
# Replace %2F with "/"
output=${output//%2F//}
# Replace %3F with "?"
output=${output//%3F/?}
# Return the result (will be copied by "copy-result" in config
# and popclip will show "Copied" indicator.)
echo -n $output # note -n flag is needed to suppress echo adding a newline

You can also use PopClip’s internal JavaScript interpreter for a task like this. This is just a preference, but I like JS for simple string manipulation since it doesn’t require calling out to an external process. Here’s my attempt to do the equivalent of your bash script:

// #popclip
// name: clean url (js)
// icon: symbol:moon.stars
// language: javascript

let output = popclip.input.text
// Remove everything before "https"
output = output.substring(output.indexOf('https'));
// Replace encoded characters    
output = output.replaceAll('%3A', ':');
output = output.replaceAll('%2F', '/');
output = output.replaceAll('%3F', '?');
popclip.copyText(output);

BTW, when posting code on the forum, add ``` on a line before and after the code so thew forum software will format it as code (I edited your post).

1 Like

Hey Nick,
I wanted to express my gratitude for the warm welcome and for promptly addressing my query. I will give it a try right now and report back.

Macmarty

1 Like

I used your Javascript and changed it to this:

// #popclip
// name: clean url (js)
// icon: symbol:moon.stars
// language: javascript

let output = popclip.input.text
// Remove everything before the second "https"
let index = output.lastIndexOf('https');
output = output.slice(index);
// Replace encoded characters    
output = output.replaceAll('%3A', ':');
output = output.replaceAll('%2F', '/');
output = output.replaceAll('%3F', '?');
popclip.copyText(output);
2 Likes

You might like to use decodeURIComponent() to do the decode, as it works on all percent-encoded characters:

// Replace encoded characters 
output = decodeURIComponent(output);

May be more robust, although of course it depends on how the particular semantics of the original URL, and how it was encoded.

1 Like