let WEBSOCKET_URL = ""; let websocket; const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; chrome.storage.local.get('firstRun', function (result) { console.log(result); if (result.firstRun === false) { chrome.storage.local.set({'firstRun': false}, function (result) { openSettings(); }); } else { chrome.storage.local.get(['baseURL', 'wssURL'], function (items) { if (items.wssURL !== undefined && items.wssURL.startsWith('wss://')) { WEBSOCKET_URL = items.wssURL; handleValidityCheckResult(true) } }); } }); function openSettings() { chrome.tabs.create({ active: true, url: 'options/options.html' }, null); } function handleValidityCheckResult(validSettings) { if (validSettings) { addContextMenuItem(); } else { openSettings(); } } function addContextMenuItem() { chrome.contextMenus.removeAll(function() { chrome.contextMenus.create({ title: "Zu StammTV Playlist hinzufügen...", contexts: ["link"], id: CONTEXT_MENU_ID }); }); chrome.contextMenus.onClicked.addListener(addToStammTV); } function parseYoutubeURL(url) { const YOUTUBE_URL_REGEX = /^(?:https?:\/\/(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=|be\.com\/shorts\/)){1}([^#&?]*)(?:[?&]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) { const playerReady = {"type": "playerReady"}; const setVideo = { type: "setVideo", video: videoId, }; console.log(websocket); if (websocket === undefined || websocket.readyState !== WebSocket.OPEN) { websocket = new WebSocket(WEBSOCKET_URL); console.log('websocket created'); } websocket.onopen = function () { console.log('websocket ready'); websocket.send(JSON.stringify(playerReady)); 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'); } } }