40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
(function() {
|
|
function isShareLink(url) {
|
|
// test if input is a share link, return True or False
|
|
const regex = /https:\/\/youtu\.be\/[A-Za-z0-9\_-]+\?si=.+/i;
|
|
const isShareLink = regex.test(url);
|
|
return(isShareLink)
|
|
}
|
|
|
|
function cleanUrl(url){
|
|
// clean tracker from URL if it is a share link
|
|
if (isShareLink(url)) {
|
|
// replace tracker portion with empty string
|
|
const cleanedUrl = url.replace(/\?si=[^&]+&?/, "");
|
|
return(cleanedUrl)
|
|
}
|
|
// return original url if it is not a share link
|
|
return(url)
|
|
}
|
|
|
|
async function shareCleaner() {
|
|
// delay clipboard read slightly because of page script nonsense
|
|
await new Promise((resolve, reject) => setTimeout(resolve, 50));
|
|
|
|
let clipText = await navigator.clipboard.readText();
|
|
let newText = cleanUrl(clipText);
|
|
|
|
if (clipText !== newText) {
|
|
const newClip = await navigator.clipboard.writeText(newText);
|
|
console.log("Modified clipboard with cleaned share link: %o", newText);
|
|
} else {
|
|
console.log("Share link not detected, clipboard contents unmodified.");
|
|
}
|
|
// discard clipboard data
|
|
clipText = undefined;
|
|
newText = undefined;
|
|
}
|
|
// run whenever copy event is detected
|
|
document.addEventListener('copy', shareCleaner, false);
|
|
})();
|