forked from zgao264/Pornhub-Video-Downloader-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
47 lines (45 loc) · 1.24 KB
/
content-script.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
function Func() {
return new Promise((resolve, reject) => {
var jsString = document.querySelector("#player >script:nth-child(2)")
if (!jsString) {
jsString = document.querySelector("#player >script:nth-child(1)")
}
jsString = jsString.innerHTML
jsString = ` var playerObjList = {};\n${jsString}`
var flashvars = jsString.match("flashvars_[0-9]{1,}")[0]
eval(jsString)
var jsObject = eval(flashvars)
resolve(jsObject)
})
}
Func().then(res => {
var videoType = []
Object.keys(res['mediaDefinitions']).forEach((item) => {
var itemInfo = res['mediaDefinitions'][item]
if (itemInfo['format'] == 'mp4') {
var mp4UrlJson = getMp4Url(itemInfo['videoUrl']);
for (const mp4Url of mp4UrlJson){
var obj = {
key: mp4Url['quality'],
val: mp4Url['videoUrl'],
video_title:res.video_title
}
videoType.push(obj)
}
}
})
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.cmd == 'test')
sendResponse(videoType);
});
})
function getMp4Url(url){
var xhr = new XMLHttpRequest();
xhr.open("GET", url , false);
xhr.onload = function(){
if (xhr.readyState === 4 && xhr.status === 200){
mp4UrlJson = JSON.parse(xhr.responseText);
}};
xhr.send();
return mp4UrlJson;
}