2018-03-02 04:48:12 +01:00
|
|
|
/*
|
2018-07-24 03:11:56 +02:00
|
|
|
This file is part of FreeTube.
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
FreeTube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
FreeTube is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FreeTube. If nsot, see <http://www.gnu.org/licenses/>.
|
2018-03-02 04:48:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2018-04-09 03:04:41 +02:00
|
|
|
* File for main layout manipulation and general variable configuration.
|
|
|
|
* There are some functions from other files that will probably need to be moved to here.
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
|
|
|
|
// Add general variables. Please put all require statements here.
|
|
|
|
window.$ = window.jQuery = require('jquery');
|
2018-03-13 00:37:30 +01:00
|
|
|
const mustache = require('mustache'); // templating
|
|
|
|
const dateFormat = require('dateformat'); // formatting dates
|
2018-03-02 04:48:12 +01:00
|
|
|
|
|
|
|
// Used for finding links within text and making them clickable. Used mostly for video descriptions.
|
|
|
|
const autolinker = require('autolinker');
|
2018-05-03 18:09:53 +02:00
|
|
|
const protocol = electron.remote.protocol;
|
2018-03-02 04:48:12 +01:00
|
|
|
|
|
|
|
// Used for getting the user's subscriptions. Can probably remove this when that function
|
|
|
|
// is rewritten.
|
2018-03-26 22:28:52 +02:00
|
|
|
const ytdl = require('ytdl-core');
|
2018-03-02 04:48:12 +01:00
|
|
|
const shell = electron.shell; // Used to open external links into the user's native browser.
|
|
|
|
const clipboard = electron.clipboard;
|
2018-03-05 04:08:53 +01:00
|
|
|
const getOpml = require('opml-to-json'); // Gets the file type for imported files.
|
2018-03-02 04:48:12 +01:00
|
|
|
const fs = require('fs'); // Used to read files. Specifically in the settings page.
|
2018-03-13 00:37:30 +01:00
|
|
|
|
2018-05-03 18:09:53 +02:00
|
|
|
let dialog = electron.remote.dialog; // Used for opening file browser to export / import subscriptions.
|
2018-03-02 04:48:12 +01:00
|
|
|
let toastTimeout; // Timeout for toast notifications.
|
2018-03-05 04:08:53 +01:00
|
|
|
let mouseTimeout; // Timeout for hiding the mouse cursor on video playback
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
require.extensions['.html'] = function (module, filename) {
|
|
|
|
module.exports = fs.readFileSync(filename, 'utf8');
|
2018-03-11 19:15:54 +01:00
|
|
|
};
|
|
|
|
|
2018-03-02 04:48:12 +01:00
|
|
|
// Grabs the default settings from the settings database file. Makes defaults if
|
|
|
|
// none are found.
|
|
|
|
|
2018-06-21 22:01:08 +02:00
|
|
|
electron.ipcRenderer.on('ping', function(event, message) {
|
2018-06-11 04:58:25 +02:00
|
|
|
console.log(message);
|
2019-03-26 16:45:20 +01:00
|
|
|
let url = message[message.length-1];
|
|
|
|
if (url) {
|
|
|
|
url = url.replace('freetube://', '');
|
|
|
|
parseSearchText(url);
|
|
|
|
}
|
2018-07-24 03:11:56 +02:00
|
|
|
ft.log(message);
|
2018-05-03 18:09:53 +02:00
|
|
|
});
|
|
|
|
|
2019-02-26 21:45:55 +01:00
|
|
|
// Listens for proxy to be set in main process
|
|
|
|
electron.ipcRenderer.on('proxyAvailable', function(event, message) {
|
|
|
|
proxyAvailable = true;
|
|
|
|
});
|
|
|
|
|
2018-03-02 04:48:12 +01:00
|
|
|
$(document).ready(() => {
|
2018-07-24 03:11:56 +02:00
|
|
|
const searchBar = document.getElementById('search');
|
|
|
|
const jumpToInput = document.getElementById('jumpToInput');
|
|
|
|
|
|
|
|
// Displays the list of subscriptions in the side bar.
|
|
|
|
displaySubs();
|
|
|
|
|
|
|
|
// Allow user to use the 'enter' key to search for a video.
|
|
|
|
searchBar.onkeypress = (e) => {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
parseSearchText();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-02 04:48:12 +01:00
|
|
|
// Display subscriptions upon the app opening up. May allow user to specify.
|
|
|
|
// Home page in the future.
|
2018-07-24 02:15:20 +02:00
|
|
|
loadingView.seen = true;
|
|
|
|
loadSubscriptions();
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Toggle the ability to view the side navigation bar.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
function toggleSideNavigation() {
|
2018-07-24 03:11:56 +02:00
|
|
|
const sideNav = document.getElementById('sideNav');
|
|
|
|
const mainContainer = document.getElementById('main');
|
|
|
|
|
|
|
|
if (sideNav.style.display === 'none') {
|
|
|
|
sideNav.style.display = 'inline';
|
|
|
|
mainContainer.style.marginLeft = '250px';
|
|
|
|
} else {
|
|
|
|
sideNav.style.display = 'none';
|
|
|
|
mainContainer.style.marginLeft = '0px';
|
|
|
|
}
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Display a toast message in the bottom right corner of the page.
|
|
|
|
* The toast automatically disappears after a timeout.
|
|
|
|
*
|
|
|
|
* @param {string} message - The toast message.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function showToast(message) {
|
2018-07-24 03:11:56 +02:00
|
|
|
let toast = document.getElementById('toast');
|
|
|
|
let toastMessage = document.getElementById('toastMessage');
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
// If a toast message is already being displayed, this will remove the previous timer that was set.
|
|
|
|
clearTimeout(toastTimeout);
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
toastMessage.innerHTML = message;
|
|
|
|
toast.style.visibility = 'visible';
|
|
|
|
toast.style.opacity = 0.9;
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
// Set the timer for the toast to be removed.
|
|
|
|
toastTimeout = window.setTimeout(hideToast, 5000);
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Hide the toast notification from the page.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function hideToast() {
|
2018-07-24 03:11:56 +02:00
|
|
|
let toast = document.getElementById('toast');
|
|
|
|
toast.style.opacity = 0;
|
|
|
|
toast.style.visibility = 'hidden';
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Displays a confirmation box before performing an action. The action will be performed
|
|
|
|
* if the user clicks 'yes'.
|
|
|
|
*
|
|
|
|
* @param {string} message - The message to be displayed in the confirmation box
|
|
|
|
* @param {function} performFunction - The function to be performed upon confirmation
|
|
|
|
* @param {*} parameters - The parameters that will be sent to performFunction
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-06-02 00:17:33 +02:00
|
|
|
function confirmFunction(message, performFunction, parameters = '') {
|
2018-07-24 03:11:56 +02:00
|
|
|
let confirmContainer = document.getElementById('confirmFunction');
|
|
|
|
let confirmMessage = document.getElementById('confirmMessage');
|
|
|
|
|
|
|
|
confirmMessage.innerHTML = message;
|
|
|
|
confirmContainer.style.visibility = 'visible';
|
|
|
|
|
|
|
|
$(document).on('click', '#confirmYes', (event) => {
|
|
|
|
if (parameters != '') {
|
|
|
|
performFunction(parameters);
|
|
|
|
} else {
|
|
|
|
performFunction();
|
|
|
|
}
|
|
|
|
hideConfirmFunction();
|
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Hides the confirmation box. Happens when the user clicks on 'no'.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function hideConfirmFunction() {
|
2018-07-24 03:11:56 +02:00
|
|
|
let confirmContainer = document.getElementById('confirmFunction');
|
|
|
|
confirmContainer.style.visibility = 'hidden';
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
2018-03-05 04:08:53 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Hide the mouse cursor after ~3 seconds. Used to hide the video when the user
|
|
|
|
* hovers the mouse over the video player.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function hideMouseTimeout() {
|
2018-07-24 03:11:56 +02:00
|
|
|
$('.videoPlayer')[0].style.cursor = 'default';
|
|
|
|
clearTimeout(mouseTimeout);
|
|
|
|
mouseTimeout = window.setTimeout(function () {
|
|
|
|
$('.videoPlayer')[0].style.cursor = 'none';
|
|
|
|
}, 3150);
|
2018-03-05 04:08:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-09 03:04:41 +02:00
|
|
|
* Remove the timeout for the mouse cursor as a fallback.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function removeMouseTimeout() {
|
2018-07-24 03:11:56 +02:00
|
|
|
$('.videoPlayer')[0].style.cursor = 'default';
|
|
|
|
clearTimeout(mouseTimeout);
|
2018-03-05 04:08:53 +01:00
|
|
|
}
|
2018-03-16 21:04:29 +01:00
|
|
|
|
2018-04-09 03:04:41 +02:00
|
|
|
function showVideoOptions(element) {
|
2018-07-24 03:11:56 +02:00
|
|
|
if (element.nextElementSibling.style.display == 'none' || element.nextElementSibling.style.display == '') {
|
|
|
|
element.nextElementSibling.style.display = 'inline-block'
|
|
|
|
} else {
|
|
|
|
element.nextElementSibling.style.display = 'none'
|
|
|
|
}
|
2018-09-19 20:34:23 +02:00
|
|
|
}
|
2019-02-26 21:45:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around AJAX calls to wait for proxy to become available
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function proxyRequest(callback) {
|
|
|
|
let proxyCheckingInterval;
|
|
|
|
let counter = 0;
|
2019-02-27 19:46:25 +01:00
|
|
|
|
2019-02-26 21:45:55 +01:00
|
|
|
// Wait for proxy to become available
|
|
|
|
proxyCheckingInterval = setInterval(function() {
|
|
|
|
if(proxyAvailable) {
|
|
|
|
clearInterval(proxyCheckingInterval)
|
2019-02-27 19:46:25 +01:00
|
|
|
|
2019-02-26 21:45:55 +01:00
|
|
|
callback();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if(counter > 10) {
|
|
|
|
clearInterval(proxyCheckingInterval);
|
|
|
|
showToast('Unable to connect to the Tor network. Check the help page if you\'re having trouble setting up your node.');
|
|
|
|
}
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}, 100);
|
2019-02-27 19:46:25 +01:00
|
|
|
}
|