Initial commit
This commit is contained in:
44
yt-share-cleaner.js
Normal file
44
yt-share-cleaner.js
Normal file
@@ -0,0 +1,44 @@
|
||||
(function() {
|
||||
// 'use strict';
|
||||
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
|
||||
console.log("Not a share link, clipboard contents unmodified.")
|
||||
return(url)
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function onMouseUp() {
|
||||
// wait half a second after clicking before reading clipboard
|
||||
await sleep(500);
|
||||
|
||||
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);
|
||||
clipText = undefined;
|
||||
newText = undefined;
|
||||
}
|
||||
clipText = undefined;
|
||||
newText = undefined;
|
||||
}
|
||||
|
||||
document.addEventListener('mouseup', onMouseUp, false);
|
||||
})();
|
Reference in New Issue
Block a user