-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
30 lines (26 loc) · 987 Bytes
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const button = document.createElement('button');
button.id = 'redirectBtn';
button.innerText = 'Watch on Piped.video';
button.classList.add('main-button');
button.addEventListener('click', function() {
const videoID = window.location.search.match(/v=([a-zA-Z0-9_-]+)/);
if (videoID && videoID[1]) {
const newURL = 'https://piped.video/watch?v=' + videoID[1];
window.location.href = newURL;
} else {
alert("This doesn't seem to be a valid YouTube video URL.");
}
});
function insertButtonIfTargetExists() {
const targetElement = document.getElementById('masthead').querySelector('ytd-topbar-menu-button-renderer');
if (targetElement && targetElement.parentElement) {
targetElement.parentElement.insertBefore(button, targetElement);
// If found and added, disconnect the observer
observer.disconnect();
}
}
const observer = new MutationObserver(insertButtonIfTargetExists);
observer.observe(document.body, {
childList: true,
subtree: true
});