mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-14 15:49:31 -07:00
Fix copy permalink when on non-secure source close #47
This commit is contained in:
@@ -7,7 +7,13 @@ const specialMappings = {
|
||||
|
||||
const init = () => {
|
||||
// add action to existing link
|
||||
document.querySelector('#share-link').addEventListener('click', createLink);
|
||||
const shareLink = document.querySelector('#share-link');
|
||||
shareLink.addEventListener('click', createLink);
|
||||
|
||||
// if navigator.clipboard does not exist, change text
|
||||
if (!navigator?.clipboard) {
|
||||
shareLink.textContent = 'Get Permalink';
|
||||
}
|
||||
};
|
||||
|
||||
const createLink = async (e) => {
|
||||
@@ -33,10 +39,23 @@ const createLink = async (e) => {
|
||||
|
||||
const url = new URL(`?${queryString}`, document.location.href);
|
||||
|
||||
// send to proper function based on availability of clipboard
|
||||
if (navigator?.clipboard) {
|
||||
copyToClipboard(url);
|
||||
} else {
|
||||
writeLinkToPage(url);
|
||||
}
|
||||
};
|
||||
|
||||
const copyToClipboard = async (url) => {
|
||||
try {
|
||||
// write to clipboard
|
||||
await navigator.clipboard.writeText(url.toString());
|
||||
// alert user
|
||||
const confirmSpan = document.querySelector('#share-link-copied');
|
||||
confirmSpan.style.display = 'inline';
|
||||
|
||||
// hide confirm text after 5 seconds
|
||||
setTimeout(() => {
|
||||
confirmSpan.style.display = 'none';
|
||||
}, 5000);
|
||||
@@ -45,6 +64,18 @@ const createLink = async (e) => {
|
||||
}
|
||||
};
|
||||
|
||||
const writeLinkToPage = (url) => {
|
||||
// get elements
|
||||
const shareLinkInstructions = document.querySelector('#share-link-instructions');
|
||||
const shareLinkUrl = shareLinkInstructions.querySelector('#share-link-url');
|
||||
// populate url and display
|
||||
shareLinkUrl.value = url;
|
||||
shareLinkInstructions.style.display = 'inline';
|
||||
// highlight for convenience
|
||||
shareLinkUrl.focus();
|
||||
shareLinkUrl.select();
|
||||
};
|
||||
|
||||
const parseQueryString = () => {
|
||||
// return memoized result
|
||||
if (parseQueryString.params) return parseQueryString.params;
|
||||
|
||||
Reference in New Issue
Block a user