-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.js
More file actions
241 lines (220 loc) · 8.71 KB
/
resource.js
File metadata and controls
241 lines (220 loc) · 8.71 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/** Author Details
* @name Vasu Jain
* @email vj@brk.im
* @date 03/08/15
* @license Gnu Public License (GPL)
*/
/**
* Global Variables
* @type {string} redirectUrl : Setting up a default Redirect URL
* @type {string} divOutputHtml : Sets Output Div HTML content
*
*/
var redirectUrl = "http://www.google.com";
var divOutputHtml = "";
/**
* Run script as soon as the document's DOM is ready.
*/
document.addEventListener('DOMContentLoaded', function () {
getCurrentTab();
//document.getElementById('buttonId').addEventListener('click', function(){}
});
/**
* Get the current tab from where the Extn. is invoked
*/
function getCurrentTab() {
return chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
checkCurrentTab(tabs[0].url, tabs[0].id);
});
}
/**
* RegEx stuff and redirection to embed URL for youtube
*/
function checkCurrentTab(tabUrl, tabId) {
var yt = fillYouTubeUrl(tabUrl, tabId);
var fb = fillFacebookUrl(tabUrl, tabId);
var vm = fillVimeoUrl(tabUrl, tabId);
var dm = fillDailyMotionUrl(tabUrl, tabId);
// Display Invalid URL if URL does not match any supported channel
if(!yt && !fb && !vm && !dm) {
displayInvalidUrl(tabUrl);
return false;
} else {
return true;
}
}
/**
* Check Current URL for Youtube URL, than redirect
* @param: Current URL
* @param: Tab Id
* @return: boolean true(Valid URL)/false(invalid URL)
*
*/
function fillYouTubeUrl(tabUrl, tabId) {
//Nasty Youtube URL Regex... can be improved for mobile URLs and international Domains like .in,.uk etc
var regExServiceUrl = /(http\:\/\/|https\:\/\/)?((www\.)youtube\.com|youtu\.?be)(\/watch\?)((\S*\&v\=)|(v\=))([_|\-|a-z|0-9|A-Z]+)/;
var matchServiceUrl = regExServiceUrl.exec(tabUrl);
var regExEmbedUrl = /(http\:\/\/|https\:\/\/)?((www\.)youtube\.com|youtu\.?be)(\/embed\/)(\S{11})/;
var matchEmbedUrl = regExEmbedUrl.exec(tabUrl);
//Check if URL is a valid YouTube URL
if (matchServiceUrl == null && matchEmbedUrl == null) {
return false;
} else {
if(matchEmbedUrl==null) {
console.log(matchServiceUrl);
console.log(matchServiceUrl[8]);
//Sanity check... to set up the new URL
if (matchServiceUrl.length >= 8) {
matchServiceUrl[4] = "\/embed\/";
if(matchServiceUrl[8].length < 11) {
return false;
}
}
redirectUrl = matchServiceUrl[1] + matchServiceUrl[2] + matchServiceUrl[4] + matchServiceUrl[8] + "?autoplay=1";
updateRedirectUrl(matchServiceUrl, tabUrl, tabId, true, redirectUrl);
} else if (matchServiceUrl == null) {
if (matchEmbedUrl.length >= 5) {
matchEmbedUrl[4] = "\/watch\?v=";
}
redirectUrl = matchEmbedUrl[1] + matchEmbedUrl[2] + matchEmbedUrl[4] + matchEmbedUrl[5];
updateRedirectUrl(matchEmbedUrl, tabUrl, tabId, true, redirectUrl);
}
return true;
}
}
/**
* Check Current URL for Facebook URL, than redirect
* @param: Current URL
* @param: Tab Id
* @return: boolean true(Valid URL)/false(invalid URL)
*
*/
function fillFacebookUrl(tabUrl, tabId) {
//Nasty Facebook URL Regex... can be improved for mobile URLs and international Domains like .in,.uk etc
var regExServiceUrl = /(http\:\/\/|https\:\/\/)?((www\.)facebook\.com)(\/video\.php\?v=)(\d{1,20})/;
var matchServiceUrl = regExServiceUrl.exec(tabUrl);
var regExEmbedUrl = /(http\:\/\/|https\:\/\/)?((www\.)facebook\.com)(\/video\/embed\?video_id=)(\d{1,20})/;
var matchEmbedUrl = regExEmbedUrl.exec(tabUrl);
//Check if URL is a valid Facebook URL
if (matchServiceUrl == null && matchEmbedUrl == null) {
return false;
} else {
if(matchEmbedUrl==null) {
//Sanity check... to set up the new URL
if (matchServiceUrl.length >= 6) {
matchServiceUrl[4] = "\/video\/embed\?video_id=";
}
redirectUrl = matchServiceUrl[1] + matchServiceUrl[2] + matchServiceUrl[4] + matchServiceUrl[5];
updateRedirectUrl(matchServiceUrl, tabUrl, tabId, true, redirectUrl);
} else if (matchServiceUrl == null) {
if (matchEmbedUrl.length >= 6) {
matchEmbedUrl[4] = "\/video\.php\?v=";
}
redirectUrl = matchEmbedUrl[1] + matchEmbedUrl[2] + matchEmbedUrl[4] + matchEmbedUrl[5];
updateRedirectUrl(matchEmbedUrl, tabUrl, tabId, true, redirectUrl);
}
return true;
}
}
/**
* Check Current URL for Vimeo URL, than redirect
* @param: Current URL
* @param: Tab Id
* @return: boolean true(Valid URL)/false(invalid URL)
*
*/
function fillVimeoUrl(tabUrl, tabId) {
//Nasty Vimeo URL Regex... can be improved for mobile URLs and international Domains like .in,.uk etc
var regExServiceUrl = /(http\:\/\/|https\:\/\/)?(vimeo\.com\/)(\d{1,20})/;
var matchServiceUrl = regExServiceUrl.exec(tabUrl);
var regExEmbedUrl = /(http\:\/\/|https\:\/\/)?(player\.)(vimeo\.com\/)(video\/)(\d{1,20})/;
var matchEmbedUrl = regExEmbedUrl.exec(tabUrl);
//Check if URL is a valid YouTube URL
if (matchServiceUrl == null && matchEmbedUrl == null) {
return false;
} else {
if(matchEmbedUrl==null) {
//Sanity check... to set up the new URL
if (matchServiceUrl.length >= 4) {
matchServiceUrl[4] = "player.";
matchServiceUrl[5] = "video\/";
matchServiceUrl[6] = "?autoplay=1";
}
redirectUrl = matchServiceUrl[1] + matchServiceUrl[4] + matchServiceUrl[2] + matchServiceUrl[5] + matchServiceUrl[3] + matchServiceUrl[6];
updateRedirectUrl(matchServiceUrl, tabUrl, tabId, true, redirectUrl);
} else if (matchServiceUrl == null) {
redirectUrl = matchEmbedUrl[1] + matchEmbedUrl[3] + matchEmbedUrl[5];
updateRedirectUrl(matchEmbedUrl, tabUrl, tabId, true, redirectUrl);
}
return true;
}
}
/**
* Check Current URL for DailyMotion URL, than redirect
* @param: Current URL
* @param: Tab Id
* @return: boolean true(Valid URL)/false(invalid URL)
*
*/
function fillDailyMotionUrl(tabUrl, tabId) {
//Nasty DailyMotion URL Regex... can be improved for mobile URLs and international Domains like .in,.uk etc
var regExServiceUrl = /(http\:\/\/|https\:\/\/)?((www\.)dailymotion\.com\/)(video\/)([a-z0-9]+)(\_*)(\S*)/;
var matchServiceUrl = regExServiceUrl.exec(tabUrl);
var regExEmbedUrl = /(http\:\/\/|https\:\/\/)?((www\.)dailymotion\.com\/)(embed\/)(video\/)([a-z0-9]+)/;
var matchEmbedUrl = regExEmbedUrl.exec(tabUrl);
//Check if URL is a valid YouTube URL
if (matchServiceUrl == null && matchEmbedUrl == null) {
return false;
} else {
if(matchEmbedUrl==null) {
//Sanity check... to set up the new URL
if (matchServiceUrl.length >= 5) {
matchServiceUrl[8] = "embed\/";
}
redirectUrl = matchServiceUrl[1] + matchServiceUrl[2] + matchServiceUrl[8] + matchServiceUrl[4] + matchServiceUrl[5];
updateRedirectUrl(matchServiceUrl, tabUrl, tabId, true, redirectUrl);
} else if (matchServiceUrl == null) {
redirectUrl = matchEmbedUrl[1] + matchEmbedUrl[2] + matchEmbedUrl[5] + matchEmbedUrl[6];
updateRedirectUrl(matchEmbedUrl, tabUrl, tabId, true, redirectUrl);
}
return true;
}
}
/**
* RegEx stuff and redirection to embed URL for youtube
* @param: regex match object
* @param: Tab Id
*
*/
function updateRedirectUrl(regexMatch, tabUrl, tabId, redirectFlag, redirectUrl) {
if(redirectFlag) {
divOutputHtml += "<div class='validUrl'><b>Redirected from: </b><br/><span class='urlText'>" + regexMatch[0] + "</span></div>";
divOutputHtml += "<div class='validUrl'><b>Redirected To: </b><br/><span class='urlText'>" + redirectUrl + "</span></div>";
document.getElementById("convertUrl").innerHTML = divOutputHtml;
chrome.tabs.update(tabId, {url: redirectUrl});
}
}
/**
* Display Invalid URL Text
*/
function displayInvalidUrl(tabUrl) {
tabUrl = trimLongUrl(tabUrl, 42);
divOutputHtml = "<div class='invalidURL'><b>Invalid URL: </b><br><span class='urlText'>" + tabUrl + "</span></div>";
document.getElementById("convertUrl").innerHTML = divOutputHtml;
}
/**
* Trim a URL to fit screen
* @param: currentUrl
* @param: trimSize
* @return: returnUrl
*
*/
function trimLongUrl(currentUrl, trimSize) {
var returnUrl = currentUrl;
if(currentUrl.length > trimSize) {
var str = String(currentUrl);
returnUrl = str.substring(0,trimSize) + "...";
return(returnUrl);
}
return returnUrl;
}