Last active
November 13, 2023 19:47
-
-
Save rileyrichter/7de098f55a6de9af5352db8a856c586c to your computer and use it in GitHub Desktop.
Show dialog with each external link to confirm
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
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | |
to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or | |
substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
// This function will create a modal dialog | |
function createModal(externalUrl) { | |
// Create the dialog element | |
var dialog = document.createElement('dialog'); | |
dialog.setAttribute('style', 'position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);'); | |
// Add content to the dialog | |
var dialogContent = ` | |
<p>Do you want to continue to the external link?</p> | |
<p><a href="${externalUrl}" target="_blank">Yes, proceed</a></p> | |
<button id="closeDialog">Cancel</button> | |
`; | |
dialog.innerHTML = dialogContent; | |
// Append the dialog to the body | |
document.body.appendChild(dialog); | |
// Add event listener to the close button | |
var closeButton = document.getElementById('closeDialog'); | |
closeButton.addEventListener('click', function() { | |
dialog.close(); | |
}); | |
return dialog; | |
} | |
// This function will add event listeners to all external links | |
function addExternalLinkListeners() { | |
var allLinks = document.getElementsByTagName('a'); | |
for (var i = 0; i < allLinks.length; i++) { | |
if (allLinks[i].hostname !== window.location.hostname) { | |
allLinks[i].addEventListener('click', function(e) { | |
e.preventDefault(); // Prevent the default link behavior | |
// Create and open the modal dialog | |
var dialog = createModal(this.href); | |
dialog.showModal(); | |
// When the dialog is closed, remove it from the document | |
dialog.addEventListener('close', function() { | |
document.body.removeChild(dialog); | |
}); | |
}); | |
} | |
} | |
} | |
// Run the function to add event listeners to external links | |
addExternalLinkListeners(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment