diff --git a/src/index.js b/src/index.js index 9c26f5e4..e6a606f5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,7 @@ -const CONTENT_TARGET_LAUNCH_PARAMETER = "contentTarget"; -const YOUTUBE_TV_URL = "https://www.youtube.com/tv"; - -function extractLaunchUrlParams(launchParameters) { - if (launchParameters === null || launchParameters === "") { - return null; - } - const launchParamJson = JSON.parse(launchParameters); - return launchParamJson[CONTENT_TARGET_LAUNCH_PARAMETER]; -} - -function concatenateUrlAndGetParams(ytUrl, path) { - if (!path) { - return ytUrl; - } else { - return ytUrl + "#?" + path; - } -} +import {extractLaunchParams, handleLaunch} from './utils'; function main() { - const launchParameters = window.PalmSystem - ? window.PalmSystem.launchParams - : window.launchParams; - const youtubeLaunchUrlPath = extractLaunchUrlParams(launchParameters); - if (typeof youtubeLaunchUrlPath === 'string' && youtubeLaunchUrlPath.indexOf('https://www.youtube.com/tv?') === 0) { - window.location = youtubeLaunchUrlPath; - } else { - window.location = concatenateUrlAndGetParams(YOUTUBE_TV_URL, youtubeLaunchUrlPath); - } + handleLaunch(extractLaunchParams()); } diff --git a/src/userScript.js b/src/userScript.js index 44838032..0577ba8f 100644 --- a/src/userScript.js +++ b/src/userScript.js @@ -3,6 +3,13 @@ import 'core-js/stable'; import 'regenerator-runtime/runtime'; import './domrect-polyfill'; +import {handleLaunch} from './utils'; + +document.addEventListener('webOSRelaunch', (evt) => { + console.info('RELAUNCH:', evt, window.launchParams); + handleLaunch(evt.detail); +}, true); + import './adblock.js'; import './sponsorblock.js'; import './ui.js'; diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 00000000..c8f0875f --- /dev/null +++ b/src/utils.js @@ -0,0 +1,24 @@ +export function extractLaunchParams() { + if (window.launchParams) { + return JSON.parse(window.launchParams); + } else { + return {}; + } +} + +export function handleLaunch(params) { + const { contentTarget } = params; + + if (contentTarget && typeof contentTarget === 'string') { + if (contentTarget.indexOf('https://www.youtube.com/tv?') === 0) { + console.info('Launching from direct contentTarget:', contentTarget); + window.location = contentTarget; + } else { + console.info('Launching from partial contentTarget:', contentTarget); + window.location = 'https://www.youtube.com/tv#?' + contentTarget; + } + } else { + console.info('Default launch'); + window.location = 'https://www.youtube.com/tv'; + } +}