You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.6 KiB
JavaScript

chrome.contextMenus.onClicked.addListener(addToStammTVHandler);
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === "install") {
chrome.runtime.openOptionsPage();
} else if (details.reason === "update") {
chrome.storage.local.get(['baseURL', 'wssURL'], function (items) {
if (items.wssURL !== undefined && items.wssURL.startsWith('wss://')) {
} else {
chrome.runtime.openOptionsPage();
}
});
2 years ago
}
})
2 years ago
const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST";
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({
title: "Zu StammTV Playlist hinzufügen...",
2 years ago
contexts: ["link", "video"],
id: CONTEXT_MENU_ID
});
});
2 years ago
2 years ago
function addToStammTVHandler(info, tab) {
2 years ago
const YOUTUBE_URL_REGEX = /^https?:\/\/(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=|be\.com\/shorts\/)([^#&?]*)(?:[?&]t=\d+)?.*/;
const INVIDIOUS_API_REGEX = /^https:\/\/inv\.theresno.cloud\/latest_version\?.*id=([a-zA-Z0-9_/-]{11}).*/;
const INVIDIOUS_VIDEO_URL_REGEX = /^https:\/\/inv\.theresno.cloud\/watch\?v=([a-zA-Z0-9_/-]{11}).*/;
2 years ago
2 years ago
function parseYoutubeURL(url) {
let ytMatch = url.match(YOUTUBE_URL_REGEX);
if (ytMatch && ytMatch[1].length === 11) {
return ytMatch[1];
}
let apiMatch = url.match(INVIDIOUS_API_REGEX);
if (apiMatch && apiMatch[1].length === 11) {
return apiMatch[1];
}
let invMatch = url.match(INVIDIOUS_VIDEO_URL_REGEX);
if (invMatch && invMatch[1].length === 11) {
return invMatch[1];
}
return false;
}
2 years ago
if (info.menuItemId === CONTEXT_MENU_ID) { //if OUR menu item was clicked
console.log('clicked!');
2 years ago
let videoURL = info.linkUrl ? info.linkUrl : info.srcUrl;
if (videoURL) {
let websocket;
chrome.storage.local.get('wssURL', function (result) {
if (result.wssURL) {
2 years ago
if (parseYoutubeURL(videoURL)) {
videoURL = 'https://inv.theresno.cloud/latest_version?id=' + parseYoutubeURL(videoURL)
}
websocket = new WebSocket(result.wssURL);
const playerReady = {"type": "playerReady"};
2 years ago
const setVideo = {type: "setVideo", video: videoURL};
2 years ago
websocket.onopen = function () {
console.log('[open] Connection established');
websocket.send(JSON.stringify(playerReady));
websocket.send(JSON.stringify(setVideo));
setTimeout(() => websocket.close(), 5000);
};
2 years ago
websocket.onmessage = function (event) {
console.log(`[message] Data received from server: ${event.data}`);
};
websocket.onclose = function (event) {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`)
} else {
console.log('[close] Connection died');
}
}
2 years ago
websocket.onerror = function (error) {
alert(`[error] ${error.message}`);
};
} else {
chrome.runtime.openOptionsPage();
}
});
2 years ago
} else {
console.error('failed to add ' + info.linkurl + ': o(一︿一+)o not a valid link');
2 years ago
}
}
}