forked from kaorun343/vue-youtube-embed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
65 lines (54 loc) · 1.51 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// fork from https://github.com/brandly/angular-youtube-embed
'use strict'
if (!String.prototype.includes) {
String.prototype.includes = function () {
'use strict'
return String.prototype.indexOf.apply(this, arguments) !== -1
}
}
const youtubeRegexp = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig
const timeRegexp = /t=(\d+)[ms]?(\d+)?s?/
/**
* get id from url
* @param {string} url url
* @return {string} id
*/
export function getIdFromURL (url) {
let id = url.replace(youtubeRegexp, '$1')
if (id.includes(';')) {
const pieces = id.split(';')
if (pieces[1].includes('%')) {
const uriComponent = decodeURIComponent(pieces[1])
id = `http:https://youtube.com${uriComponent}`.replace(youtubeRegexp, '$1')
} else {
id = pieces[0]
}
} else if (id.includes('#')) {
id = id.split('#')[0]
}
return id
}
/**
* get time from url
* @param {string} url url
* @return {number} time
*/
export function getTimeFromURL (url = '') {
const times = url.match(timeRegexp)
if (!times) {
return 0
}
const [full] = times
let [, minutes, seconds] = times
if (typeof seconds !== 'undefined') {
seconds = parseInt(seconds, 10)
minutes = parseInt(minutes, 10)
} else if (full.includes('m')) {
minutes = parseInt(minutes, 10)
seconds = 0
} else {
seconds = parseInt(minutes, 10)
minutes = 0
}
return seconds + (minutes * 60)
}