fixed a bug where websocket was not handled properly
This commit is contained in:
and94x 2022-08-17 21:09:17 +02:00
parent 04948e24f9
commit d87a264d87
3 changed files with 58 additions and 56 deletions

View File

@ -1,8 +1,4 @@
chrome.contextMenus.onClicked.addListener(addToStammTV); chrome.contextMenus.onClicked.addListener(addToStammTVHandler);
let WEBSOCKET_URL;
let websocket;
const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST";
chrome.runtime.onInstalled.addListener(function (details) { chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === "install") { if (details.reason === "install") {
@ -17,6 +13,8 @@ chrome.runtime.onInstalled.addListener(function (details) {
} }
}) })
const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST";
chrome.contextMenus.removeAll(function () { chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({ chrome.contextMenus.create({
title: "Zu StammTV Playlist hinzufügen...", title: "Zu StammTV Playlist hinzufügen...",
@ -33,47 +31,50 @@ function parseYoutubeURL(url) {
} }
function addToStammTV(info, tab) { function addToStammTVHandler(info, tab) {
if (info.menuItemId === CONTEXT_MENU_ID) { //if OUR menu item was clicked if (info.menuItemId === CONTEXT_MENU_ID) { //if OUR menu item was clicked
let videoId = parseYoutubeURL(info.linkUrl) console.log('clicked!');
let videoId = parseYoutubeURL(info.linkUrl);
if (videoId) { if (videoId) {
if (websocket === undefined let websocket;
|| websocket.readyState !== WebSocket.OPEN) {
chrome.storage.local.get('wssURL', function (result) { chrome.storage.local.get('wssURL', function (result) {
if (result.wssURL) { if (result.wssURL) {
WEBSOCKET_URL = result.wssURL; websocket = new WebSocket(result.wssURL);
const playerReady = {"type": "playerReady"};
const setVideo = {type: "setVideo", video: videoId,};
websocket.onopen = function () {
console.log('[open] Connection established');
websocket.send(JSON.stringify(playerReady));
websocket.send(JSON.stringify(setVideo));
setTimeout(() => websocket.close(), 5000);
};
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');
}
}
websocket.onerror = function (error) {
alert(`[error] ${error.message}`);
};
} else { } else {
chrome.runtime.openOptionsPage(); chrome.runtime.openOptionsPage();
} }
}); });
websocket = new WebSocket(WEBSOCKET_URL);
console.log('websocket created');
} else { } else {
console.error('websocket undefined or not OPEN') console.error('failed to add ' + info.linkurl + ': o(一︿一+)o not a valid link');
}
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');
} }
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "StammTV Helper", "name": "StammTV Helper",
"version": "1.3", "version": "1.4",
"action": { "action": {
"default_popup": "popup/popup.html" "default_popup": "popup/popup.html"
}, },

View File

@ -1,8 +1,19 @@
{ {
"name": "StammTV Helper",
"description": "Adds a context-menu option to send URLs to your instance of StammTV",
"version": "1.3",
"manifest_version": 2, "manifest_version": 2,
"name": "StammTV Helper",
"version": "1.4",
"browser_action": {
"default_title": "StammTV Context-Menu",
"default_popup": "popup/popup.html"
},
"default_locale": "en",
"description": "Adds a context-menu option to send URLs to your instance of StammTV",
"icons": {
"16": "stammtv-bitty.png",
"48": "stammtv-small.png",
"128": "stammtv-large.png"
},
"author": "The Founders of StammTV (panki27, and94x)",
"background": { "background": {
"scripts": [ "scripts": [
"script.js" "script.js"
@ -16,15 +27,5 @@
"permissions": [ "permissions": [
"storage", "storage",
"contextMenus" "contextMenus"
], ]
"browser_action": {
"default_title": "StammTV Context-Menu",
"default_popup": "popup/popup.html"
},
"icons": {
"16": "stammtv-bitty.png",
"48": "stammtv-small.png",
"128": "stammtv-large.png"
},
"default_locale": "en"
} }