Fix some drive userscript issues

This commit is contained in:
Calvin Montgomery 2016-10-07 19:55:41 -07:00
parent 7c3f2d0a8b
commit d0d2002a5f
6 changed files with 79 additions and 43 deletions

View file

@ -7,7 +7,7 @@
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// @connect docs.google.com // @connect docs.google.com
// @run-at document-end // @run-at document-end
// @version 1.1.0 // @version 1.2.0
// ==/UserScript== // ==/UserScript==
try { try {
@ -67,18 +67,14 @@ try {
}); });
if (data.status === 'fail') { if (data.status === 'fail') {
error = new Error('Google Docs request failed: ' + var error = 'Google Docs request failed: ' +
'metadata indicated status=fail'); unescape(data.reason).replace(/\+/g, ' ');
error.response = res.responseText;
error.reason = 'RESPONSE_STATUS_FAIL';
return cb(error); return cb(error);
} }
if (!data.fmt_stream_map) { if (!data.fmt_stream_map) {
error = new Error('Google Docs request failed: ' + var error = 'Google Docs request failed: ' +
'metadata lookup returned no valid links'); 'metadata lookup returned no valid links';
error.response = res.responseText;
error.reason = 'MISSING_LINKS';
return cb(error); return cb(error);
} }
@ -96,8 +92,8 @@ try {
}, },
onerror: function () { onerror: function () {
var error = new Error('Google Docs request failed: ' + var error = 'Google Docs request failed: ' +
'metadata lookup HTTP request failed'); 'metadata lookup HTTP request failed';
error.reason = 'HTTP_ONERROR'; error.reason = 'HTTP_ONERROR';
return cb(error); return cb(error);
} }
@ -205,6 +201,7 @@ try {
unsafeWindow.console.log('Initialized userscript Google Drive player'); unsafeWindow.console.log('Initialized userscript Google Drive player');
unsafeWindow.hasDriveUserscript = true; unsafeWindow.hasDriveUserscript = true;
unsafeWindow.driveUserscriptVersion = '1.2';
} catch (error) { } catch (error) {
unsafeWindow.console.error(error); unsafeWindow.console.error(error);
} }

View file

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.23.2", "version": "3.23.3",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View file

@ -6,13 +6,14 @@ window.GoogleDrivePlayer = class GoogleDrivePlayer extends VideoJSPlayer
super(data) super(data)
load: (data) -> load: (data) ->
window.maybePromptToUpgradeUserscript()
if typeof window.getGoogleDriveMetadata is 'function' if typeof window.getGoogleDriveMetadata is 'function'
window.getGoogleDriveMetadata(data.id, (error, metadata) => window.getGoogleDriveMetadata(data.id, (error, metadata) =>
if error if error
console.error(error) console.error(error)
alertBox = window.document.createElement('div') alertBox = window.document.createElement('div')
alertBox.className = 'alert alert-danger' alertBox.className = 'alert alert-danger'
alertBox.textContent = error.message alertBox.textContent = error
document.getElementById('ytapiplayer').appendChild(alertBox) document.getElementById('ytapiplayer').appendChild(alertBox)
else else
data.meta.direct = metadata.videoMap data.meta.direct = metadata.videoMap

View file

@ -49,7 +49,7 @@ html(lang="en")
| Tampermonkey supports many browsers besides Chrome. | Tampermonkey supports many browsers besides Chrome.
p. p.
Once you have installed the userscript manager addon for your browser, Once you have installed the userscript manager addon for your browser,
you can <a href="/js/cytube-google-drive.user.js?v=1.1" target="_blank"> you can <a href="/js/cytube-google-drive.user.js" target="_blank">
install the userscript</a>. If this link 404s, it means the administrator install the userscript</a>. If this link 404s, it means the administrator
of this server hasn't generated it yet. of this server hasn't generated it yet.
p. p.
@ -58,17 +58,3 @@ html(lang="en")
include footer include footer
+footer() +footer()
script(type="text/javascript").
function showEmail(btn, email, key) {
email = unescape(email);
key = unescape(key);
var dest = new Array(email.length);
for (var i = 0; i < email.length; i++) {
dest[i] = String.fromCharCode(email.charCodeAt(i) ^ key.charCodeAt(i % key.length));
}
email = dest.join("");
$("<a/>").attr("href", "mailto:" + email)
.text(email)
.insertBefore(btn);
$(btn).remove();
}

View file

@ -679,6 +679,7 @@
} }
GoogleDrivePlayer.prototype.load = function(data) { GoogleDrivePlayer.prototype.load = function(data) {
window.maybePromptToUpgradeUserscript();
if (typeof window.getGoogleDriveMetadata === 'function') { if (typeof window.getGoogleDriveMetadata === 'function') {
return window.getGoogleDriveMetadata(data.id, (function(_this) { return window.getGoogleDriveMetadata(data.id, (function(_this) {
return function(error, metadata) { return function(error, metadata) {
@ -687,7 +688,7 @@
console.error(error); console.error(error);
alertBox = window.document.createElement('div'); alertBox = window.document.createElement('div');
alertBox.className = 'alert alert-danger'; alertBox.className = 'alert alert-danger';
alertBox.textContent = error.message; alertBox.textContent = error;
return document.getElementById('ytapiplayer').appendChild(alertBox); return document.getElementById('ytapiplayer').appendChild(alertBox);
} else { } else {
data.meta.direct = metadata.videoMap; data.meta.direct = metadata.videoMap;
@ -1019,7 +1020,7 @@
}; };
TwitchPlayer.prototype.load = function(data) { TwitchPlayer.prototype.load = function(data) {
var error, error1; var error;
this.setMediaProperties(data); this.setMediaProperties(data);
try { try {
if (data.type === 'tv') { if (data.type === 'tv') {
@ -1034,7 +1035,7 @@
}; };
TwitchPlayer.prototype.pause = function() { TwitchPlayer.prototype.pause = function() {
var error, error1; var error;
try { try {
this.twitch.pause(); this.twitch.pause();
return this.paused = true; return this.paused = true;
@ -1045,7 +1046,7 @@
}; };
TwitchPlayer.prototype.play = function() { TwitchPlayer.prototype.play = function() {
var error, error1; var error;
try { try {
this.twitch.play(); this.twitch.play();
return this.paused = false; return this.paused = false;
@ -1056,7 +1057,7 @@
}; };
TwitchPlayer.prototype.seekTo = function(time) { TwitchPlayer.prototype.seekTo = function(time) {
var error, error1; var error;
try { try {
return this.twitch.seek(time); return this.twitch.seek(time);
} catch (error1) { } catch (error1) {
@ -1066,7 +1067,7 @@
}; };
TwitchPlayer.prototype.getTime = function(cb) { TwitchPlayer.prototype.getTime = function(cb) {
var error, error1; var error;
try { try {
return cb(this.twitch.getCurrentTime()); return cb(this.twitch.getCurrentTime());
} catch (error1) { } catch (error1) {
@ -1076,7 +1077,7 @@
}; };
TwitchPlayer.prototype.setVolume = function(volume) { TwitchPlayer.prototype.setVolume = function(volume) {
var error, error1; var error;
try { try {
this.twitch.setVolume(volume); this.twitch.setVolume(volume);
if (volume > 0) { if (volume > 0) {
@ -1089,7 +1090,7 @@
}; };
TwitchPlayer.prototype.getVolume = function(cb) { TwitchPlayer.prototype.getVolume = function(cb) {
var error, error1; var error;
try { try {
if (this.twitch.isPaused()) { if (this.twitch.isPaused()) {
return cb(0); return cb(0);
@ -1513,7 +1514,7 @@
}; };
window.loadMediaPlayer = function(data) { window.loadMediaPlayer = function(data) {
var e, error, error1, error2, error3, error4; var e, error;
try { try {
if (window.PLAYER) { if (window.PLAYER) {
window.PLAYER.destroy(); window.PLAYER.destroy();
@ -1525,8 +1526,8 @@
if (data.meta.direct && data.type !== 'gd') { if (data.meta.direct && data.type !== 'gd') {
try { try {
return window.PLAYER = new VideoJSPlayer(data); return window.PLAYER = new VideoJSPlayer(data);
} catch (error2) { } catch (error1) {
e = error2; e = error1;
return console.error(e); return console.error(e);
} }
} else if (data.type === 'gd') { } else if (data.type === 'gd') {
@ -1536,15 +1537,15 @@
} else { } else {
return window.PLAYER = new GoogleDriveYouTubePlayer(data); return window.PLAYER = new GoogleDriveYouTubePlayer(data);
} }
} catch (error3) { } catch (error1) {
e = error3; e = error1;
return console.error(e); return console.error(e);
} }
} else if (data.type in TYPE_MAP) { } else if (data.type in TYPE_MAP) {
try { try {
return window.PLAYER = TYPE_MAP[data.type](data); return window.PLAYER = TYPE_MAP[data.type](data);
} catch (error4) { } catch (error1) {
e = error4; e = error1;
return console.error(e); return console.error(e);
} }
} }

View file

@ -3205,3 +3205,54 @@ function stopQueueSpinner(data) {
$("#queueprogress").remove(); $("#queueprogress").remove();
} }
} }
function maybePromptToUpgradeUserscript() {
if (document.getElementById('prompt-upgrade-drive-userscript')) {
return;
}
if (!window.hasDriveUserscript) {
return;
}
var currentVersion = [1, 2];
var userscriptVersion = window.driveUserscriptVersion;
if (!userscriptVersion) {
userscriptVersion = '1.0';
}
userscriptVersion = userscriptVersion.split('.').map(function (part) {
return parseInt(part, 10);
});
var older = false;
for (var i = 0; i < currentVersion.length; i++) {
if (userscriptVersion[i] < currentVersion[i]) {
older = true;
}
}
if (!older) {
return;
}
var alertBox = document.createElement('div');
alertBox.id = 'prompt-upgrade-drive-userscript';
alertBox.className = 'alert alert-info'
alertBox.innerHTML = 'A newer version of the Google Drive userscript is available.';
alertBox.appendChild(document.createElement('br'));
var infoLink = document.createElement('a');
infoLink.className = 'btn btn-info';
infoLink.href = '/google_drive_userscript';
infoLink.textContent = 'Click here for installation instructions';
infoLink.target = '_blank';
alertBox.appendChild(infoLink);
var closeButton = document.createElement('button');
closeButton.className = 'close pull-right';
closeButton.innerHTML = '&times;';
closeButton.onclick = function () {
alertBox.parentNode.removeChild(alertBox);
}
alertBox.insertBefore(closeButton, alertBox.firstChild)
document.getElementById('videowrap').appendChild(alertBox);
}