invidious-experimenting/assets/js/handlers.js

168 lines
6.7 KiB
JavaScript
Raw Normal View History

'use strict';
2020-03-16 04:22:49 +05:30
(function () {
var n2a = function (n) { return Array.prototype.slice.call(n); };
2020-03-27 20:17:46 +05:30
var video_player = document.getElementById('player_html5_api');
if (video_player) {
2020-03-16 04:22:49 +05:30
video_player.onmouseenter = function () { video_player['data-title'] = video_player['title']; video_player['title'] = ''; };
video_player.onmouseleave = function () { video_player['title'] = video_player['data-title']; video_player['data-title'] = ''; };
video_player.oncontextmenu = function () { video_player['title'] = video_player['data-title']; };
}
// For dynamically inserted elements
2020-03-16 04:22:49 +05:30
document.addEventListener('click', function (e) {
if (!e || !e.target) { return; }
e = e.target;
var handler_name = e.getAttribute('data-onclick');
switch (handler_name) {
2020-03-16 04:22:49 +05:30
case 'jump_to_time':
var time = e.getAttribute('data-jump-time');
player.currentTime(time);
break;
case 'get_youtube_replies':
var load_more = e.getAttribute('data-load-more') !== null;
2021-03-18 10:53:32 +05:30
var load_replies = e.getAttribute('data-load-replies') !== null;
get_youtube_replies(e, load_more, load_replies);
2020-03-16 04:22:49 +05:30
break;
case 'toggle_parent':
toggle_parent(e);
break;
default:
break;
}
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-mouse="switch_classes"]')).forEach(function (e) {
var classes = e.getAttribute('data-switch-classes').split(',');
var ec = classes[0];
var lc = classes[1];
2020-03-16 04:22:49 +05:30
var onoff = function (on, off) {
var cs = e.getAttribute('class');
cs = cs.split(off).join(on);
e.setAttribute('class', cs);
};
2020-03-16 04:22:49 +05:30
e.onmouseenter = function () { onoff(ec, lc); };
e.onmouseleave = function () { onoff(lc, ec); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onsubmit="return_false"]')).forEach(function (e) {
e.onsubmit = function () { return false; };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="mark_watched"]')).forEach(function (e) {
e.onclick = function () { mark_watched(e); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="mark_unwatched"]')).forEach(function (e) {
e.onclick = function () { mark_unwatched(e); };
});
n2a(document.querySelectorAll('[data-onclick="add_playlist_video"]')).forEach(function (e) {
e.onclick = function () { add_playlist_video(e); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="add_playlist_item"]')).forEach(function (e) {
e.onclick = function () { add_playlist_item(e); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="remove_playlist_item"]')).forEach(function (e) {
e.onclick = function () { remove_playlist_item(e); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="revoke_token"]')).forEach(function (e) {
e.onclick = function () { revoke_token(e); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="remove_subscription"]')).forEach(function (e) {
e.onclick = function () { remove_subscription(e); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onclick="notification_requestPermission"]')).forEach(function (e) {
e.onclick = function () { Notification.requestPermission(); };
});
2020-03-16 04:22:49 +05:30
n2a(document.querySelectorAll('[data-onrange="update_volume_value"]')).forEach(function (e) {
2022-04-20 14:35:19 +05:30
var cb = function () { update_volume_value(e); };
e.oninput = cb;
e.onchange = cb;
});
function update_volume_value(element) {
document.getElementById('volume-value').innerText = element.value;
}
function revoke_token(target) {
var row = target.parentNode.parentNode.parentNode.parentNode.parentNode;
row.style.display = 'none';
var count = document.getElementById('count');
count.innerText = count.innerText - 1;
var referer = window.encodeURIComponent(document.location.href);
var url = '/token_ajax?action_revoke_token=1&redirect=false' +
'&referer=' + referer +
'&session=' + target.getAttribute('data-session');
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.timeout = 10000;
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2020-03-16 04:22:49 +05:30
xhr.onreadystatechange = function () {
2022-04-20 14:43:16 +05:30
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
count.innerText = parseInt(count.innerText) + 1;
row.style.display = '';
}
}
2022-04-20 14:35:19 +05:30
};
var csrf_token = target.parentNode.querySelector('input[name="csrf_token"]').value;
xhr.send('csrf_token=' + csrf_token);
}
function remove_subscription(target) {
var row = target.parentNode.parentNode.parentNode.parentNode.parentNode;
row.style.display = 'none';
var count = document.getElementById('count');
count.innerText = count.innerText - 1;
var referer = window.encodeURIComponent(document.location.href);
var url = '/subscription_ajax?action_remove_subscriptions=1&redirect=false' +
'&referer=' + referer +
'&c=' + target.getAttribute('data-ucid');
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.timeout = 10000;
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2020-03-16 04:22:49 +05:30
xhr.onreadystatechange = function () {
2022-04-20 14:43:16 +05:30
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
count.innerText = parseInt(count.innerText) + 1;
row.style.display = '';
}
}
2022-04-20 14:35:19 +05:30
};
var csrf_token = target.parentNode.querySelector('input[name="csrf_token"]').value;
xhr.send('csrf_token=' + csrf_token);
}
2022-01-14 02:50:10 +05:30
// Handle keypresses
2022-04-20 15:06:52 +05:30
window.addEventListener('keydown', function (event) {
2022-01-14 02:50:10 +05:30
// Ignore modifier keys
if (event.ctrlKey || event.metaKey) return;
// Ignore shortcuts if any text input is focused
let focused_tag = document.activeElement.tagName.toLowerCase();
const allowed = /^(button|checkbox|file|radio|submit)$/;
2022-04-20 15:19:05 +05:30
if (focused_tag === 'textarea') return;
if (focused_tag === 'input') {
let focused_type = document.activeElement.type.toLowerCase();
if (!focused_type.match(allowed)) return;
}
2022-01-14 02:50:10 +05:30
// Focus search bar on '/'
2022-04-20 15:19:05 +05:30
if (event.key === '/') {
2022-01-14 02:50:10 +05:30
document.getElementById('searchbox').focus();
event.preventDefault();
}
});
})();