userscripts/youtube2piped.user.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-10-15 15:45:43 -05:00
// ==UserScript==
// @name YouTube2Piped
// @namespace YouTube
2024-03-04 18:45:22 -06:00
// @version 1.4.2
2023-10-15 15:45:43 -05:00
// @description Redirect YouTube to chosen Piped instance
// @author SinTan
// @match *://*.youtube.com/*
// @match *://youtu.be/*
// @icon https://raw.githubusercontent.com/TeamPiped/Piped/32e7ddaaff22f4a6c0d7f6359400323da7fefd69/public/img/icons/logo.svg
// @grant none
// @run-at document-start
2023-10-27 02:35:06 -05:00
// @license GPL-3.0-only
2023-10-15 15:45:43 -05:00
// ==/UserScript==
(function () {
2023-11-03 00:13:47 -05:00
"use strict";
2024-03-04 18:45:22 -06:00
// Do not execute inside embedded players
if (window.location !== window.parent.location) {
exit;;
}
// Use #no-piped as an escape term
if (location.href.endsWith('#no-piped')) {
exit;
}
2023-11-03 00:13:47 -05:00
// Edit instance url here to go to any instance of choice
const instance = "https://piped.video";
2024-02-26 23:56:06 -06:00
const url = new URL(window.location.href.replace('/shorts/','/watch?v=').replace('?si=','&si='));
2023-11-03 00:13:47 -05:00
let url_new = null;
let id = url.searchParams.get('v');
let ts = url.searchParams.get('t');
let listId = url.searchParams.get('list');
if (id) {
url_new = instance + '/watch?v=' + id;
if (ts) {
url_new += '&t=' + ts;
}
if (listId) {
url_new += '&list=' + listId;
2023-10-15 15:45:43 -05:00
}
2023-11-03 00:13:47 -05:00
}
if (!(url_new)) {
if (listId) {
url_new = instance + '/playlist?list=' + listId;
}
}
if (!(url_new)) {
let pattern = /https:\/\/www\.youtube\.com\/((?:(?:channel\/)|\@)[A-Za-z0-9\_\-]+).*/;
let channelAddr = pattern.exec(url)[1];
if (channelAddr) {
url_new = instance + '/' + channelAddr;
}
}
if (url_new) {
window.location.replace(url_new);
}
2023-10-15 15:45:43 -05:00
}
2024-02-26 23:56:06 -06:00
)();