diff --git a/.idea/artifacts/chrome.xml b/.idea/artifacts/chrome.xml index 791b24b..0b9454c 100644 --- a/.idea/artifacts/chrome.xml +++ b/.idea/artifacts/chrome.xml @@ -1,5 +1,5 @@ - + $PROJECT_DIR$/out/artifacts/chrome diff --git a/.idea/artifacts/firefox.xml b/.idea/artifacts/firefox.xml deleted file mode 100644 index d7e47f3..0000000 --- a/.idea/artifacts/firefox.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - $PROJECT_DIR$/out/artifacts/firefox - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 8ace007..2a4dcdf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ ## Build - +```shell +cd base; find base -type f -exec zip out/artifacts/firefox/extension.crx {} + +find firefox -type f -exec zip out/artifacts/firefox/extension.crx {} + +zip -r out/artifacts/firefox/extension.crx base/* firefox/* +``` ## Prerequisites - Chromium based Browser (Chrome, Edge, ..) diff --git a/base/contextMenuHandler.js b/base/contextMenuHandler.js new file mode 100644 index 0000000..e69de29 diff --git a/base/popup/popup.html b/base/popup/popup.html index 281016f..56ff8e3 100644 --- a/base/popup/popup.html +++ b/base/popup/popup.html @@ -4,8 +4,9 @@ Popup - -→ Take me to StammTV! + +→ Take me to StammTV! +⚙ Settings \ No newline at end of file diff --git a/base/script.js b/base/script.js index 5a13e93..18b8f50 100644 --- a/base/script.js +++ b/base/script.js @@ -1,45 +1,64 @@ +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) { - firstRun(); + 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 firstRun(){ - chrome.storage.local.set({'firstRun': false}, function (result) { - chrome.tabs.create - }); - +function openSettings() { + chrome.tabs.create({ + active: true, + url: 'options/options.html' + }, null); } -const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; -const YOUTUBE_URL_REGEX = /^(?:https?:\/\/(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=|be\.com\/shorts\/)){1}([^#&?]*)(?:[?&]t=\d+)?.*/; - -let WEBSOCKET_URL = ""; -let websocket; - - -chrome.storage.local.get('wssURL', function (result) { - WEBSOCKET_URL = result.wssURL; -}); +function handleValidityCheckResult(validSettings) { + if (validSettings) { + addContextMenuItem(); + } else { + openSettings(); + } +} -chrome.contextMenus.create({ - title: "Zu StammTV Playlist hinzufügen...", - contexts: ["link"], - id: CONTEXT_MENU_ID -}); +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); + 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 playerReady = {"type": "playerReady"}; const setVideo = { type: "setVideo", @@ -69,4 +88,4 @@ function addToStammTV(info, tab) { console.error('o(一︿一+)o not a valid videoId'); } } -} \ No newline at end of file +} diff --git a/base/stammtv-bitty.png b/base/stammtv-bitty.png new file mode 100644 index 0000000..50ca342 Binary files /dev/null and b/base/stammtv-bitty.png differ diff --git a/base/stammtv-large.png b/base/stammtv-large.png new file mode 100644 index 0000000..241939a Binary files /dev/null and b/base/stammtv-large.png differ diff --git a/base/stammtv-small.png b/base/stammtv-small.png new file mode 100644 index 0000000..939cdf7 Binary files /dev/null and b/base/stammtv-small.png differ diff --git a/build-ff.sh b/build-ff.sh new file mode 100644 index 0000000..660ca52 --- /dev/null +++ b/build-ff.sh @@ -0,0 +1,15 @@ +#!/usr/bin/bash +PWD=$(pwd) +BASE_DIR=${PWD}/base +MANIFEST_DIR=${PWD}/firefox +OUT="../out/artifacts/firefox/extension.zip" + +echo $BASE_DIR +echo $MANIFEST_DIR +echo $OUT + +rm -f ${OUT} +cd "$BASE_DIR" || return +zip -r ${OUT} * +cd "${MANIFEST_DIR}" || return +zip -r ${OUT} manifest.json \ No newline at end of file diff --git a/chrome/manifest.json b/chrome/manifest.json index df26515..bd4be07 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -13,5 +13,10 @@ ], "action": { "default_popup": "popup/popup.html" + }, + "icons": { + "16": "stammtv-bitty.png", + "48": "stammtv-small.png", + "128": "stammtv-large.png" } } \ No newline at end of file diff --git a/firefox/manifest.json b/firefox/manifest.json index bcaa621..30261ba 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -4,7 +4,9 @@ "version": "1.0", "manifest_version": 2, "background": { - "scripts": ["script.js"] + "scripts": [ + "script.js" + ] }, "options_page": "options/options.html", "permissions": [ @@ -13,5 +15,10 @@ ], "action": { "default_popup": "popup/popup.html" + }, + "icons": { + "16": "icon-bitty.png", + "48": "icon-small.png", + "128": "icon-large.png" } } \ No newline at end of file diff --git a/out/artifacts/chrome/manifest.json b/out/artifacts/chrome/manifest.json index df26515..bd4be07 100644 --- a/out/artifacts/chrome/manifest.json +++ b/out/artifacts/chrome/manifest.json @@ -13,5 +13,10 @@ ], "action": { "default_popup": "popup/popup.html" + }, + "icons": { + "16": "stammtv-bitty.png", + "48": "stammtv-small.png", + "128": "stammtv-large.png" } } \ No newline at end of file diff --git a/out/artifacts/chrome/popup/popup.html b/out/artifacts/chrome/popup/popup.html index 281016f..56ff8e3 100644 --- a/out/artifacts/chrome/popup/popup.html +++ b/out/artifacts/chrome/popup/popup.html @@ -4,8 +4,9 @@ Popup - -→ Take me to StammTV! + +→ Take me to StammTV! +⚙ Settings \ No newline at end of file diff --git a/out/artifacts/chrome/script.js b/out/artifacts/chrome/script.js index 343fe03..18b8f50 100644 --- a/out/artifacts/chrome/script.js +++ b/out/artifacts/chrome/script.js @@ -1,31 +1,64 @@ -const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; -const YOUTUBE_URL_REGEX = /^(?:https?:\/\/(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=|be\.com\/shorts\/)){1}([^#&?]*)(?:[?&]t=\d+)?.*/; let WEBSOCKET_URL = ""; - let websocket; +const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; -chrome.storage.local.get('wssURL', function (result) { - WEBSOCKET_URL = result.wssURL; +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) + } + }); + } }); -chrome.contextMenus.create({ - title: "Zu StammTV Playlist hinzufügen...", - contexts: ["link"], - id: CONTEXT_MENU_ID -}); +function openSettings() { + chrome.tabs.create({ + active: true, + url: 'options/options.html' + }, null); +} + +function handleValidityCheckResult(validSettings) { + if (validSettings) { + addContextMenuItem(); + } else { + openSettings(); + } +} -chrome.contextMenus.onClicked.addListener(addToStammTV); +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 playerReady = {"type": "playerReady"}; const setVideo = { type: "setVideo", @@ -55,4 +88,4 @@ function addToStammTV(info, tab) { console.error('o(一︿一+)o not a valid videoId'); } } -} \ No newline at end of file +} diff --git a/out/artifacts/firefox/base.iml b/out/artifacts/firefox/base.iml deleted file mode 100644 index 183c2ef..0000000 --- a/out/artifacts/firefox/base.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/out/artifacts/firefox/firefox.iml b/out/artifacts/firefox/firefox.iml deleted file mode 100644 index 183c2ef..0000000 --- a/out/artifacts/firefox/firefox.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/out/artifacts/firefox/manifest.json b/out/artifacts/firefox/manifest.json deleted file mode 100644 index bcaa621..0000000 --- a/out/artifacts/firefox/manifest.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "StammTV Firefox Extension", - "description": "Adds a context-menu option to send URLs to StammTV", - "version": "1.0", - "manifest_version": 2, - "background": { - "scripts": ["script.js"] - }, - "options_page": "options/options.html", - "permissions": [ - "storage", - "contextMenus" - ], - "action": { - "default_popup": "popup/popup.html" - } -} \ No newline at end of file diff --git a/out/artifacts/firefox/options/options.html b/out/artifacts/firefox/options/options.html deleted file mode 100644 index bf4fdab..0000000 --- a/out/artifacts/firefox/options/options.html +++ /dev/null @@ -1,21 +0,0 @@ - - - - Options for StammTV Chrome Extension - - -
- - -
- -
- - - - - \ No newline at end of file diff --git a/out/artifacts/firefox/options/options.js b/out/artifacts/firefox/options/options.js deleted file mode 100644 index 7ef8389..0000000 --- a/out/artifacts/firefox/options/options.js +++ /dev/null @@ -1,32 +0,0 @@ -// Saves options to chrome.storage -function save_options() { - var stammTVBaseUrl = document.getElementById('stammtv-base-url').value;; - var stammTVWSSUrl = document.getElementById('stammtv-wss-url').value;; - chrome.storage.local.set({ - baseURL: stammTVBaseUrl, - wssURL: stammTVWSSUrl - }, function () { - // Update status to let user know options were saved. - var status = document.getElementById('status'); - status.textContent = 'Saved.'; - status.style.color = '00FF00'; - setTimeout(function () { - status.textContent = ''; - }, 1500); - }); -} - -// Restores select box and checkbox state using the preferences -// stored in chrome.storage. -function restore_options() { - chrome.storage.local.get({ - baseURL: 'Please fill in.', - wssURL: 'Please fill in.' - }, function (items) { - document.getElementById('stammtv-base-url').value = items.baseURL; - document.getElementById('stammtv-wss-url').value = items.wssURL; - }); -} - -document.addEventListener('DOMContentLoaded', restore_options); -document.getElementById('save').addEventListener('click', save_options); \ No newline at end of file diff --git a/out/artifacts/firefox/popup/popup.html b/out/artifacts/firefox/popup/popup.html deleted file mode 100644 index 281016f..0000000 --- a/out/artifacts/firefox/popup/popup.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Popup - - -→ Take me to StammTV! - - - \ No newline at end of file diff --git a/out/artifacts/firefox/popup/popup.js b/out/artifacts/firefox/popup/popup.js deleted file mode 100644 index 857c049..0000000 --- a/out/artifacts/firefox/popup/popup.js +++ /dev/null @@ -1,7 +0,0 @@ -function restore_options() { - chrome.storage.local.get('baseURL', function (result) { - document.getElementById('open-stammtv').href = result.baseURL; - }); -} - -document.addEventListener('DOMContentLoaded', restore_options); diff --git a/out/artifacts/firefox/script.js b/out/artifacts/firefox/script.js deleted file mode 100644 index 343fe03..0000000 --- a/out/artifacts/firefox/script.js +++ /dev/null @@ -1,58 +0,0 @@ -const CONTEXT_MENU_ID = "STAMMTV_ADD_URL_TO_PLAYLIST"; -const YOUTUBE_URL_REGEX = /^(?:https?:\/\/(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=|be\.com\/shorts\/)){1}([^#&?]*)(?:[?&]t=\d+)?.*/; -let WEBSOCKET_URL = ""; - -let websocket; - -chrome.storage.local.get('wssURL', function (result) { - WEBSOCKET_URL = result.wssURL; -}); - -chrome.contextMenus.create({ - title: "Zu StammTV Playlist hinzufügen...", - contexts: ["link"], - id: CONTEXT_MENU_ID -}); - -chrome.contextMenus.onClicked.addListener(addToStammTV); - -function parseYoutubeURL(url) { - 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'); - } - } -} \ No newline at end of file