From 0099a9822e846e5eed1eaf12e48bdbe427f9aa59 Mon Sep 17 00:00:00 2001
From: Omar Roth
Date: Sun, 5 May 2019 08:35:54 -0500
Subject: [PATCH] Refactor subscribe_widget
---
.../js/subscribe_widget.js | 35 ++++++++++---------
src/invidious/views/channel.ecr | 5 ---
.../views/components/subscribe_widget.ecr | 24 +++++++++----
src/invidious/views/playlists.ecr | 5 ---
src/invidious/views/watch.ecr | 5 ---
5 files changed, 36 insertions(+), 38 deletions(-)
rename src/invidious/views/components/subscribe_widget_script.ecr => assets/js/subscribe_widget.js (62%)
diff --git a/src/invidious/views/components/subscribe_widget_script.ecr b/assets/js/subscribe_widget.js
similarity index 62%
rename from src/invidious/views/components/subscribe_widget_script.ecr
rename to assets/js/subscribe_widget.js
index 107bd3a2..64e3e55f 100644
--- a/src/invidious/views/components/subscribe_widget_script.ecr
+++ b/assets/js/subscribe_widget.js
@@ -1,31 +1,33 @@
-subscribe_button = document.getElementById('subscribe');
+var subscribe_button = document.getElementById('subscribe');
+subscribe_button.parentNode['action'] = 'javascript:void(0)';
-if (subscribe_button.getAttribute('onclick')) {
- subscribe_button['href'] = 'javascript:void(0)';
+if (subscribe_button.getAttribute('data-type') === 'subscribe') {
+ subscribe_button.onclick = subscribe;
+} else {
+ subscribe_button.onclick = unsubscribe;
}
function subscribe(timeouts = 0) {
- subscribe_button = document.getElementById('subscribe');
-
if (timeouts > 10) {
console.log('Failed to subscribe.');
return;
}
var url = '/subscription_ajax?action_create_subscription_to_channel=1&redirect=false' +
- '&c=<%= ucid %>&referer=<%= env.get("current_page") %>';
+ '&c=' + subscribe_data.ucid +
+ '&referer=' + location.pathname + location.search;
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.timeout = 20000;
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.send('csrf_token=<%= URI.escape(env.get?("csrf_token").try &.as(String) || "") %>');
+ xhr.send('csrf_token=' + subscribe_data.csrf_token);
var fallback = subscribe_button.innerHTML;
subscribe_button.onclick = unsubscribe;
- subscribe_button.innerHTML = '<%= translate(locale, "Unsubscribe").gsub("'", "\\'") %> | <%= sub_count_text %>';
+ subscribe_button.innerHTML = '' + subscribe_data.unsubscribe_text + ' | ' + subscribe_data.sub_count_text + '';
- xhr.onreadystatechange = function() {
+ xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
subscribe_button.onclick = subscribe;
@@ -34,34 +36,33 @@ function subscribe(timeouts = 0) {
}
}
- xhr.ontimeout = function() {
+ xhr.ontimeout = function () {
console.log('Subscribing timed out.');
subscribe(timeouts + 1);
};
}
function unsubscribe(timeouts = 0) {
- subscribe_button = document.getElementById('subscribe');
-
if (timeouts > 10) {
console.log('Failed to subscribe');
return;
}
var url = '/subscription_ajax?action_remove_subscriptions=1&redirect=false' +
- '&c=<%= ucid %>&referer=<%= env.get("current_page") %>';
+ '&c=' + subscribe_data.ucid +
+ '&referer=' + location.pathname + location.search;
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.timeout = 20000;
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- xhr.send('csrf_token=<%= URI.escape(env.get?("csrf_token").try &.as(String) || "") %>');
+ xhr.send('csrf_token=' + subscribe_data.csrf_token);
var fallback = subscribe_button.innerHTML;
subscribe_button.onclick = subscribe;
- subscribe_button.innerHTML = '<%= translate(locale, "Subscribe").gsub("'", "\\'") %> | <%= sub_count_text %>';
+ subscribe_button.innerHTML = '' + subscribe_data.subscribe_text + ' | ' + subscribe_data.sub_count_text + '';
- xhr.onreadystatechange = function() {
+ xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
subscribe_button.onclick = unsubscribe;
@@ -70,7 +71,7 @@ function unsubscribe(timeouts = 0) {
}
}
- xhr.ontimeout = function() {
+ xhr.ontimeout = function () {
console.log('Unsubscribing timed out.');
unsubscribe(timeouts + 1);
};
diff --git a/src/invidious/views/channel.ecr b/src/invidious/views/channel.ecr
index 6737c0d2..9d55f3c3 100644
--- a/src/invidious/views/channel.ecr
+++ b/src/invidious/views/channel.ecr
@@ -82,8 +82,3 @@
<% end %>
-
-
diff --git a/src/invidious/views/components/subscribe_widget.ecr b/src/invidious/views/components/subscribe_widget.ecr
index a3d6a55f..c9240098 100644
--- a/src/invidious/views/components/subscribe_widget.ecr
+++ b/src/invidious/views/components/subscribe_widget.ecr
@@ -1,23 +1,35 @@
<% if user %>
<% if subscriptions.includes? ucid %>
-
<% else %>
-
<% end %>
+
+
+
<% else %>
-
-
diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr
index 1f21f281..be594556 100644
--- a/src/invidious/views/watch.ecr
+++ b/src/invidious/views/watch.ecr
@@ -276,11 +276,6 @@ function number_with_separator(val) {
return val;
}
-<% ucid = video.ucid %>
-<% author = video.author %>
-<% sub_count_text = video.sub_count_text %>
-<%= rendered "components/subscribe_widget_script" %>
-
<% if plid %>
function get_playlist(plid, timeouts = 0) {
playlist = document.getElementById('playlist');