commit 5f7ba1d7f427be56d287ca5279f0b69ff77cf8c6 Author: turtel Date: Fri Feb 16 22:50:11 2024 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/background.js b/background.js new file mode 100644 index 0000000..9d1a813 --- /dev/null +++ b/background.js @@ -0,0 +1,24 @@ +/* chrome.runtime.onInstalled.addListener(() => { + chrome.action.setBadgeBackgroundColor({ color: '#FF0000' }); + chrome.action.setBadgeText({text: 'ON'}); +}); */ + +chrome.action.onClicked.addListener((tab) => { + // Ensure we have a valid tab ID + if (tab.id !== undefined) { + chrome.tabs.get(tab.id, (currentTab) => { + if (currentTab.url.includes('youtube.com/watch?v=')) { + const videoID = new URL(currentTab.url).searchParams.get('v'); + if (videoID) { + const newURL = 'https://pip.turtel.xyz/watch?v=' + videoID; + chrome.tabs.update(currentTab.id, { url: newURL }); + } else { + alert("This doesn't seem to be a valid YouTube video URL."); + } + } else { + alert("Please navigate to a YouTube video before clicking the extension icon."); + } + }); + } +}); + diff --git a/content.js b/content.js new file mode 100644 index 0000000..0d811b8 --- /dev/null +++ b/content.js @@ -0,0 +1,30 @@ +const button = document.createElement('button'); +button.id = 'redirectBtn'; +button.innerText = 'Watch on Pipet'; +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://pip.turtel.xyz/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 +}); diff --git a/icon128.png b/icon128.png new file mode 100644 index 0000000..95c1f2e Binary files /dev/null and b/icon128.png differ diff --git a/icon16.png b/icon16.png new file mode 100644 index 0000000..95c1f2e Binary files /dev/null and b/icon16.png differ diff --git a/icon48.png b/icon48.png new file mode 100644 index 0000000..95c1f2e Binary files /dev/null and b/icon48.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..797c06a --- /dev/null +++ b/manifest.json @@ -0,0 +1,27 @@ +{ + "manifest_version": 3, + "name": "Pipet redirector XD", + "version": "1.0", + "description": "Redirects from YouTube to Piped.video using the video's ID", + "permissions": ["tabs"], + "content_scripts": [{ + "matches": ["https://www.youtube.com/watch*"], + "css": ["styles.css"], + "js": ["content.js"] + }], + "background": { + "service_worker": "background.js" + }, + "action": { + "default_icon": { + "16": "icon16.png", + "48": "icon48.png", + "128": "icon128.png" + } + }, + "icons": { + "16": "icon16.png", + "48": "icon48.png", + "128": "icon128.png" + } +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..feb3edd --- /dev/null +++ b/popup.html @@ -0,0 +1,16 @@ + + + + Redirect to Piped.video + + + + + + + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..59620d2 --- /dev/null +++ b/popup.js @@ -0,0 +1,13 @@ +document.getElementById('redirectBtn').addEventListener('click', function() { + chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + var currentTab = tabs[0]; + var videoID = currentTab.url.match(/watch\?v=([a-zA-Z0-9_-]+)/); + + if(videoID && videoID[1]) { + var newURL = 'https://piped.video/watch?v=' + videoID[1]; + chrome.tabs.update(currentTab.id, {url: newURL}); + } else { + alert("This doesn't seem to be a valid YouTube video URL."); + } + }); +}); diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..998c6dd --- /dev/null +++ b/styles.css @@ -0,0 +1,12 @@ +.main-button { + background: #FF0000; + color: white; + border: none; + border-radius: 5px; + padding: 10px 15px; + font-size: 14px; + cursor: pointer; + z-index: 9999; + box-shadow: 0 0 5px rgba(0, 0, 0, 0.5); + margin-right: 10px; +}