-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
102 lines (86 loc) · 2.6 KB
/
popup.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
var message;
function onPopupShow() {
message = document.getElementById('message');
var query = { active: true, currentWindow: true };
chrome.tabs.query(query, processtab);
}
var matchers = [
matchYoutube,
matchIqiyi,
match56,
matchLetv,
matchQq,
matchQq2,
matchYouku,
matchSina,
matchTudou,
matchSohu,
matchYoutubePlaylist,
matchYoutubeChannel,
match17173
];
function processtab(tabs) {
var currentTab = tabs[0];
console.log(currentTab.url);
if(!currentTab.url) {
message.innerHTML = 'Put focus on webpage before clicking.'
return;
}
matchCaller(currentTab.url, matchers, matchCallback);
}
function matchCallback(match) {
if (match === null) {
console.log('domain not matched');
message.innerHTML = 'Invalid domain. Can only submit videos of the following websites: youtube, iqiyi, youku, tudou, 56, letv, sohu, qq, sina';
} else if (match.error) {
message.innerHTML = 'Matched domain, but couldn\'t add video. ' + match.error.message;
} else {
postToForm(match);
}
}
function matchCaller(url, callbackArray, done) {
var i = 0;
matchOne();
function doneFunction(match) {
if (match === null) {
i++;
matchOne();
} else {
done(match);
}
}
function matchOne() {
if (i >= callbackArray.length) {
done(null);
} else {
callbackArray[i](url, doneFunction);
}
}
}
function postToForm(match) {
var FORM_ID = '1kZ8E6hNBZl7EyCz7SSM2qEU6kDo4nM1F-a7ohIWNC7E';
var SOURCE_ENTRY = 'entry.407797214';
var VIDEOID_ENTRY = 'entry.1917016815';
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
var list = xmlhttp.responseXML.getElementsByClassName("error-message");
if (list.length > 0) {
message.innerHTML = 'Error submitting form! This is probably a bug. Please contact us.'
} else {
message.innerHTML = 'Video successfully sent!';
}
} else if (xmlhttp.status == 0 && xmlhttp.readyState == 4) {
message.innerHTML = 'Strange network error ' + xmlhttp.readyState + ' when submitting entry. Are you behind a firewall?';
} else {
message.innerHTML = 'Error ' + xmlhttp.status + ':' + xmlhttp.readyState + ' when submitting entry. Please contact us.';
}
}
}
xmlhttp.open('POST', 'https://docs.google.com/forms/d/' + FORM_ID + '/formResponse');
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.responseType = "document";
xmlhttp.send(SOURCE_ENTRY + '=' + match.source + '&' + VIDEOID_ENTRY + '=' + encodeURIComponent(match.videoId));
}
document.addEventListener('DOMContentLoaded', onPopupShow);