Skip to content

Using a clicked link as an argument for a URL

Sander Ronde edited this page Feb 7, 2021 · 2 revisions

Introduction

A common use case seems to be the pasting of URLs of links into another URL and opening that. For example having a right-click menu entry that, when clicked on a link, opens that link in google cache. In this tutorial you can see an example of how to do this.

Adding the script

  1. First of all create a new script-type menu item
  2. Select only the link-type content type, such that the right-click menu entry only shows up when clicking links.

selecting the link content type

  1. Paste in the following script
var target = crmAPI.contextData.target; // Get the clicked element
if (!target) { // Make sure it exists, if it doesn't, show an alert
    alert('No target selected');
    return;
}

// Find the closest link to what was clicked
var currentElement = target;
while (currentElement && !currentElement.href) {
    currentElement = currentElement.parentNode; 
}

// If we selected a link, open the new URL with the
// link target pasted in
if (currentElement && currentElement.href) {
		// Change the URL below to point to your preferred URL
    window.open(`http://example.com/?url=${currentElement.href}`, '_blank');
} else {
		// If nothing was selected, show an alert
    alert('Target is not a link!');
}
  1. Modify the URL (currently http://example.com/?url=) to be the one you want to point to.
  2. You're done! Save the script and try it out.