From f450b048acb061adecc43b768a58d1ca85af68a6 Mon Sep 17 00:00:00 2001 From: Alex Buznik Date: Sat, 18 Jun 2022 16:47:47 +0300 Subject: [PATCH] start --- bg-loader.js | 5 + manifest.json | 9 +- src/background/index.js | 418 ++++++++++++++++++++-------------------- src/content/index.js | 6 +- 4 files changed, 226 insertions(+), 212 deletions(-) create mode 100644 bg-loader.js diff --git a/bg-loader.js b/bg-loader.js new file mode 100644 index 0000000..7876e68 --- /dev/null +++ b/bg-loader.js @@ -0,0 +1,5 @@ +try { + importScripts("/dist/background.js"); +} catch (e) { + console.error(e); +} diff --git a/manifest.json b/manifest.json index 010722b..f81635d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "StoPlay", "short_name": "StoPlay", "homepage_url": "http://stoplay.github.io/", @@ -34,10 +34,10 @@ } ], "background": { - "scripts": ["dist/background.js"] + "service_worker": "bg-loader.js" }, "permissions": ["tabs", "storage"], - "browser_action": { + "action": { "default_title": "StoPlay", "default_icon": "img/icon128.png" }, @@ -48,7 +48,6 @@ } }, "options_ui": { - "page": "dist/options/index.html", - "chrome_style": true + "page": "dist/options/index.html" } } diff --git a/src/background/index.js b/src/background/index.js index d04851c..a2dc1cb 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,250 +1,256 @@ -import {AppIcons} from "./models/AppIcons.js"; -import {ProvidersList} from "./models/ProvidersList.js"; -import {Actions} from "../common/Actions.js"; -import {Status} from "./models/Status.js"; -import {AppState} from "./services/AppState.js"; -import {Logger} from "./services/Logger.js"; +import { AppIcons } from "./models/AppIcons.js"; +import { ProvidersList } from "./models/ProvidersList.js"; +import { Actions } from "../common/Actions.js"; +import { Status } from "./models/Status.js"; +import { AppState } from "./services/AppState.js"; +import { Logger } from "./services/Logger.js"; const appState = AppState.getInstance(); const version = chrome.app.getDetails().version; const providersDefault = ProvidersList.map((item) => { - return {uri: item, enabled: true}; + return { uri: item, enabled: true }; }); function saveToOptions(dataObject) { - chrome.storage.sync.set(dataObject, () => { - Logger.log('STOPLAY saveToOptions saved'); - }); + chrome.storage.sync.set(dataObject, () => { + Logger.log("STOPLAY saveToOptions saved"); + }); } // Get options from chrome.storage.sync. function restoreOptions(callback) { - chrome.storage.sync.get({ - enabled: true, - providers: providersDefault - }, (items) => { - const providersCurrent = mergeProviders(items.providers); - - if (typeof callback === 'function') { - callback(providersCurrent); - } - }); + chrome.storage.sync.get( + { + enabled: true, + providers: providersDefault, + }, + (items) => { + const providersCurrent = mergeProviders(items.providers); + + if (typeof callback === "function") { + callback(providersCurrent); + } + } + ); } function onFirstRun() { - Logger.log('STOPLAY first_run'); - appState.setVersion(version); - saveToOptions({providers: providersDefault}); + Logger.log("STOPLAY first_run"); + appState.setVersion(version); + saveToOptions({ providers: providersDefault }); } // find missing providers and add from defaults function mergeProviders(oldItems) { - if (!Array.isArray(oldItems)) { - Logger.log('STOPLAY mergeProviders returning default values'); - return providersDefault; - } - - Logger.log('STOPLAY mergeProviders', oldItems); - - return providersDefault.map(function(itemDefault) { - // looking if any of the new items have appeared - // in older version of settings - const found = oldItems.find((itemOld) => { - return itemOld.uri === itemDefault.uri; - }); - - // if not found, add it - if (!found) { - return itemDefault; - } else { - return found; - } - }); + if (!Array.isArray(oldItems)) { + Logger.log("STOPLAY mergeProviders returning default values"); + return providersDefault; + } + + Logger.log("STOPLAY mergeProviders", oldItems); + + return providersDefault.map(function (itemDefault) { + // looking if any of the new items have appeared + // in older version of settings + const found = oldItems.find((itemOld) => { + return itemOld.uri === itemDefault.uri; + }); + + // if not found, add it + if (!found) { + return itemDefault; + } else { + return found; + } + }); } function resetProviders(callback) { - restoreOptions(function(providersMerged) { - saveToOptions({providers: providersMerged}); - if (typeof callback === 'function') { - callback(providersMerged); - } - }) + restoreOptions(function (providersMerged) { + saveToOptions({ providers: providersMerged }); + if (typeof callback === "function") { + callback(providersMerged); + } + }); } appState.setStatus("silent"); if (!appState.getVersion()) { - // first run - onFirstRun(); + // first run + onFirstRun(); } else if (appState.getVersion() !== version) { - // extension updated - appState.setVersion(version); - resetProviders(); + // extension updated + appState.setVersion(version); + resetProviders(); } chrome.storage.onChanged.addListener((changes, namespace) => { - for (const key in changes) { - const storageChange = changes[key]; + for (const key in changes) { + const storageChange = changes[key]; - if (namespace === "sync" && key === "enabled") { - let icon = AppIcons.PLAY_ICON; + if (namespace === "sync" && key === "enabled") { + let icon = AppIcons.PLAY_ICON; - if (storageChange.newValue !== true) { - icon = AppIcons.DISABLED_ICON; - } + if (storageChange.newValue !== true) { + icon = AppIcons.DISABLED_ICON; + } - chrome.browserAction.setIcon({path: icon}); - } - } + chrome.action.setIcon({ path: icon }); + } + } }); -chrome.browserAction.onClicked.addListener(() => { - const lastPlayingTabId = appState.getLastPlayingTabId(); - const lastPlayingFrameId = appState.getLastPlayingFrameId() || 0; - const lastPausedTabId = appState.getLastPausedTabId(); - const lastPausedFrameId = appState.getLastPausedFrameId() || 0; - const status = appState.getStatus(); - - switch (status) { - case Status.PLAYING: - if (lastPlayingTabId) { - chrome.tabs.sendMessage( - lastPlayingTabId, - {action: Actions.PAUSE}, - {frameId: lastPlayingFrameId} - ); - } - break; - - case Status.PAUSED: - if (lastPlayingTabId) { - chrome.tabs.sendMessage( - lastPausedTabId, - {action: Actions.PLAY}, - {frameId: lastPausedFrameId} - ); - } - break; - } -}) +chrome.action.onClicked.addListener(() => { + const lastPlayingTabId = appState.getLastPlayingTabId(); + const lastPlayingFrameId = appState.getLastPlayingFrameId() || 0; + const lastPausedTabId = appState.getLastPausedTabId(); + const lastPausedFrameId = appState.getLastPausedFrameId() || 0; + const status = appState.getStatus(); + + switch (status) { + case Status.PLAYING: + if (lastPlayingTabId) { + chrome.tabs.sendMessage( + lastPlayingTabId, + { action: Actions.PAUSE }, + { frameId: lastPlayingFrameId } + ); + } + break; + + case Status.PAUSED: + if (lastPlayingTabId) { + chrome.tabs.sendMessage( + lastPausedTabId, + { action: Actions.PLAY }, + { frameId: lastPausedFrameId } + ); + } + break; + } +}); chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - const lastPlayingTabId = appState.getLastPlayingTabId(); - const lastPlayingFrameId = appState.getLastPlayingFrameId() || 0; - const lastPausedTabId = appState.getLastPausedTabId(); - const status = appState.getStatus(); - const isOptionsPage = sender.url.indexOf(chrome.runtime.id) > -1; - const hasNoAction = !request.action; - const hasNoTab = !sender.tab && !isOptionsPage; - - if (hasNoAction || hasNoTab) { - return; - } - - switch (request.action) { - case 'updateTitle': - if (!request.title) { - break; - } - - chrome.browserAction.setTitle({title: "Playing: " + request.title}); - break; - - case 'started': - const isFrameIdChanged = (lastPlayingTabId && sender.frameId != lastPlayingFrameId); - const hasLastPlayingTabId = Boolean(lastPlayingTabId); - const senderIsNotLastPlaying = sender.tab.id !== lastPlayingTabId; - - // pause previously playing tab - if (hasLastPlayingTabId && senderIsNotLastPlaying || isFrameIdChanged) { - chrome.tabs.sendMessage( - lastPlayingTabId, - {action: Actions.PAUSE}, - {frameId: lastPlayingFrameId} - ); - } - - appState.setLastPlayingTabId(sender.tab.id); - appState.setLastPlayingFrameId(sender.frameId); - appState.setStatus(Status.PLAYING); - - chrome.browserAction.setIcon({path: AppIcons.STOP_ICON}); - - if (request.title) { - chrome.browserAction.setTitle({title: "Playing: " + request.title}); - } else { - chrome.browserAction.setTitle({title: "Playing: " + sender.tab.title}); - } - break; - - case 'paused': - appState.setLastPausedTabId(sender.tab.id); - appState.setLastPausedFrameId(sender.frameId); - appState.setStatus(Status.PAUSED); - - chrome.browserAction.setIcon({path: AppIcons.PLAY_ICON}); - chrome.browserAction.setTitle({title: "StoPlay"}); - break; - - case 'toggle': - if (!lastPlayingTabId) { - break; - } - - const action = status === Status.PLAYING - ? Actions.PAUSE - : Actions.PLAY; - - chrome.tabs.sendMessage(lastPlayingTabId, {action: action}); - - break; - - case 'resetProviders': - resetProviders((providers) => { - sendResponse({ - providers - }) - }); - break; - } + const lastPlayingTabId = appState.getLastPlayingTabId(); + const lastPlayingFrameId = appState.getLastPlayingFrameId() || 0; + const lastPausedTabId = appState.getLastPausedTabId(); + const status = appState.getStatus(); + const isOptionsPage = sender.url.indexOf(chrome.runtime.id) > -1; + const hasNoAction = !request.action; + const hasNoTab = !sender.tab && !isOptionsPage; + + if (hasNoAction || hasNoTab) { + return; + } + + switch (request.action) { + case "updateTitle": + if (!request.title) { + break; + } + + chrome.action.setTitle({ title: "Playing: " + request.title }); + break; + + case "started": + const isFrameIdChanged = + lastPlayingTabId && sender.frameId != lastPlayingFrameId; + const hasLastPlayingTabId = Boolean(lastPlayingTabId); + const senderIsNotLastPlaying = sender.tab.id !== lastPlayingTabId; + + // pause previously playing tab + if ((hasLastPlayingTabId && senderIsNotLastPlaying) || isFrameIdChanged) { + chrome.tabs.sendMessage( + lastPlayingTabId, + { action: Actions.PAUSE }, + { frameId: lastPlayingFrameId } + ); + } + + appState.setLastPlayingTabId(sender.tab.id); + appState.setLastPlayingFrameId(sender.frameId); + appState.setStatus(Status.PLAYING); + + chrome.action.setIcon({ path: AppIcons.STOP_ICON }); + + if (request.title) { + chrome.action.setTitle({ title: "Playing: " + request.title }); + } else { + chrome.action.setTitle({ title: "Playing: " + sender.tab.title }); + } + break; + + case "paused": + appState.setLastPausedTabId(sender.tab.id); + appState.setLastPausedFrameId(sender.frameId); + appState.setStatus(Status.PAUSED); + + chrome.action.setIcon({ path: AppIcons.PLAY_ICON }); + chrome.action.setTitle({ title: "StoPlay" }); + break; + + case "toggle": + if (!lastPlayingTabId) { + break; + } + + const action = status === Status.PLAYING ? Actions.PAUSE : Actions.PLAY; + + chrome.tabs.sendMessage(lastPlayingTabId, { action: action }); + + break; + + case "resetProviders": + resetProviders((providers) => { + sendResponse({ + providers, + }); + }); + break; + } }); chrome.commands.onCommand.addListener(() => { - const lastPlayingTabId = appState.getLastPlayingTabId(); - const lastPlayingFrameId = appState.getLastPlayingFrameId() || 0; - const lastPausedTabId = appState.getLastPausedTabId(); - const lastPausedFrameId = appState.getLastPausedFrameId() || 0; - const status = appState.getStatus(); - - let action = Actions.PAUSE; - let frameId = lastPlayingFrameId; - let tabId = lastPlayingTabId; - - if (status !== Status.PLAYING) { - tabId = lastPausedTabId; - action = Actions.PLAY; - frameId = lastPausedFrameId; - } - - if (tabId) { - chrome.tabs.sendMessage(tabId, {action}, {frameId}); - } + const lastPlayingTabId = appState.getLastPlayingTabId(); + const lastPlayingFrameId = appState.getLastPlayingFrameId() || 0; + const lastPausedTabId = appState.getLastPausedTabId(); + const lastPausedFrameId = appState.getLastPausedFrameId() || 0; + const status = appState.getStatus(); + + let action = Actions.PAUSE; + let frameId = lastPlayingFrameId; + let tabId = lastPlayingTabId; + + if (status !== Status.PLAYING) { + tabId = lastPausedTabId; + action = Actions.PLAY; + frameId = lastPausedFrameId; + } + + if (tabId) { + chrome.tabs.sendMessage(tabId, { action }, { frameId }); + } }); chrome.tabs.onRemoved.addListener((tabId) => { - const lastPlayingTabId = appState.getLastPlayingTabId(); - const lastPausedTabId = appState.getLastPausedTabId(); - const lastPausedFrameId = appState.getLastPausedFrameId() || 0; - - if (tabId === lastPlayingTabId) { - appState.setLastPlayingTabId(null); - - if (lastPausedTabId !== tabId) { - chrome.tabs.sendMessage( - lastPausedTabId, - {action: Actions.PLAY}, - {frameId: lastPausedFrameId} - ); - } - } + const lastPlayingTabId = appState.getLastPlayingTabId(); + const lastPausedTabId = appState.getLastPausedTabId(); + const lastPausedFrameId = appState.getLastPausedFrameId() || 0; + + if (tabId === lastPlayingTabId) { + appState.setLastPlayingTabId(null); + + if (lastPausedTabId !== tabId) { + chrome.tabs.sendMessage( + lastPausedTabId, + { action: Actions.PLAY }, + { frameId: lastPausedFrameId } + ); + } + } +}); + +self.addEventListener("install", (event) => { + console.log("sw install"); }); diff --git a/src/content/index.js b/src/content/index.js index f335a75..2a8a827 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -288,7 +288,11 @@ class Provider { if (stoplayLastStatus !== currentStatus) { stoplayLastStatus = currentStatus; - window.localStorage.setItem('stoplaystate', currentStatus); + let title; + if (navigator.mediaSession) { + title = navigator.mediaSession.metadata.title + ' - ' + navigator.mediaSession.metadata.artist; + } + chrome.runtime.sendMessage({ action: "started", title }); } }, 400); `);