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.

80 lines
2.6 KiB
JavaScript

chrome.contextMenus.onClicked.addListener(addToStammTV);
let WEBSOCKET_URL;
let websocket;
const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST";
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();
}
});
}
})
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({
title: "Zu StammTV Playlist hinzufügen...",
contexts: ["link"],
id: CONTEXT_MENU_ID
});
});
function parseYoutubeURL(url) {
const YOUTUBE_URL_REGEX = /^https?:\/\/(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=|be\.com\/shorts\/)([^#&?]*)(?:[?&]t=\d+)?.*/;
let match = url.match(YOUTUBE_URL_REGEX);
return (match && match[1].length === 11) ? match[1] : false;
}
function addToStammTV(info, tab) {
if (info.menuItemId === CONTEXT_MENU_ID) { //if OUR menu item was clicked
let videoId = parseYoutubeURL(info.linkUrl)
if (videoId) {
if (websocket === undefined
|| websocket.readyState !== WebSocket.OPEN) {
chrome.storage.local.get('wssURL', function (result) {
if (result.wssURL) {
WEBSOCKET_URL = result.wssURL;
} else {
chrome.runtime.openOptionsPage();
}
});
websocket = new WebSocket(WEBSOCKET_URL);
console.log('websocket created');
} else {
console.error('websocket undefined or not OPEN')
}
websocket.onopen = function () {
console.log('websocket ready - sending');
const playerReady = {"type": "playerReady"};
websocket.send(JSON.stringify(playerReady));
console.log(playerReady);
const setVideo = {
type: "setVideo",
video: videoId,
};
websocket.send(JSON.stringify(setVideo));
console.log(setVideo);
};
setTimeout(function () {
websocket.close();
console.log('websocket closed');
}, 1000);
} else {
console.error('o(一︿一+)o not a valid videoId');
}
}
}