Skip to content

Instantly share code, notes, and snippets.

@Elyorbe
Last active December 23, 2022 12:05
Show Gist options
  • Save Elyorbe/28a68358922730f2033cb15ac3e1eb19 to your computer and use it in GitHub Desktop.
Save Elyorbe/28a68358922730f2033cb15ac3e1eb19 to your computer and use it in GitHub Desktop.
const iframe = document.getElementsByTagName('iframe')[0]
if (iframe) {
const player = new Vimeo.Player(iframe);
const url = iframe.getAttribute("src").split('?')[0];
const vimeoId = extractVimeoId(url);
updateCurrentPlayerTime(vimeoId, player);
trackWatchProgress(vimeoId, player);
}
function updateCurrentPlayerTime(vimeoId, player) {
jQuery.ajax({
url: ajaxurl,
data: {
'action': 'last_watch_progress_request',
'video_id': vimeoId
},
success: function (lastWatchProgress) {
if (lastWatchProgress) {
setPlayerTime(lastWatchProgress, player)
}
},
error: function (errorThrown) {
console.log("Error while fetching last watch progress from server");
}
});
}
function setPlayerTime(lastWatchProgress, player) {
// THIS IS NOT BEING CALLED
player.setCurrentTime(lastWatchProgress).then(function (seconds) {
console.log("This is not being called in Wordpress");
}).catch(function (error) {
switch (error.name) {
case 'RangeError':
console.log("Seconds must be greater than 0")
break;
default:
console.log("Error: ", error)
break;
}
});
}
function trackWatchProgress(vimeoId, player) {
setInterval(function () {
// THIS IS NOT BEING CALLED
player.getCurrentTime().then(function (seconds) {
console.log("This is not being called in Wordpress");
sendProgressToServer(vimeoId, seconds);
});
}, 5000);
}
function extractVimeoId(url) {
const vimeoRegex = /https?:\/\/(?:www\.)?(vimeo.com|player.vimeo.com)\/(?:(channels|video)\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/;
const match = url.match(vimeoRegex);
if (match) return match[5];
}
function sendProgressToServer(videoId, progress) {
jQuery.ajax({
url: ajaxurl,
data: {
'action': 'last_watch_progress_update',
'video_id': videoId,
'progress': progress
},
success: function (response) {
},
error: function (errorThrown) {
console.log("Error while sending watch progress: ", errorThrown);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment