Skip to content

Instantly share code, notes, and snippets.

@blacklight
Created July 7, 2020 18:39
Show Gist options
  • Save blacklight/a1262a376bcf57db7d58578dd791c682 to your computer and use it in GitHub Desktop.
Save blacklight/a1262a376bcf57db7d58578dd791c682 to your computer and use it in GitHub Desktop.
// Sample Platypush user script to cast the current tab or any media item selected
// on the page to the default Chromecast device configured in Platypush.
async (app, args) => {
const baseURL = await app.getURL();
// Default URL to cast: current page URL
let url = baseURL;
if (args.target) {
// The user executed the action from a context menu
const target = app.HTML2DOM(args.target);
// If it's a <video> or <audio> HTML element then it will have a <source> tag
const src = target.querySelector('source');
// Otherwise, check if it's a link
const link = target.tagName.toLowerCase() === 'a' ? target : target.querySelector('a');
if (src) {
url = src.attributes.src.value;
} else if (link) {
url = link.attributes.href.value;
}
}
// Resolve relative URLs
if (!url.match('^https?://[^/]+') && !url.startsWith('magnet:?')) {
url = baseURL.match('(^https?://[^/]+)')[1] + (url || '');
}
// Finally, cast the media
await app.run({
action: 'media.chromecast.play',
args: {
resource: url,
}
}, args.host);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment