From d87a264d87825a96b701c55aadd5db10cbb2a84d Mon Sep 17 00:00:00 2001 From: and94x Date: Wed, 17 Aug 2022 21:09:17 +0200 Subject: [PATCH] bump 1.4 fixed a bug where websocket was not handled properly --- base/script.js | 77 ++++++++++++++++++++++--------------------- chrome/manifest.json | 2 +- firefox/manifest.json | 27 +++++++-------- 3 files changed, 54 insertions(+), 52 deletions(-) diff --git a/base/script.js b/base/script.js index ccbd1af..29c1581 100644 --- a/base/script.js +++ b/base/script.js @@ -1,8 +1,4 @@ -chrome.contextMenus.onClicked.addListener(addToStammTV); - -let WEBSOCKET_URL; -let websocket; -const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; +chrome.contextMenus.onClicked.addListener(addToStammTVHandler); chrome.runtime.onInstalled.addListener(function (details) { 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.create({ 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 - let videoId = parseYoutubeURL(info.linkUrl) + console.log('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') - } + let websocket; + chrome.storage.local.get('wssURL', function (result) { + if (result.wssURL) { + websocket = new WebSocket(result.wssURL); + + const playerReady = {"type": "playerReady"}; + const setVideo = {type: "setVideo", video: videoId,}; - websocket.onopen = function () { - console.log('websocket ready - sending'); + websocket.onopen = function () { + console.log('[open] Connection established'); - const playerReady = {"type": "playerReady"}; - websocket.send(JSON.stringify(playerReady)); - console.log(playerReady); + websocket.send(JSON.stringify(playerReady)); + websocket.send(JSON.stringify(setVideo)); - const setVideo = { - type: "setVideo", - video: videoId, - }; - websocket.send(JSON.stringify(setVideo)); - console.log(setVideo); - }; + setTimeout(() => websocket.close(), 5000); + }; - setTimeout(function () { - websocket.close(); - console.log('websocket closed'); - }, 1000); + 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 { + chrome.runtime.openOptionsPage(); + } + }); } else { - console.error('o(一︿一+)o not a valid videoId'); + console.error('failed to add ' + info.linkurl + ': o(一︿一+)o not a valid link'); } } } diff --git a/chrome/manifest.json b/chrome/manifest.json index 09887b3..4b21c51 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "StammTV Helper", - "version": "1.3", + "version": "1.4", "action": { "default_popup": "popup/popup.html" }, diff --git a/firefox/manifest.json b/firefox/manifest.json index 82eb2b8..bec14bf 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -1,8 +1,19 @@ { + "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", - "version": "1.3", - "manifest_version": 2, + "icons": { + "16": "stammtv-bitty.png", + "48": "stammtv-small.png", + "128": "stammtv-large.png" + }, + "author": "The Founders of StammTV (panki27, and94x)", "background": { "scripts": [ "script.js" @@ -16,15 +27,5 @@ "permissions": [ "storage", "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" + ] } \ No newline at end of file