Skip to content

Instantly share code, notes, and snippets.

@llimllib
Created June 2, 2010 19:15
Show Gist options
  • Save llimllib/422837 to your computer and use it in GitHub Desktop.
Save llimllib/422837 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<div id="plugins"></div>
<!--<embed type="application/x-vlc-plugin" pluginspage="http://www.videolan.org" version="VideoLAN.VLCPlugin.2" id="vlc" hidden="true" autoplay="off" />-->
<!--<embed type="application/x-mplayer2" id="mediaPlayer" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" autostart="false" width="320" height="240" />-->
<script type="text/javascript" src="scripts/repository.js"></script>
<script type="text/javascript">
var PlayerVlc = function(id) {
var vlc = document.getElementById(id);
// Properties
this.name = 'VLC';
this.isInstalled = function() {
if (vlc == null) return false;
return 'input' in vlc;
}
this.setVolume = function(value) { vlc.audio.volume = parseInt(value); };
this.getVolume = function() { return vlc.audio.volume; };
this.getState = function() {
var state = vlc.input.state;
if (state == 6) return 5;
if (state == 7) return 6;
return state;
};
this.getTrackName = function() { return null; } // Not supported in VLC
this.setFile = function(mrl) { vlc.playlist.add(mrl); };
// Methods
this.play = function() { vlc.playlist.play(); };
this.playFile = function(mrl) {
if (mrl) {
var trackId = vlc.playlist.add(mrl);
vlc.playlist.playItem(trackId);
}
};
this.stop = function() {
if (vlc.playlist)
vlc.playlist.stop();
};
this.pause = function() { vlc.playlist.togglePause(); };
this.isMuted = function() { return vlc.audio.mute; };
this.toggleMute = function() { vlc.audio.toggleMute(); };
this.kill = function() {
var plugins = document.getElementById('plugins');
plugins.innerHTML = '';
};
};
var PlayerWmp = function(id) {
var wmp = document.getElementById(id);
var self = this;
// Properties
this.name = 'WMP'
this.isInstalled = function() {
if (wmp == null) return false;
return 'settings' in wmp;
}
this.setVolume = function(value) { wmp.settings.volume = parseInt(value); };
this.getVolume = function() { return wmp.settings.volume; };
this.getState = function() {
var state = wmp.playState;
if (wmp.error != null && wmp.error.errorCount > 0) return 6;
if (state == 10 || state == 0) return 0;
if (state == 9) return 1;
if (state == 6 || state == 7 || state == 11) return 2;
if (state == 2) return 4;
if (state == 1 || state == 8) return 5;
return state;
};
this.getTrackName = function() {
if (wmp.currentMedia)
return wmp.currentMedia.name;
return null;
}
this.setFile = function(mrl) { wmp.URL = mrl; };
// Methods
this.play = function() {
clearErrors();
if (wmp.controls != null)
wmp.controls.play();
};
this.playFile = function(mrl) {
if (mrl) {
clearErrors();
wmp.URL = mrl;
wmp.controls.play();
}
};
this.stop = function() {
clearErrors();
if (wmp.controls != null)
wmp.controls.stop();
};
this.pause = function() {
// Doesn't do anything on streams
if (wmp.controls != null)
wmp.controls.pause();
};
this.isMuted = function() {
return wmp.settings.mute;
};
this.toggleMute = function() {
clearErrors();
wmp.settings.mute = !wmp.settings.mute;
};
this.kill = function() {
var plugins = document.getElementById('plugins');
plugins.innerHTML = '';
};
var clearErrors = function() {
if (wmp.error != null && wmp.error.errorCount > 0)
wmp.error.clearErrorQueue();
};
};
var PlayerNone = { name: 'None', isInstalled: function() { return false; } };
var PluginActivator = function() {
var mimeTypes = window.navigator.mimeTypes;
// Plugins
this.hasVlc = function() { return mimeTypes.namedItem('application/x-vlc-plugin') != null; };
this.hasWmp = function() { return mimeTypes.namedItem('application/x-mplayer2') != null; };
this.hasWmp2 = function() { return mimeTypes.namedItem('application/x-ms-wmp') != null; };
this.createWmp = function(mimetype) {
// Inject embed html
var embed = document.createElement('embed');
embed.id = 'mediaPlayer';
if (mimetype)
embed.type = mimetype;
else
embed.type = 'application/x-mplayer2';
embed.setAttribute('autostart', 'false');
// Append to plugins container
var plugins = document.getElementById('plugins');
plugins.innerHTML = '';
plugins.appendChild(embed);
embed.uiMode = 'invisible';
return new PlayerWmp(embed.id);
};
this.createVlc = function() {
var embed = document.createElement('embed');
embed.id = 'vlc';
embed.type = 'application/x-vlc-plugin'
embed.setAttribute('version', 'VideoLAN.VLCPlugin.2')
embed.setAttribute('hidden', 'true');
// Append to plugins container
var plugins = document.getElementById('plugins');
plugins.innerHTML = '';
plugins.appendChild(embed);
return new PlayerVlc(embed.id);
};
this.createPlayer = function(plugin, force) {
if (this.hasWmp() && plugin != 'VLC') {
var player = this.createWmp();
if (player.isInstalled())
return player;
}
if (this.hasWmp2() && plugin != 'VLC') {
var player = this.createWmp('application/x-ms-wmp');
if (player.isInstalled())
return player;
}
if (this.hasVlc() && plugin != 'WMP') {
var player = this.createVlc();
if (player.isInstalled())
return player;
}
// If didn't find a plugin with a certain name, try again with auto select
if (plugin != null && force == true)
return this.createPlayer();
//console.warn('Could not activate a plugin!');
return PlayerNone;
};
};
</script>
<script type="text/javascript">
var currentState = -1;
var currentChannel = null;
var db = new Repository();
var player = PlayerNone;
var lastClick = 0;
var killTimeout;
var states = {
0: "Idle",
1: "Opening",
2: "Buffering",
3: "Playing",
4: "Paused",
5: "Stopped",
6: "Error",
"Idle": 0,
"Opening": 1,
"Buffering": 2,
"Playing": 3,
"Paused": 4,
"Stopped": 5,
"Error": 6
};
// Consts
var DEBUG_MODE = false;
var DOUBLE_CLICK_TIME = 250;
var PLAYER_KILL_TIME = 5000;
var MONITOR_TIME = 500;
window.onload = init;
function init() {
var autoPlay = localStorage.autoPlay ? JSON.parse(localStorage.autoPlay) : false;
if (autoPlay) {
activatePlayer();
radio.play();
}
}
var activatePlayer = function() {
player = new PluginActivator().createPlayer(localStorage.player, true);
if (!checkPlayerExists())
return;
initStateCheck();
if (localStorage.volume)
radio.volume = localStorage.volume;
if (localStorage.lastPlayed != null)
radio.addChannel(localStorage.lastPlayed);
}
var popupOpened = function() {
log('popupOpened');
if (localStorage.pluginInstalled == 'false')
return;
if (player.name == 'None' || !player.isInstalled())
activatePlayer();
if (lastClick != null) {
var diff = new Date().getTime() - lastClick;
checkDoubleClick(diff);
}
lastClick = new Date().getTime();
if (killTimeout != null) {
clearTimeout(killTimeout);
}
};
var popupClosed = function() {
log('popupClosed');
// Time (ms) since popup was opened
var diff = new Date().getTime() - lastClick;
checkDoubleClick(diff);
lastClick = new Date().getTime();
if (killTimeout != null) {
clearTimeout(killTimeout);
}
};
var checkDoubleClick = function(diff) {
if (radio.state == 'Idle' || radio.state == 'Stopped') {
if (diff < DOUBLE_CLICK_TIME)
radio.play();
else {
log('Going to kill...');
killTimeout = setTimeout(function() {
clearInterval(stateCheck);
player.kill();
log('Player killed');
}, PLAYER_KILL_TIME);
}
} else if (radio.state == 'Playing' || radio.state == 'Paused') {
if (diff < DOUBLE_CLICK_TIME)
radio.togglePause();
}
};
var radio = {
get volume() {
try {
if (player.isMuted())
return this.value;
} catch (ex) {}
return player.getVolume();
},
set volume(val){
player.setVolume(val);
localStorage.volume = val;
this.value = val;
return val;
},
get state() {
if (player.isMuted())
return states[4];
return states[currentState];
},
get channel() {
return currentChannel;
}
};
radio.stop = function() {
player.stop();
if (player.isMuted())
player.toggleMute();
log('Stop');
};
radio.play = function() {
if (player == null || !player.isInstalled())
activatePlayer();
if (currentChannel != null && currentChannel.id != null)
db.increasePlayCount(currentChannel);
player.play();
log('Play');
};
radio.playDirect = function(mrl) {
if (mrl == null) throw new TypeError('Missing mrl parameter');
if (mrl.length == 0) return;
if (player == null || !player.isInstalled())
activatePlayer();
player.playFile(mrl);
currentChannel = { name: mrl, mrl: mrl };
fireEvent('channelChange', { channel: currentChannel });
};
radio.playChannel = function(id) {
if (id == null) throw new TypeError('Missing id parameter');
if (player == null || !player.isInstalled())
activatePlayer();
var channel = db.getChannel(id);
db.increasePlayCount(channel);
if (channel == null)
return;
player.playFile(channel.mrl);
localStorage.lastPlayed = channel.id;
fireEvent('channelChange', { channel: channel });
currentChannel = channel;
};
radio.addChannel = function(id) {
var channel = db.getChannel(id);
if (channel == null)
return;
player.setFile(channel.mrl);
fireEvent('channelChange', { channel: channel });
currentChannel = channel;
};
radio.togglePause = function() {
if (currentState != states.Paused && currentState != states.Playing)
radio.play();
else {
player.toggleMute();
log('TogglePause (Mute)');
}
}
var stateCheck;
var currentTrack;
// State and track check
var initStateCheck = function() {
stateCheck = setInterval(function() {
try {
var newState = player.getState();
var newTrack = player.getTrackName();
if (newTrack != currentTrack) {
fireEvent('channelChange', { channel: currentChannel, track: newTrack });
currentTrack = newTrack;
}
if (player.isMuted()) newState = states.Paused;
if (newState != currentState) {
fireEvent('stateChange', { state: states[newState] });
currentState = newState;
if (newState == 0 || newState == 5 || newState == 4) { // Stopped, Idle, Paused
chrome.browserAction.setIcon({ path: "images/audio-volume-low.png" });
} else if (newState == 1 || newState == 2) { // Opening, Buffering
chrome.browserAction.setIcon({ path: "images/audio-volume-medium.png" });
} else if (newState == 6) { // Error
chrome.browserAction.setIcon({ path: "images/audio-volume-muted.png" });
} else { // Playing
chrome.browserAction.setIcon({ path: "images/audio-volume-high.png" });
}
}
} catch (ex) {
log('Error in state timer.');
clearInterval(stateCheck);
}
}, MONITOR_TIME);
};
// Check if plugin is installed and activated
var checkPlayerExists = function() {
var isInstalled = player.isInstalled();
localStorage.pluginInstalled = isInstalled;
return isInstalled;
};
var recheckPlayerExists = function() {
player = new PluginActivator().createPlayer();
if (checkPlayerExists()) {
activatePlayer();
return true;
}
return false;
};
var fireEvent = function(_event, _data) {
chrome.extension.sendRequest({ event: _event, data: _data });
};
var log = function(msg) {
if (DEBUG_MODE) {
//console.log(msg);
fireEvent('log', { message: msg });
}
};
// var updated = new Date().getTime();
//
// var sync = new -({
// getUpdate: function() {
// return updated;
// },
//
// 'onRead': function(json, bookmark) {
// updated = this.syncedAt;
//
// console.log('READING');
// console.log(json.data);
// },
//
// 'onWrite': function() {
// console.log('WRITING');
//
// this.write({
// 'data' : db.getChannels()
// });
// }
// }).start();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment