Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ChathuraGH/906f5f52eeeccf93c798cb1a282f89fd to your computer and use it in GitHub Desktop.
Save ChathuraGH/906f5f52eeeccf93c798cb1a282f89fd to your computer and use it in GitHub Desktop.
(function(videojs) {
const defaults = {
bookmarks: []
};
const BookmarkPlugin = function(options) {
const player = this;
const settings = videojs.mergeOptions(defaults, options);
player.bookmarks = settings.bookmarks;
// Create a button in the control bar
const bookmarkButton = player.controlBar.addChild('Button', {
text: 'Bookmarks',
name: 'BookmarkButton'
});
bookmarkButton.addClass('vjs-bookmark-menu-control-button');
bookmarkButton.on('click', () => {
toggleBookmarkMenu();
});
// Hotkeys for bookmarking
// videojs.hotkeys({
player.hotkeys({
// seekStep: 50,
volume: false,
seek: false,
playPause: false,
// Add your hotkeys here
// 'b': () => {
// console.log('hi');
// const currentTime = player.currentTime();
// addBookmark(currentTime);
// }
customKeys: {
// Create custom hotkeys
p_Key: {
key: function(event) {
// Toggle something with CTRL + D Key
// return (event.ctrlKey && event.which === 68);
// return (event.ctrlKey);
return (event.which === 80);//p
},
handler: function(player, options, event) {
const menu = document.getElementById('bookmark-menu');
if (menu) {
menu.style.display = menu.style.display === 'none' ? 'block' : 'none';
} else {
createBookmarkMenu();
}
}
},
b_Key: {
key: function(event) {
// Toggle something with CTRL + D Key
// return (event.ctrlKey && event.which === 68);
// return (event.ctrlKey);
return (event.which === 66);//b
},
handler: function(player, options, event) {
// Using mute as an example
// if (options.enableMute) {
// player.muted(!player.muted());
const currentTime = player.currentTime();
addBookmark(currentTime);
console.log('hi',currentTime);
}
},
v_Key: {
key: function(event) {
// console.log('v_Key',event);
return (event.which === 86);//v
},
handler: function(player, options, event) {
const jump_currentTime = player.currentTime();
temp_bookmarks = player.bookmarks.slice();
temp_bookmarks.push(jump_currentTime);
temp_bookmarks.sort(function(a, b){return a-b});
for (var i = 0; i < temp_bookmarks.length; i++) {
if( jump_currentTime == temp_bookmarks[i] ){
if( (temp_bookmarks[i]-temp_bookmarks[i-1]) <= 4.00){
player.currentTime(temp_bookmarks[i-2]);
var index=temp_bookmarks.indexOf(jump_currentTime);
if( index !== -1 ){
temp_bookmarks.splice(index, 1);
}
console.log(temp_bookmarks[i]-temp_bookmarks[i-1],'if--v_Key',jump_currentTime);
}
else{
player.currentTime(temp_bookmarks[i-1]);
var index=temp_bookmarks.indexOf(jump_currentTime);
if( index !== -1 ){
temp_bookmarks.splice(index, 1);
}
console.log('else--v_Key',jump_currentTime);
}
}
}
// const before =
console.log('v--hi',jump_currentTime,temp_bookmarks,'\n##############################');
}
},
n_Key: {
key: function(event) {
// console.log('n_Key',event);
return (event.which === 78);//v
},
handler: function(player, options, event) {
const jump_currentTime = player.currentTime();
temp_bookmarks = player.bookmarks.slice();
temp_bookmarks.push(jump_currentTime);
temp_bookmarks.sort(function(a, b){return a-b});
for (var i = 0; i < temp_bookmarks.length; i++) {
if( jump_currentTime == temp_bookmarks[i] ){
if( temp_bookmarks[i] == temp_bookmarks[i+1] ){
player.currentTime(temp_bookmarks[i+2]);
var index=temp_bookmarks.indexOf(jump_currentTime);
if( index !== -1 ){
temp_bookmarks.splice(index, 1);
}
console.log(i,'--',i+1,'--if--n_Key',jump_currentTime,'--',temp_bookmarks[i+1]);
}
else{
player.currentTime(temp_bookmarks[i+1]);
var index=temp_bookmarks.indexOf(jump_currentTime);
if( index !== -1 ){
temp_bookmarks.splice(index, 1);
}
console.log(i,'--',i+1,'--if--n_Key',jump_currentTime,'--',temp_bookmarks[i+1]);
}
}
}
// const before =
console.log('n--hi',jump_currentTime,temp_bookmarks,'\n##############################');
}
},
}
});
// Function to add a bookmark
function addBookmark(time) {
if (!player.bookmarks.includes(time)) {
player.bookmarks.push(time);
player.bookmarks.sort(function(a, b){return a-b});
updateBookmarkMenu();
}
}
// Function to add a bookmark
window.videojs__bookmarkPlugin__Remove_This_Bookmark_from_player = (time) => {
if (player.bookmarks.includes(time)) {
// const jump_currentTime = player.currentTime();
// temp_bookmarks = player.bookmarks.slice();
var index=player.bookmarks.indexOf(time);
if( index !== -1 ){
// console.log('--splice--');
player.bookmarks.splice(index, 1);
}
player.bookmarks.sort(function(a, b){return a-b});
// console.log('-rrrr-',index,'--',player.bookmarks);
updateBookmarkMenu();
}
}
// Function to toggle the bookmark menu
function toggleBookmarkMenu() {
const menu = document.getElementById('bookmark-menu');
if (menu) {
menu.style.display = menu.style.display === 'none' ? 'block' : 'none';
} else {
createBookmarkMenu();
}
}
// Function to create the bookmark menu
function createBookmarkMenu() {
const menu = document.createElement('div');
menu.id = 'bookmark-menu';
menu.style.position = 'absolute';
menu.style.top = '50px';
menu.style.left = '10px';
menu.style.backgroundColor = 'white';
menu.style.border = '1px solid black';
menu.style.padding = '10px';
menu.style.display = 'block';
const title = document.createElement('h3');
// title.innerText = 'Bookmarks<sub>(click to play)</sub>';
title.innerHTML = 'Bookmarks<br><sub>(click to play)</sub>';
menu.appendChild(title);
const list = document.createElement('ul');
player.bookmarks.forEach((time) => {
const listItem = document.createElement('li');
// listItem.innerText = `Bookmark at ${time.toFixed(2)}s`;
listItem.innerHTML = `<button class="remove-button" onclick="window.videojs__bookmarkPlugin__Remove_This_Bookmark_from_player(${time});">x</button>Bookmark at ${time.toFixed(2)}s`;
listItem.onclick = () => {
player.currentTime(time);
};
list.appendChild(listItem);
});
menu.appendChild(list);
// document.body.appendChild(menu);
player.el().appendChild(menu);
$('#bookmark-menu').draggable({appendTo: 'body',containment: "parent",cursor: "crosshair"});
}
// Function to update the bookmark menu
function updateBookmarkMenu() {
const menu = document.getElementById('bookmark-menu');
if (menu) {
const list = menu.querySelector('ul');
list.innerHTML = ''; // Clear existing list
player.bookmarks.forEach((time) => {
const listItem = document.createElement('li');
// listItem.innerText = `Bookmark at ${time.toFixed(2)}s`;
listItem.innerHTML = `<button class="remove-button" onclick="window.videojs__bookmarkPlugin__Remove_This_Bookmark_from_player(${time});">x</button>Bookmark at ${time.toFixed(2)}s`;
// player.Remove_This_Bookmark_from_player(${time});
listItem.onclick = () => {
player.currentTime(time);
};
list.appendChild(listItem);
});
}
player.markers.removeAll();
player.markers.add(player.bookmarks.map(item => ({ time: item, text: `Bookmark at ${item.toFixed(2)}s` })));
// {markers: [player.bookmarks.map(item => ({ time: item }))]}
console.log('--------------------------',[player.bookmarks.map(item => ({ time: item }))]);
}
};
// Register the plugin with Video.js
videojs.plugin('bookmarkPlugin', BookmarkPlugin);
})(videojs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment