diff options
author | Sam Potts <me@sampotts.me> | 2018-01-12 19:35:46 +1100 |
---|---|---|
committer | Sam Potts <me@sampotts.me> | 2018-01-12 19:35:46 +1100 |
commit | d9ec1d1b8e251cf30509e88a76132c0e04f8c00d (patch) | |
tree | 7b342f44dce8855680776b904678ce03c03a11ce /src/js/utils.js | |
parent | 2e5d06ad8529f99c607e3f2e4c678f07e6973274 (diff) | |
download | plyr-d9ec1d1b8e251cf30509e88a76132c0e04f8c00d.tar.lz plyr-d9ec1d1b8e251cf30509e88a76132c0e04f8c00d.tar.xz plyr-d9ec1d1b8e251cf30509e88a76132c0e04f8c00d.zip |
Progressively enhance <iframe> embeds
Diffstat (limited to 'src/js/utils.js')
-rw-r--r-- | src/js/utils.js | 75 |
1 files changed, 71 insertions, 4 deletions
diff --git a/src/js/utils.js b/src/js/utils.js index d9dd3df1..930d3e9f 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -3,6 +3,7 @@ // ========================================================================== import support from './support'; +import { providers } from './types'; const utils = { // Check variable types @@ -103,7 +104,7 @@ const utils = { element.callbacks.forEach(cb => cb.call(null, event)); element.callbacks = null; }, - false + false, ); } @@ -168,7 +169,7 @@ const utils = { prefix + id, JSON.stringify({ content: text, - }) + }), ); } @@ -274,6 +275,17 @@ const utils = { } }, + // Replace element + replaceElement(newChild, oldChild) { + if (!utils.is.element(oldChild) || !utils.is.element(oldChild.parentNode) || !utils.is.element(newChild)) { + return null; + } + + oldChild.parentNode.replaceChild(newChild, oldChild); + + return newChild; + }, + // Set attributes setAttributes(element, attributes) { if (!utils.is.element(element) || utils.is.empty(attributes)) { @@ -491,7 +503,7 @@ const utils = { event.preventDefault(); } }, - false + false, ); }, @@ -617,14 +629,37 @@ const utils = { return utils.extend(target, ...sources); }, + // Get the provider for a given URL + getProviderByUrl(url) { + // YouTube + if (/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/.test(url)) { + return providers.youtube; + } + + // Vimeo + if (/^https?:\/\/player.vimeo.com\/video\/\d{8,}(?=\b|\/)/.test(url)) { + return providers.vimeo; + } + + return null; + }, + // Parse YouTube ID from URL parseYouTubeId(url) { + if (utils.is.empty(url)) { + return null; + } + const regex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/; return url.match(regex) ? RegExp.$2 : url; }, // Parse Vimeo ID from URL parseVimeoId(url) { + if (utils.is.empty(url)) { + return null; + } + if (utils.is.number(Number(url))) { return url; } @@ -633,8 +668,40 @@ const utils = { return url.match(regex) ? RegExp.$2 : url; }, + // Convert a URL to a location object + parseUrl(url) { + const parser = document.createElement('a'); + parser.href = url; + return parser; + }, + + // Get URL query parameters + getUrlParams(input) { + let search = input; + + // Parse URL if needed + if (input.startsWith('http://') || input.startsWith('https://')) { + ({ search } = this.parseUrl(input)); + } + + if (this.is.empty(search)) { + return null; + } + + const hashes = search.slice(search.indexOf('?') + 1).split('&'); + + return hashes.reduce((params, hash) => { + const [ + key, + val, + ] = hash.split('='); + + return Object.assign(params, { [key]: decodeURIComponent(val) }); + }, {}); + }, + // Convert object to URL parameters - buildUrlParameters(input) { + buildUrlParams(input) { if (!utils.is.object(input)) { return ''; } |