Add workaround for GM sandbox and refactor userscript a bit

This commit is contained in:
Calvin Montgomery 2016-08-20 10:59:20 -07:00
parent 8d3b2e59df
commit 578d3fbb23
5 changed files with 155 additions and 62 deletions

View File

@ -7,16 +7,20 @@
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// @connect docs.google.com // @connect docs.google.com
// @run-at document-end // @run-at document-end
// @version 1.0.0 // @version 1.1.0
// ==/UserScript== // ==/UserScript==
(function () { try {
function debug(message) { function debug(message) {
if (!unsafeWindow.enableCyTubeGoogleDriveUserscriptDebug) { if (!unsafeWindow.enableCyTubeGoogleDriveUserscriptDebug) {
return; return;
} }
unsafeWindow.console.log.apply(unsafeWindow.console, arguments); try {
unsafeWindow.console.log(message);
} catch (error) {
unsafeWindow.console.error(error);
}
} }
var ITAG_QMAP = { var ITAG_QMAP = {
@ -53,6 +57,8 @@
method: 'GET', method: 'GET',
url: url, url: url,
onload: function (res) { onload: function (res) {
try {
debug('Got response ' + res.responseText);
var data = {}; var data = {};
var error; var error;
res.responseText.split('&').forEach(function (kv) { res.responseText.split('&').forEach(function (kv) {
@ -81,8 +87,12 @@
var pair = item.split('|'); var pair = item.split('|');
data.links[pair[0]] = pair[1]; data.links[pair[0]] = pair[1];
}); });
data.videoMap = mapLinks(data.links);
cb(null, data); cb(null, data);
} catch (error) {
unsafeWindow.console.error(error);
}
}, },
onerror: function () { onerror: function () {
@ -118,36 +128,83 @@
return videos; return videos;
} }
function GoogleDrivePlayer(data) { /*
if (!(this instanceof GoogleDrivePlayer)) { * Greasemonkey 2.0 has this wonderful sandbox that attempts
return new GoogleDrivePlayer(data); * to prevent script developers from shooting themselves in
* the foot by removing the trigger from the gun, i.e. it's
* impossible to cross the boundary between the browser JS VM
* and the privileged sandbox that can run GM_xmlhttpRequest().
*
* So in this case, we have to resort to polling a special
* variable to see if getGoogleDriveMetadata needs to be called
* and deliver the result into another special variable that is
* being polled on the browser side.
*/
/*
* Browser side function -- sets gdUserscript.pollID to the
* ID of the Drive video to be queried and polls
* gdUserscript.pollResult for the result.
*/
function getGoogleDriveMetadata_GM(id, callback) {
debug('Setting GD poll ID to ' + id);
unsafeWindow.gdUserscript.pollID = id;
var tries = 0;
var i = setInterval(function () {
if (unsafeWindow.gdUserscript.pollResult) {
debug('Got result');
clearInterval(i);
var result = unsafeWindow.gdUserscript.pollResult;
unsafeWindow.gdUserscript.pollResult = null;
callback(result.error, result.result);
} else if (++tries > 100) {
// Took longer than 10 seconds, give up
clearInterval(i);
}
}, 100);
} }
this.setMediaProperties(data); /*
this.load(data); * Sandbox side function -- polls gdUserscript.pollID for
} * the ID of a Drive video to be queried, looks up the
* metadata, and stores it in gdUserscript.pollResult
GoogleDrivePlayer.prototype = Object.create(unsafeWindow.VideoJSPlayer.prototype); */
function setupGDPoll() {
GoogleDrivePlayer.prototype.load = function (data) { unsafeWindow.gdUserscript = cloneInto({}, unsafeWindow);
var self = this; var pollInterval = setInterval(function () {
getVideoInfo(data.id, function (err, videoData) { if (unsafeWindow.gdUserscript.pollID) {
if (err) { var id = unsafeWindow.gdUserscript.pollID;
debug(err); unsafeWindow.gdUserscript.pollID = null;
var alertBox = unsafeWindow.document.createElement('div'); debug('Polled and got ' + id);
alertBox.className = 'alert alert-danger'; getVideoInfo(id, function (error, data) {
alertBox.textContent = err.message; unsafeWindow.gdUserscript.pollResult = cloneInto({
document.getElementById('ytapiplayer').appendChild(alertBox); error: error,
return; result: data
} }, unsafeWindow);
debug('Retrieved links: ' + JSON.stringify(videoData.links));
data.meta.direct = mapLinks(videoData.links);
unsafeWindow.VideoJSPlayer.prototype.loadPlayer.call(self, data);
}); });
}; }
}, 1000);
}
function isRunningTampermonkey() {
try {
return GM_info.scriptHandler === 'Tampermonkey';
} catch (error) {
return false;
}
}
if (isRunningTampermonkey()) {
unsafeWindow.getGoogleDriveMetadata = getVideoInfo;
} else {
debug('Using non-TM polling workaround');
unsafeWindow.getGoogleDriveMetadata = exportFunction(
getGoogleDriveMetadata_GM, unsafeWindow);
setupGDPoll();
}
unsafeWindow.GoogleDrivePlayer = GoogleDrivePlayer;
unsafeWindow.console.log('Initialized userscript Google Drive player'); unsafeWindow.console.log('Initialized userscript Google Drive player');
unsafeWindow.hasDriveUserscript = true; unsafeWindow.hasDriveUserscript = true;
})(); } catch (error) {
unsafeWindow.console.error(error);
}

View File

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

View File

@ -33,7 +33,7 @@ window.loadMediaPlayer = (data) ->
else if data.type is 'gd' else if data.type is 'gd'
try try
if data.meta.html5hack or window.hasDriveUserscript if data.meta.html5hack or window.hasDriveUserscript
window.PLAYER = new window.GoogleDrivePlayer(data) window.PLAYER = new GoogleDrivePlayer(data)
else else
window.PLAYER = new GoogleDriveYouTubePlayer(data) window.PLAYER = new GoogleDriveYouTubePlayer(data)
catch e catch e

View File

@ -43,8 +43,7 @@ window.VideoJSPlayer = class VideoJSPlayer extends Player
if not (this instanceof VideoJSPlayer) if not (this instanceof VideoJSPlayer)
return new VideoJSPlayer(data) return new VideoJSPlayer(data)
@setMediaProperties(data) @load(data)
@loadPlayer(data)
loadPlayer: (data) -> loadPlayer: (data) ->
waitUntilDefined(window, 'videojs', => waitUntilDefined(window, 'videojs', =>

View File

@ -503,8 +503,7 @@
if (!(this instanceof VideoJSPlayer)) { if (!(this instanceof VideoJSPlayer)) {
return new VideoJSPlayer(data); return new VideoJSPlayer(data);
} }
this.setMediaProperties(data); this.load(data);
this.loadPlayer(data);
} }
VideoJSPlayer.prototype.loadPlayer = function(data) { VideoJSPlayer.prototype.loadPlayer = function(data) {
@ -676,6 +675,28 @@
GoogleDrivePlayer.__super__.constructor.call(this, data); GoogleDrivePlayer.__super__.constructor.call(this, data);
} }
GoogleDrivePlayer.prototype.load = function(data) {
if (typeof window.getGoogleDriveMetadata === 'function') {
return window.getGoogleDriveMetadata(data.id, (function(_this) {
return function(error, metadata) {
var alertBox;
if (error) {
console.error(error);
alertBox = window.document.createElement('div');
alertBox.className = 'alert alert-danger';
alertBox.textContent = error.message;
return document.getElementById('ytapiplayer').appendChild(alertBox);
} else {
data.meta.direct = metadata.videoMap;
return GoogleDrivePlayer.__super__.load.call(_this, data);
}
};
})(this));
} else {
return GoogleDrivePlayer.__super__.load.call(this, data);
}
};
return GoogleDrivePlayer; return GoogleDrivePlayer;
})(VideoJSPlayer); })(VideoJSPlayer);
@ -1359,7 +1380,7 @@
} else if (data.type === 'gd') { } else if (data.type === 'gd') {
try { try {
if (data.meta.html5hack || window.hasDriveUserscript) { if (data.meta.html5hack || window.hasDriveUserscript) {
return window.PLAYER = new window.GoogleDrivePlayer(data); return window.PLAYER = new GoogleDrivePlayer(data);
} else { } else {
return window.PLAYER = new GoogleDriveYouTubePlayer(data); return window.PLAYER = new GoogleDriveYouTubePlayer(data);
} }