/
ClickToPlayer.js
112 lines (88 loc) · 2.79 KB
/
ClickToPlayer.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { extract, setProviderList } from 'oembed-parser'
import videoToBlock from '../../video-to-block'
// import { getYoutubeId, getDailymotionId, getFacebookId, getVimeoId, getTwitchId } from '../utils/get-video-id'
import providersJson from '../../../providers.json'
(function(){
console.log('ClickToPlayer')
setProviderList(providersJson)
let players = document.querySelectorAll('.lowweb__click-to-load')
players.forEach(( player )=>{
let videoUrl, oembedUrl
const type = player.dataset.lowtype
const data = videoToBlock[type]
// button
if( data.icon ){
player.innerHTML = data.icon
}
const id = getId( player.dataset.lowsrc, type )
if( data.video_url != '' && data.oembed != '' && id ) {
videoUrl = data.video_url.replace('##ID##', id)
if( videoUrl ){
oembedUrl = data.oembed + '?format=json&url='+encodeURIComponent(videoUrl)
chrome.runtime.sendMessage({msg: "oembed", options: {
type: type,
videoUrl: videoUrl,
oembedUrl: oembedUrl
}}, function(res) {
console.log(res);
});
console.log(videoUrl)
console.log(oembedUrl)
// extract(videoUrl, {mode:'no-cors', credentials:'include'}).then((oembed) => {
// console.log('result')
// console.log(oembed.text())
// console.log(oembed.json())
// return oembed.text()
// })
// .then((data) => {
// // resolve(data ? JSON.parse(data) : {})
// })
// .then((test) => {
// console.log('test', test)
// })
// .catch((error) => {
// console.error('error', error)
// // reject(error)
// })
// fetch(oembedUrl,{mode:'no-cors', credentials:'include'})
// .then(
// function(response) {
// if (response.status !== 200) {
// console.log('Looks like there was a problem. Status Code: ' +
// response.status);
// return;
// }
// // Examine the text in the response
// response.json().then(function(data) {
// console.log(data);
// });
// }
// )
// .catch(function(err) {
// console.log('Fetch Error :-S', err);
// });
}
}
})
}())
function getId( url, type ){
let id
switch( type ){
case 'youtube':
id = getYoutubeId( url )
break;
case 'vimeo':
id = getVimeoId( url )
break;
case 'dailymotion':
id = getDailymotionId( url )
break;
case 'twitch':
// id = getTwitchId( url )
break;
case 'facebook':
id = getFacebookId( url )
break;
}
return id
}