The extension code is at this repo. Follow the instruction to install it for your browser.

Problem

I'm tired(pissed) of(f) selecting and copy a fixed part in an URL.

As I write more and more code changes, the possibility of mentioning change list (CL, diff, whatever) links becomes higher and higher. Besides, whenever I refer to someone else's code change in the group, I often need to attach a CL. The complete link is something like this:

https://code-review.awesome-company.com/cl/<cl number>/<whatever>

For some reasons, probably for simplicity, I never see people paste the full link. Instead, everyone just pastes as cl/<cl number> and takes the advantage of company browser settings which can recognize the short link and jump to the correspondent one. Sure, this is good, simple and straightforward, but it's not a killer yet.

What's more, there are many other similar cases where just a certain part of a URL is needed to be redirected to the right site. Some sites provide a button so that we can copy a shorter part but you need some "luck" - not every site has it.

DRY (Don't Repeat Yourself)

The idea is very straightforward as well. For any given URL, if I just need a part of it, I whish there was a button smart enough so that I could click it and by doing this I would be able to select the part I need and put it in my clipboard. Then I could paste it to wherever I want.

Chrome is the browser, Chrome Extension is a very good tool to solve this problem.

I also considered Tampermonkey, which is super cool and maybe is an even better solution. However, I want to learn more about Chrome Extensions so I will create an extension instead.

Match the URL, then the subpart of the URL

It's very not smart to write a very general regex(Regular Expression) to match a part of interst in the URL, which can lead to conflict and copy a trash out of it. Instead, we are going to deal with it with 2 matches.

1. Know which URL to match

Whenever the user clicks this extension when enabled, the extension code runs. The extension gets the active URL, which is the URL of current tab, and checks if this is an URL that it knows how to deal with.

This is the first time a match attempt occurs. For this step, the regex should be as specific as possible.

2. Know which part of this URL is needed

Once the URL is matched, the extension needs to know which part of it to take out and put to clipboard.

This is the second time a match attempt occurs. The code extracts the part from URL.

3. Put the matched part into clipboard

How do you put a giraffe(text) into a refrigerator(clipboard)?

-- Open clipboard and place the string.

When the content is valid, the code will put it into clipboard. You don't even need to close clipboard!

In the option settings of the extension, I added following value as URL regex and pattern regex:
URL: code-review\.awesome-company\.com\/cl\/[0-9]+\/?.*
pattern: cl\/[0-9]+

Now if I go to the code review page and click the extension, it knows to copy the cl/<number> part and tells me content copied when it's done(instantly). With a Ctrl-v combo, pasted, hooray!
Then I did similar settings to other sites I frequently use.

Possible Improvement

I spent just a couple hours writing this extension. It's simple and ugly but my effort is paying off.

To make it a cooler project, the first thing I would improve is make the matching logic smarter, enable the extension(make it clickable) only when the site is on the list. Besides, I currently don't have a better strategy to match the URL more smartly. Last but not the least, adding a test field in option can help users quickly verify whether their regex works as expected.

Update 04/10: oh I updated UI today, it looks much better.