Skip to content

Commit

Permalink
show video poster if is embed && use js-beautify to beautify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lejenome committed Nov 26, 2014
1 parent a99ce6d commit 9dc2314
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 89 deletions.
14 changes: 7 additions & 7 deletions data/youtube-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ var OPTIONS = {};
//Youtube video formats sorted as preferred
var PREF_FORMATS = [];
// will be defined on youtube.js later
var prefChangeHandler = function () {
};
(function () {
var prefChangeHandler = function() {};
(function() {
"use strict";
self.port.on("preferences", function (prefs) {
self.port.on("preferences", function(prefs) {
OPTIONS = prefs;
generatePrefFormats();
});

function generatePrefFormats() {
var FORMT = [
[
"43",
// 320p on webm
"22" //720p is not availbale on webm
"22" //720p is not availbale on webm
],
[
"18",
// 320p on mp4
"22" // 720p on mp3
"22" // 720p on mp3
]
];
var x = OPTIONS.preferredCodec === 0 ? {
Expand All @@ -46,7 +46,7 @@ var prefChangeHandler = function () {
PREF_FORMATS.push(FORMT[x.i][y.i + y.n]);
PREF_FORMATS.push(FORMT[x.i + x.n][y.i + y.n]);
}
self.port.on("prefChanged", function (pref) {
self.port.on("prefChanged", function(pref) {
OPTIONS[pref.name] = pref.value;
if (pref.name === "preferredCodec" || pref.name === "preferredQuality")
generatePrefFormats();
Expand Down
170 changes: 89 additions & 81 deletions data/youtube.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,102 @@
/*globals videojs, PREF_FORMATS, FORMATS, OPTIONS, prefChangeHandler */
(function () {
/*globals videojs, PREF_FORMATS, FORMATS, OPTIONS*/
(function() {
"use strict";
var player, player_container;
var INFO_URL = "https://www.youtube.com/get_video_info?hl=en_US&el=detailpage&video_id=";

function main() {
changePlayer();
window.addEventListener("spfrequest", function () {
window.addEventListener("spfrequest", function() {
if (player)
player.src = "";
});
window.addEventListener("spfdone", function () {
window.addEventListener("spfdone", function() {
changePlayer();
});
prefChangeHandler = function (pref) {
window.prefChangeHandler = function(pref) {
if (player && pref.name === "volume") {
player[pref.name] = pref.value / 100;
}
};
}

function changePlayer() {
var conf = getConfig();
if (conf === undefined)
return;
asyncText(INFO_URL + conf.id).then(getURL(conf)).then(function (url) {
try {
if (player_container)
getConfig()
.then(getVideoInfo)
.then(processInfo)
.then(function(conf) {
try {
if (player_container)
player_container.innerHTML = "";
player_container = document.getElementById("player-mole-container");
if (conf.isEmbed)
player_container = document.body;
if (conf.isChannel)
player_container = document.getElementsByClassName("c4-player-container")[0];
if (!player_container)
return;
player_container.innerHTML = "";
player_container = document.getElementById("player-mole-container");
if (conf.isEmbed)
player_container = document.body;
if (conf.isChannel)
player_container = document.getElementsByClassName("c4-player-container")[0];
if (!player_container)
return;
player_container.innerHTML = "";
var player_opt = {
id: "video_player",
src: url,
className: "video-js vjs-default-skin " + conf.className,
controls: true,
volume: OPTIONS.volume / 100
};
if (!conf.isEmbed)
player_opt.autoplay = true;
player = createNode("video", player_opt);
//videojs(player); //TODO: use video-js custom video player
player_container.appendChild(player);
} catch (e) {
console.error("Exception on changePlayer()", e.lineNumber, e.columnNumber, e.message, e.stack);
}
});
var player_opt = {
id: "video_player",
src: conf.url,
className: "video-js vjs-default-skin " + conf.className,
controls: true,
volume: OPTIONS.volume / 100
};
if (conf.isEmbed)
player_opt.poster = conf.poster ? conf.poster : "";
else
player_opt.autoplay = true;
player = createNode("video", player_opt);
//videojs(player); //TODO: use video-js custom video player
player_container.appendChild(player);
} catch (e) {
console.error("Exception on changePlayer()", e.lineNumber, e.columnNumber, e.message, e.stack);
}
});
}

function getConfig() {
var isEmbed = location.href.search("youtube.com/embed/") > -1;
var isWatch = location.href.search("youtube.com/watch?") > -1;
var isChannel = location.href.search("youtube.com/channel/") > -1 || location.href.search("youtube.com/user/") > -1;
if (!isEmbed && !isWatch && !isChannel)
return;
var player_id, player_class;
if (isEmbed) {
player_id = location.pathname.match(/^\/embed\/([^?#/]*)/)[1];
player_class = "full-frame";
} else if (isChannel) {
var upsell = document.getElementById("upsell-video");
if (!upsell)
return;
player_id = upsell.dataset["videoId"];
player_class = "html5-main-video";
} else {
player_id = location.search.slice(1).match(/v=([^/?#]*)/)[1];
player_class = "player-width player-height";
}
if (!player_id)
return;
return {
isEmbed: isEmbed,
isWatch: isWatch,
isChannel: isChannel,
id: player_id,
className: player_class
};
return new Promise(function(resolve, reject) {
var isEmbed = location.href.search("youtube.com/embed/") > -1;
var isWatch = location.href.search("youtube.com/watch?") > -1;
var isChannel = location.href.search("youtube.com/channel/") > -1 || location.href.search("youtube.com/user/") > -1;
if (!isEmbed && !isWatch && !isChannel)
reject();
var player_id, player_class;
if (isEmbed) {
player_id = location.pathname.match(/^\/embed\/([^?#/]*)/)[1];
player_class = "full-frame";
} else if (isChannel) {
var upsell = document.getElementById("upsell-video");
if (!upsell)
reject();
player_id = upsell.dataset["videoId"];
player_class = "html5-main-video";
} else {
player_id = location.search.slice(1).match(/v=([^/?#]*)/)[1];
player_class = "player-width player-height";
}
if (!player_id)
reject();
resolve({
isEmbed: isEmbed,
isWatch: isWatch,
isChannel: isChannel,
id: player_id,
className: player_class
});
});
}
function getURL(conf) {
return function (data) {

function processInfo([conf, data]) {
return new Promise(function(resolve, reject) {
var poster = data.match(/iurlhq=([^&]*)/);
if (poster)
conf.poster = decodeURIComponent(poster[1]);
var info = data.match(/url_encoded_fmt_stream_map=([^&]*)/)[1];
info = decodeURIComponent(info);
var formats = {};
info.split(",").forEach(function (f, i) {
info.split(",").forEach(function(f, i) {
var itag = f.match(/itag=([^&]*)/)[1];
var url = decodeURIComponent(f.match(/url=([^&]*)/)[1]);
if (FORMATS[itag])
Expand All @@ -96,10 +106,12 @@
return Promise.reject();
for (var i = 0; i < PREF_FORMATS.length; i++)
if (formats[PREF_FORMATS[i]]) {
return Promise.resolve(formats[PREF_FORMATS[i]]);
conf.url = formats[PREF_FORMATS[i]];
resolve(conf);
}
};
});
}

function createNode(type, obj, data, style) {
var node = document.createElement(type);
if (obj)
Expand All @@ -116,28 +128,24 @@
node.style[st] = style[st];
return node;
}
function asyncText(url, headers, success) {
return new Promise(function (resolve, reject) {

function getVideoInfo(conf) {
return new Promise(function(resolve, reject) {
var INFO_URL = "https://www.youtube.com/get_video_info?hl=en_US&el=detailpage&video_id=";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.open("GET", INFO_URL + conf.id, true);
if (xhr.overrideMimeType)
xhr.overrideMimeType("text/plain");
xhr.onload = function () {
xhr.onload = function() {
if (this.status !== 200) {
reject(this.status);
return;
}
resolve(this.responseText);
resolve([conf, this.responseText]);
};
xhr.onerror = function () {
xhr.onerror = function() {
reject();
};
if (headers) {
for (var header in headers) {
if (headers.hasOwnProperty(header))
xhr.setRequestHeader(header, headers[header]);
}
}
xhr.send();
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Moez Bouhlel <[email protected]> (http://lejenome.github.io/)",
"homepage": "https://github.com/lejenome/youtube-html5-player",
"license": "MPL 2.0",
"version": "0.1.2",
"version": "0.1.3",
"preferences": [
{
"name": "preferredCodec",
Expand Down
Binary file modified youtube-video-player.xpi
Binary file not shown.

0 comments on commit 9dc2314

Please sign in to comment.