From 470ae7e8d01190c08e54a8f2e6630faa190195d4 Mon Sep 17 00:00:00 2001 From: and94x Date: Wed, 17 Aug 2022 19:54:04 +0200 Subject: [PATCH] fixed first-run contextmenu not shown bug --- base/script.js | 40 ++++++++++++++++++------------------- base/settings/settings.html | 1 - base/settings/settings.js | 1 - 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/base/script.js b/base/script.js index 17056d0..ccbd1af 100644 --- a/base/script.js +++ b/base/script.js @@ -1,51 +1,51 @@ chrome.contextMenus.onClicked.addListener(addToStammTV); -let WEBSOCKET_URL = ""; +let WEBSOCKET_URL; let websocket; const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; -chrome.storage.local.get('firstRun', function (result) { - if (result.firstRun === false) { - chrome.storage.local.set({'firstRun': false}, function (result) { - chrome.runtime.openOptionsPage(); - }); - } else { +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://')) { - WEBSOCKET_URL = items.wssURL; - init(); } else { chrome.runtime.openOptionsPage(); } }); } -}); +}) -function init() { - chrome.contextMenus.removeAll(function() { - chrome.contextMenus.create({ - title: "Zu StammTV Playlist hinzufügen...", - contexts: ["link"], - id: CONTEXT_MENU_ID - }); +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\/)){1}([^#&?]*)(?:[?&]t=\d+)?.*/; + 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 { diff --git a/base/settings/settings.html b/base/settings/settings.html index e40e904..f226625 100644 --- a/base/settings/settings.html +++ b/base/settings/settings.html @@ -46,6 +46,5 @@ - \ No newline at end of file diff --git a/base/settings/settings.js b/base/settings/settings.js index b6ef8a4..411892a 100644 --- a/base/settings/settings.js +++ b/base/settings/settings.js @@ -28,7 +28,6 @@ function validateAndSave() { if (valid) { save(); - init(); } else { console.log('Failed to save settings - form validation was not successful.') saveButton.classList.replace('btn-primary', 'btn-danger');