fixed a bug where websocket was not handled properly
master
and94x 2 years ago
parent 04948e24f9
commit d87a264d87

@ -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);
} else {
chrome.runtime.openOptionsPage(); const playerReady = {"type": "playerReady"};
} const setVideo = {type: "setVideo", video: videoId,};
});
websocket = new WebSocket(WEBSOCKET_URL);
console.log('websocket created');
} else {
console.error('websocket undefined or not OPEN')
}
websocket.onopen = function () { websocket.onopen = function () {
console.log('websocket ready - sending'); console.log('[open] Connection established');
const playerReady = {"type": "playerReady"};
websocket.send(JSON.stringify(playerReady)); websocket.send(JSON.stringify(playerReady));
console.log(playerReady); websocket.send(JSON.stringify(setVideo));
const setVideo = { setTimeout(() => websocket.close(), 5000);
type: "setVideo",
video: videoId,
}; };
websocket.send(JSON.stringify(setVideo));
console.log(setVideo); websocket.onmessage = function (event) {
console.log(`[message] Data received from server: ${event.data}`);
}; };
setTimeout(function () { websocket.onclose = function (event) {
websocket.close(); if (event.wasClean) {
console.log('websocket closed'); console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`)
}, 1000); } else {
console.log('[close] Connection died');
}
}
websocket.onerror = function (error) {
alert(`[error] ${error.message}`);
};
} else {
chrome.runtime.openOptionsPage();
}
});
} else { } else {
console.error('o(一︿一+)o not a valid videoId'); console.error('failed to add ' + info.linkurl + ': o(一︿一+)o not a valid link');
} }
} }
} }

@ -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"
}, },

@ -1,8 +1,19 @@
{ {
"manifest_version": 2,
"name": "StammTV Helper", "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", "description": "Adds a context-menu option to send URLs to your instance of StammTV",
"version": "1.3", "icons": {
"manifest_version": 2, "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"
} }
Loading…
Cancel
Save