-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotification.js
286 lines (246 loc) · 9.23 KB
/
notification.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//constants
vagModule = null;
var sText = null;
window.URL = window.URL || window.webkitURL;
//messaging to the background page
var debugPort = chrome.extension.connect({name:"debug"});
var moduleDidLoad = function() {
vagModule = document.getElementById("NACL_Module");
updateStatus("Transcoder successfully loaded!");
};
var pageDidLoad = function() {
if(vagModule === null) {
updateStatus('Initializing transcoder...');
} else {
updateStatus();
}
};
var updateStatus = function(opt_message) {
if(opt_message) {
sText = opt_message;
}
var statusField = document.getElementById("saving");
if(statusField &&
(statusField.innerHTML === "Initializing transcoder...")
|| statusField.innerHTML === "") {
statusField.innerHTML = sText;
};
};
var loadURL = function(messageStr) {
debugPort.postMessage(messageStr);
vagModule.postMessage(messageStr);
};
var downloadFileName = "download";
var globleText = null;
var createDownloadBlob = function (data) {
var blob = new Blob([data], {type:"audo/video"});
var blobURL = window.URL.createObjectURL(blob);
//fade away the text content
$(".downloadthis").fadeOut('slow', function() {
// Animation complete.
});
//download button appears
var selectContainer = document.querySelector(".selectcontainer");
var downloadButton = document.createElement("a");
downloadButton.addEventListener('click', function(e) {
$(downloadButton).fadeOut('slow', function() {});
var hiddenButtons = document.querySelectorAll(".btn");
for(var buttonIndex = 0; buttonIndex < hiddenButtons.length; buttonIndex++) {
var hiddenButton = hiddenButtons[buttonIndex];
if (hiddenButton.parentNode.id !== "downloadBtn") {
console.log(hiddenButton.id);
$(hiddenButton).fadeIn('slow', function(e) {});
}
}
globleStatusText = globleText;
});
downloadButton.setAttribute("id", "downloadBtn");
downloadButton.setAttribute("href", blobURL);
downloadButton.setAttribute("download", downloadFileName);
var insideDiv = document.createElement("div");
insideDiv.className = "btn "+"downloadBtn";
insideDiv.textContent = "download";
downloadButton.appendChild(insideDiv);
selectContainer.appendChild(downloadButton);
};
var progressBarDisplay = function(fName, progressStat) {
var gLength = 220;
var px = 220 - 220 * 0.01 * progressStat;
var barURL = '';
debugPort.postMessage("progressStat:"+px);
if (/\.mp3|\.mpeg3/.test(fName)) {
//debugPort.postMessage("mp3");
barURL = 'UI/img/red.png';
} else if (/\.mp4|\.mpeg4/.test(fName)) {
//debugPort.postMessage("mp4");
barURL = 'UI/img/blue.png';
} else {
//debugPort.postMessage("original download");
barURL = 'UI/img/green.png';
}
var container = document.querySelector(".foundcontainer");
container.style.backgroundImage = "url("+barURL+")";
container.style.backgroundPosition = "-"+px+"px 50%";
};
var handleMessage = function(message_event) {
var NACLMessage = message_event.data;
if (NACLMessage.hasOwnProperty("byteLength")) {
//debugPort.postMessage(typeof NACLMessage.byteLength);
//buf view for accessing the array buffer
var bufView = new Uint8Array(NACLMessage.byteLength);
bufView.set(NACLMessage);
//debug port
debugPort.postMessage(bufView);
createDownloadBlob("This is a test!");
} else {
var progressCaptured = /^progress---->.+---->(.+)$/.exec(NACLMessage);
if (progressCaptured) {
//transcoding progress
var progressStat = parseFloat(progressCaptured[1]);
//debugPort.postMessage(progressStat);
progressBarDisplay(downloadFileName, progressStat);
}
}
};
var transcodeOptions = {
"flv":["mp3","mp4", "original"],
"mp4":["mp3","original"],
"mpeg4":["mp3","original"]
};
var conversionOptions = {
"mp3":{tooltip:"Convert to MP3 audio format"},
"mp4":{tooltip:"Convert to MP4 video format"},
"original":{tooltip:"Download as current format"}
};
var globleStatusText = "";
var decideAction = function(DOMElement, fileName, mediaURL, convertType, vidID) {
DOMElement.title = conversionOptions[convertType].tooltip;
DOMElement.addEventListener("mouseover", function() {
var downloadType = "current";
var fmatCaptured = /[^\.]+\.([^\.]+)$/.exec(fileName);
if (fmatCaptured) {
downloadType = fmatCaptured[1];
}
if (convertType !== "original") {
document.getElementById("saving").textContent = conversionOptions[convertType].tooltip;
} else {
document.getElementById("saving").textContent = "Download as " + downloadType + " format";
}
}, false);
DOMElement.addEventListener("mouseout", function() {
document.getElementById("saving").textContent = globleStatusText;
}, false);
var buttons = document.querySelectorAll('.btn');
//direct download if no conversion is needed
if (convertType === "original") {
DOMElement.href = mediaURL;
DOMElement.download = fileName;
//change status text
DOMElement.addEventListener("click",function() {
for (var buttonIndex = 0 ; buttonIndex < buttons.length ; buttonIndex++) {
$(buttons[buttonIndex]).fadeOut('slow', function() {
// Animation complete.
});
}
$('.downloadandfilenametomove').animate({left:'-1'}, 1000);
var statusTag = document.getElementById("saving");
statusTag.textContent = "Download and Save!";
globleStatusText = statusTag.textContent;
document.querySelector('p.downloadthis b').textContent = "Downloading......";
}, false);
}
else {
//send the url to NACL for transcoding
DOMElement.addEventListener("click", function() {
for (var buttonIndex = 0 ; buttonIndex < buttons.length ; buttonIndex++) {
$(buttons[buttonIndex]).fadeOut('slow', function() {
// Animation complete.
});
}
$('.downloadandfilenametomove').animate({left:'-1'}, 1000);
var statusTag = document.getElementById("saving");
statusTag.textContent = "Converting '"+fileName+"'...";
globleStatusText = statusTag.textContent;
document.querySelector('p.downloadthis b').textContent = "Conversion In Progress......";
var currentFormat =
(vidID.indexOf(".mp4") !== -1 | vidID.indexOf(".mpeg4") !== -1)
? "mp4" : "flv";
var mstr = vidID + "((--))"
+ convertType + "((--))" + mediaURL + "<<-->>" + currentFormat;
var fileNameComponents = /([^\.]+\.)[^\.]+$/.exec(fileName);
if (fileNameComponents) {
downloadFileName = fileNameComponents[1]+convertType;
}
loadURL(mstr);
}, false);
}
};
$(document).ready(function() {
//media meta from background page
var bgView = chrome.extension.getBackgroundPage();
var mediaMeta = bgView.metaToNotification;
//generate buttons
var paIter;
var detected = false;
var selectContainer = document.querySelector(".selectcontainer");
//chrome download API:In Dev Progress
for (paIter in transcodeOptions) {
if ((mediaMeta.vidID).indexOf(paIter) !== -1) {
detected = true;
var formats = transcodeOptions[paIter];
var selections = "";
if (formats.length === 2) {
var selection1 =
"<a id=\""+formats[0]+"\"><div style=\"left:45px;border-radius: 6px 0px 0px 6px;\" class=\"btn "
+formats[0]+"\">"+formats[0]+"</div></a>";
var selection2 =
"<a id=\""+formats[1]+"\"><div style=\"left:85px;border-radius: 0px 6px 6px 0px;\" class=\"btn "
+formats[1]+"\">"+formats[1]+"</div></a>";
selections = selection1+selection2;
} else if (formats.length === 3) {
for (var fIndex = 0 ; fIndex < formats.length ; fIndex++) {
selections += "<a id=\""+formats[fIndex]+"\"><div class=\"btn "
+formats[fIndex]+"\">"+formats[fIndex]+"</div></a>";
}
}
$(selectContainer).append(selections);
}
}
if (!detected) {
//Download directly
var directDownload = "<a id=\"original\"><div class=\"btn original\" "
+"style=\"left:61px;border-radius: 6px 6px 6px 6px\""
+">Download</div></a>";
$(selectContainer).append(directDownload);
}
//text for file saving notification
var savingStatusTag = document.getElementById("saving");
var text = null;
var trimmedName = null;
if (mediaMeta) {
var downloadName = null;
var downloadNameCaptured = /.*\/([^\/]+)$/.exec(mediaMeta.vidID);
if (downloadNameCaptured) {
downloadName = downloadNameCaptured[1];
} else {
downloadName = mediaMeta.vidID;
}
trimmedName =
downloadName.length < 10
? downloadName
: ("..." + downloadName.substring(downloadName.length - 20));
text = "Save \'"+trimmedName+"\' as";
globleText = text;
globleStatusText = text;
} else {
text = "Oops! Error on loading the file!";
}
savingStatusTag.textContent = text;
//append tooltips to conversion formats
var mp3Tag = document.getElementById("mp3");
var mp4Tag = document.getElementById("mp4");
var originalTag = document.getElementById("original");
if (mp3Tag) decideAction(mp3Tag, trimmedName, mediaMeta.mediaURL, "mp3", mediaMeta.vidID);
if (mp4Tag) decideAction(mp4Tag, trimmedName, mediaMeta.mediaURL, "mp4", mediaMeta.vidID);
if (originalTag) decideAction(originalTag, trimmedName, mediaMeta.mediaURL, "original", mediaMeta.vidID);
});