Creating a Bookmarklet That Emails the Current URL
Save the current page URL and title to your email when you select one bookmark.
A browser bookmarklet is a browser bookmark that uses JavaScript. This example sends the current page URL and title to an email target when you select the bookmark. Use it to save an article, share a link, or build a reading list.
Purpose#
Use this guide to:
- Build a JavaScript bookmarklet that calls a PayloadRelay endpoint.
- Configure an email target for the saved URLs.
- Add an optional note and tags to a saved link.
- Install the bookmarklet in a browser.
Before you start#
- Create a PayloadRelay endpoint that accepts
POSTwith theJSONpayload format. - Attach an email target that shows
Availableto the endpoint. - Cross-origin resource sharing (CORS) must permit the current page origin when
the bookmarklet uses
fetch(). The GET and image-pixel alternative in step 7 cannot show if the delivery was successful. A page Content Security Policy (CSP) can also block that alternative.
Procedure#
1. Create the endpoint#
- Open
Endpointsand selectCreate endpoint. - Set the accepted method to
POST. - Set the payload format to
JSON. - In
Destinations, attach your email target that showsAvailable. - Under
Configure destinations, setEmail subjectto{{subject}}. PayloadRelay then uses thesubjectfield of the payload. IfEmail subjectis empty, the subject isNo subject. - Save and copy the endpoint URL.
2. Basic bookmarklet#
This bookmarklet sends the current page URL and title to the endpoint:
javascript:void(fetch('https://api.payloadrelay.com/relay/YOUR_ENDPOINT_ID',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({subject:'Saved Link: '+document.title,url:location.href,title:document.title,saved_at:new Date().toISOString()})}).then(function(r){if(!r.ok){throw new Error('HTTP '+r.status)}alert('Link saved!')}).catch(function(e){alert('Failed to save link: '+e.message)}))Readable version:
javascript:void(
fetch('https://api.payloadrelay.com/relay/YOUR_ENDPOINT_ID', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
subject: 'Saved Link: ' + document.title,
url: location.href,
title: document.title,
saved_at: new Date().toISOString()
})
})
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
alert('Link saved!');
})
.catch((error) => alert(`Failed to save link: ${error.message}`))
)3. Bookmarklet with notes#
This version asks for an optional note before it sends the request:
javascript:void((function(){var n=prompt('Add a note (optional):','');if(n!==null){fetch('https://api.payloadrelay.com/relay/YOUR_ENDPOINT_ID',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({subject:'Saved Link: '+document.title,url:location.href,title:document.title,note:n||'(no note)',saved_at:new Date().toISOString()})}).then(function(r){if(!r.ok){throw new Error('HTTP '+r.status)}alert('Link saved!')}).catch(function(e){alert('Failed to save link: '+e.message)})}})())Readable version:
javascript:void((function() {
var note = prompt('Add a note (optional):', '');
if (note !== null) {
fetch('https://api.payloadrelay.com/relay/YOUR_ENDPOINT_ID', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
subject: 'Saved Link: ' + document.title,
url: location.href,
title: document.title,
note: note || '(no note)',
saved_at: new Date().toISOString()
})
})
.then(function(response) {
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
alert('Link saved!');
})
.catch(function(error) {
alert('Failed to save link: ' + error.message);
});
}
})())If you select Cancel in the prompt, the bookmarklet does not send the request.
4. Bookmarklet with tags#
This version asks for tags and an optional note:
javascript:void((function(){var t=prompt('Tags (comma-separated):','reading');if(t!==null){var n=prompt('Note (optional):','');if(n!==null){fetch('https://api.payloadrelay.com/relay/YOUR_ENDPOINT_ID',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({subject:'['+t+'] '+document.title,url:location.href,title:document.title,tags:t,note:n||'(no note)',saved_at:new Date().toISOString()})}).then(function(r){if(!r.ok){throw new Error('HTTP '+r.status)}alert('Link saved!')}).catch(function(e){alert('Failed to save link: '+e.message)})}}})())If Email subject is {{subject}}, the email subject contains the tags, for example: [reading, tech] Article Title.
5. Install the bookmarklet#
Chrome, Edge, Firefox, Brave
- Show the bookmarks bar (
Ctrl+Shift+BorCmd+Shift+B). - Right-click the bookmarks bar. Select
Add pageorAdd bookmark. - Set a name that you recognize, for example,
📧 Save Link. - Paste the bookmarklet code, which starts with
javascript:, into the URL field. - Save.
Safari
- Open
Bookmarksand selectAdd Bookmarkfor a page. - Edit the bookmark. Right-click it and select
Edit Address. - Replace the URL with the bookmarklet code.
- Save.
Mobile (iOS Safari)
- Bookmark a page.
- Edit the bookmark, and replace the URL with the bookmarklet code.
- To use it, type the first characters of the bookmark name in the address bar, and tap the suggestion.
6. Configure the email target#
- Open
Relay targets. SelectCreate relay target, then selectEmail. - Enter the email address that receives the saved links.
- Use the confirmation link to complete the email target confirmation.
- Attach it to the bookmarklet endpoint.
Each time you select the bookmarklet, it sends an email with:
- Subject:
Saved Link: <page title>, or with the tags if you configure them. This needsEmail subjectset to{{subject}}. - Body fields: URL, title, note, tags, timestamp
7. Handling CORS restrictions#
Some websites have a Content Security Policy that blocks a fetch() request to an external URL. A GET and image-pixel request does not read the response, so it does not need CORS. The page img-src CSP can still block the request:
javascript:void((function(){var q=new URLSearchParams({subject:'Saved: '+document.title,url:location.href,title:document.title,saved_at:new Date().toISOString()});new Image().src='https://api.payloadrelay.com/relay/YOUR_GET_ENDPOINT_ID?'+q;alert('Save requested. Read PayloadRelay Activity for the result.')})())The POST endpoint from step 1 rejects a GET request with 405. Create a second endpoint for this version. In Details, open Advanced. Set Accepted HTTP method to GET. A change to GET sets the payload format to None (ignore body). Above Advanced, set Expected payload format to Query Parameters. Attach the email target. Set Email subject to {{subject}}. PayloadRelay makes one payload field from each query parameter. A GET query string has a length limit, so use this method for a short payload. JavaScript cannot read the image response. The alert shows only that the browser tried the request. Look for Completed (ACCEPTED) in PayloadRelay Activity.
Sandbox and Solo permit 1 active endpoint. On these plans, PayloadRelay creates the second endpoint as a disabled endpoint. A disabled endpoint returns 404 and creates no Activity row. To use the second endpoint, disable the endpoint from step 1. Then enable the second endpoint. Alternatively, upgrade your plan in Billing.
Expected result#
- A fetch bookmarklet shows a success message only after a 2xx response. The image fallback shows an alert for the request attempt. For the result, read Activity.
- The email arrives in seconds with the page URL, the title, and the optional note and tags.
- A request appears in PayloadRelay
Request activityasCompleted(ACCEPTED).
Common issues#
- The bookmarklet does nothing: make sure that the URL field starts with
javascript:and has no line breaks. - The
Failed to save linkalert: read the browser console. A Content Security Policy often blocks the fetch request. - No email: make sure that the email target is
Availableand that it is attached to the endpoint. - A CORS error: permit the current page origin in
Allowed CORS originsinSecurity, or use the GET and image-pixel version. An emptyAllowed CORS originslist permits every origin. If the CSP blocksconnect-srcandimg-src, the page cannot use either version. - The prompt does not appear: some browsers block a prompt from a bookmarklet on some pages. Use the basic version with no prompt.