Last active
September 5, 2024 17:00
-
-
Save chrislewis/eaef7cad6632f8f15d16bdb34a746916 to your computer and use it in GitHub Desktop.
This JS is meant to be used as a bookmarklet. Its intent is to grab the Trello card "slug", either from the URL or the DOM in the case of following a card short url to a displaying card, and write it to the clipboard. For teams running off Trello this might be a handy way to quickly grab the slug for use as git branch names, for example. https:/…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pattern of card "slugs" in URLs. | |
const CardSlug = /(^\d+-[a-z,A-Z,\d,-]+)/ | |
// Given a string assumed to be the last path segment of a url, attempt to extract a slug using the pattern. | |
// Return the slug or undefined if the pattern didn't match. | |
const slug = (s) => { | |
const m = s.match(CardSlug) | |
return m ? m[0] : undefined | |
} | |
// Fetch the last path segment. If path char not present, return the input. | |
const lastPathSeg = (s) => s.substring(s.lastIndexOf("/") + 1) | |
// Get the slug from the (last path segment of the) url. | |
const urlToSlug = (u) => slug(lastPathSeg(u)) | |
// "Lazy" places to search for a slug. For trello card short urls (via share button or slack notif), a secondary | |
// search is a link within the DOM of the rendered card. | |
const hrefs = [ | |
() => window.location.href, | |
() => document.querySelector('.phenom-meta a.js-highlight-link')?.href | |
] | |
// Take the first found slug from hrefs, if any. | |
const foundSlug = hrefs.reduce((p, c) => { | |
if(p) { | |
return p | |
} else { | |
const s = c() | |
return s ? urlToSlug(s) : undefined | |
} | |
}, undefined) | |
// Write slug to clipboard or alert on fail. | |
if(foundSlug) { | |
navigator.clipboard.writeText(foundSlug) | |
} else { | |
alert("Can't find the card!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment