Skip to content

Commit

Permalink
userscript: support relaunches
Browse files Browse the repository at this point in the history
  • Loading branch information
Informatic committed Jun 12, 2021
1 parent 4910cff commit 4f8fc37
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
29 changes: 2 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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());
}


Expand Down
7 changes: 7 additions & 0 deletions src/userScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
24 changes: 24 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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';
}
}

0 comments on commit 4f8fc37

Please sign in to comment.