Browse Source

Added URL support to sandbox service.

pull/216/head
Simon Eisenmann 10 years ago
parent
commit
e35dd0f8a3
  1. 2
      static/js/directives/odfcanvas.js
  2. 2
      static/js/directives/pdfcanvas.js
  3. 2
      static/js/directives/youtubevideo.js
  4. 52
      static/js/services/sandbox.js

2
static/js/directives/odfcanvas.js

@ -36,7 +36,7 @@ define(['require', 'underscore', 'jquery', 'text!partials/odfcanvas_sandbox.html
template = template.replace(/__PARENT_ORIGIN__/g, $window.location.protocol + "//" + $window.location.host); template = template.replace(/__PARENT_ORIGIN__/g, $window.location.protocol + "//" + $window.location.host);
template = template.replace(/__WEBODF_SANDBOX_JS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('sandboxes/webodf') + ".js")); template = template.replace(/__WEBODF_SANDBOX_JS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('sandboxes/webodf') + ".js"));
template = template.replace(/__WEBODF_URL__/g, restURL.createAbsoluteUrl(require.toUrl('webodf') + ".js")); template = template.replace(/__WEBODF_URL__/g, restURL.createAbsoluteUrl(require.toUrl('webodf') + ".js"));
var sandboxApi = sandbox.createSandbox(container, template, "allow-scripts", null, { var sandboxApi = sandbox.createSandbox(container, template, null, "allow-scripts", null, {
allowfullscreen: true, allowfullscreen: true,
mozallowfullscreen: true, mozallowfullscreen: true,
webkitallowfullscreen: true webkitallowfullscreen: true

2
static/js/directives/pdfcanvas.js

@ -36,7 +36,7 @@ define(['require', 'underscore', 'jquery', 'text!partials/pdfcanvas_sandbox.html
template = template.replace(/__PDFJS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('pdf') + ".js")); template = template.replace(/__PDFJS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('pdf') + ".js"));
template = template.replace(/__PDFJS_WORKER_URL__/g, restURL.createAbsoluteUrl(require.toUrl('pdf.worker') + ".js")); template = template.replace(/__PDFJS_WORKER_URL__/g, restURL.createAbsoluteUrl(require.toUrl('pdf.worker') + ".js"));
template = template.replace(/__PDFJS_COMPATIBILITY_URL__/g, restURL.createAbsoluteUrl(require.toUrl('libs/pdf/compatibility') + ".js")); template = template.replace(/__PDFJS_COMPATIBILITY_URL__/g, restURL.createAbsoluteUrl(require.toUrl('libs/pdf/compatibility') + ".js"));
var sandboxApi = sandbox.createSandbox(container, template, "allow-scripts", null, { var sandboxApi = sandbox.createSandbox(container, template, null, "allow-scripts", null, {
allowfullscreen: true, allowfullscreen: true,
mozallowfullscreen: true, mozallowfullscreen: true,
webkitallowfullscreen: true webkitallowfullscreen: true

2
static/js/directives/youtubevideo.js

@ -116,7 +116,7 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
var template = sandboxTemplate; var template = sandboxTemplate;
template = template.replace(/__PARENT_ORIGIN__/g, $window.location.protocol + "//" + $window.location.host); template = template.replace(/__PARENT_ORIGIN__/g, $window.location.protocol + "//" + $window.location.host);
template = template.replace(/__YOUTUBE_SANDBOX_JS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('sandboxes/youtube') + ".js")); template = template.replace(/__YOUTUBE_SANDBOX_JS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('sandboxes/youtube') + ".js"));
sandboxApi = sandbox.createSandbox($(".youtubeplayercontainer", $element)[0], template, "allow-scripts allow-same-origin", "youtubeplayer"); sandboxApi = sandbox.createSandbox($(".youtubeplayercontainer", $element)[0], template, null, "allow-scripts allow-same-origin", "youtubeplayer");
sandboxApi.e.on("message", function(event, message) { sandboxApi.e.on("message", function(event, message) {
var msg = message.data; var msg = message.data;

52
static/js/services/sandbox.js

@ -24,18 +24,35 @@ define(["jquery", "underscore"], function($, _) {
return ["$window", function($window) { return ["$window", function($window) {
var Sandbox = function(container, template, sandbox, className, attrs) { var Sandbox = function(container, template, url, sandbox, className, attrs) {
var blob = new $window.Blob([template], {type: "text/html;charset=utf-8"}); this.container = container;
this.url = $window.URL.createObjectURL(blob); this.sandbox = sandbox ? sandbox : "";
this.className = className;
this.attrs = attrs;
if (template) {
var blob = new $window.Blob([template], {type: "text/html;charset=utf-8"});
this.url = this.blobUrl = $window.URL.createObjectURL(blob);
} else if (url) {
this.url = url;
}
if (this.url) {
this.create();
}
};
Sandbox.prototype.create = function() {
if (!this.url) {
return;
}
var iframe; var iframe;
var $container = $(container); var $container = $(this.container);
if ($container.is("iframe")) { if ($container.is("iframe")) {
// Container is iframe. // Container is iframe.
if (className) { if (this.className) {
$container.addClass(className); $container.addClass(this.className);
} }
if (attrs) { if (this.attrs) {
$container.attr(attrs); $container.attr(this.attrs);
} }
iframe = $container[0]; iframe = $container[0];
iframe.src = this.url; iframe.src = this.url;
@ -43,12 +60,12 @@ define(["jquery", "underscore"], function($, _) {
} else { } else {
// Create iframe. // Create iframe.
iframe = $window.document.createElement("iframe"); iframe = $window.document.createElement("iframe");
iframe.sandbox = sandbox; iframe.sandbox = this.sandbox;
if (className) { if (this.className) {
iframe.className = className; iframe.className = this.className;
} }
if (attrs) { if (this.attrs) {
$(iframe).attr(attrs); $(iframe).attr(this.attrs);
} }
iframe.src = this.url; iframe.src = this.url;
$container.append(iframe); $container.append(iframe);
@ -69,10 +86,13 @@ define(["jquery", "underscore"], function($, _) {
$window.removeEventListener("message", this.handler, false); $window.removeEventListener("message", this.handler, false);
this.handler = null; this.handler = null;
} }
if (this.url) { if (this.blobUrl) {
$window.URL.revokeObjectURL(this.url); $window.URL.revokeObjectURL(this.blobUrl);
this.url = null; this.blobUrl = null;
} }
this.url = null;
this.container = null;
this.attrs = null;
if (this.created) { if (this.created) {
$(this.iframe).remove(); $(this.iframe).remove();
} }

Loading…
Cancel
Save