Search code examples
javascriptbookmarklet

Bookmarklet to grab info from current url and insert into new one


I'd like a bookmarklet that takes info from my current url (like these):

https://www.themoviedb.org/tv/88396-falcon-winter-soldier
https://www.themoviedb.org/movie/64688-21-jump-street
https://www.themoviedb.org/person/7447-alec-baldwin

Extracts just id portion, the first set of numbers (88396, 64688, 7447, etc) and appends it to a new url like this:

https://webhookurl.com/webhook?movieID=(id from url)

Which opens in a background tab ideally.

I'm super new to javascript, been trying to cobble something together from similar answers on here but unable to figure it out. Any help is very much appreciated.

update: thanks to some help here with the regex and tinkering until i got the other tab thing to work, i now have a finished bookmarklet that works. thanks

ud2: changed again to 1. match urls with no '-film-name' after the id number, and also to open in a small new window rather than new tab so i can easily close after the webhook is accepted and it doesn't obstruct my view of the page

javascript:(function(){ open(window.location.toString().replace(/^https:\/\/www.themoviedb.org\/.*?\/([0-9]+)-?.*/, 'https://fakewebhook.com/webhook?movieID=$1'), "", "width=400, height=200");})()

Solution

  • You may use regex to form the new url from the given url.

    'https://www.themoviedb.org/tv/88396-falcon-winter-soldier'.replace(/^https:\/\/www.themoviedb.org\/tv\/([0-9]+)-.+/, 'https://webhookurl.com/webhook?movieID=$1')