From b91d3436796827a9187987759710b22301e35b73 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 14 Feb 2013 17:28:03 +0100 Subject: [PATCH] Use link+blob urls for the stylesheet. That'll help me inspect the css. I wonder if it matters performance-wise. --- 4chan_x.user.js | 36 ++++++++++++++++++++++++------------ lib/$.coffee | 13 ++++++++++--- src/features.coffee | 2 +- src/qr.coffee | 16 +++++++++------- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 43a3655b2..283960803 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -733,10 +733,17 @@ return setTimeout($.asap, 25, test, cb); } }, - addStyle: function(css) { + addStyle: function(css, type) { var style; - style = $.el('style', { + style = type === 'style' || !window.URL ? $.el('style', { textContent: css + }) : $.el('link', { + rel: 'stylesheet', + href: URL.createObjectURL(new Blob([css])) + }, { + type: 'text/css' + }, { + title: g.NAMESPACE }); $.asap((function() { return d.head; @@ -3505,7 +3512,7 @@ if (checked) { $.on(window, 'resize', ImageExpand.resize); if (!ImageExpand.style) { - ImageExpand.style = $.addStyle(''); + ImageExpand.style = $.addStyle(null, 'style'); } return ImageExpand.resize(); } else { @@ -4474,7 +4481,7 @@ } _Class.prototype.setFile = function(file) { - var fileUrl, img, url, + var fileUrl, img, _this = this; this.file = file; this.el.title = "" + file.name + " (" + ($.bytesToString(file.size)) + ")"; @@ -4485,10 +4492,10 @@ this.el.style.backgroundImage = null; return; } - if (!(url = window.URL || window.webkitURL)) { + if (!window.URL) { return; } - url.revokeObjectURL(this.url); + URL.revokeObjectURL(this.url); fileUrl = url.createObjectURL(file); img = $.el('img'); $.on(img, 'load', function() { @@ -4516,17 +4523,16 @@ for (i = _i = 0; 0 <= l ? _i < l : _i > l; i = 0 <= l ? ++_i : --_i) { ui8a[i] = data.charCodeAt(i); } - _this.url = url.createObjectURL(new Blob([ui8a], { + _this.url = URL.createObjectURL(new Blob([ui8a], { type: 'image/png' })); _this.el.style.backgroundImage = "url(" + _this.url + ")"; - return typeof url.revokeObjectURL === "function" ? url.revokeObjectURL(fileUrl) : void 0; + return URL.revokeObjectURL(fileUrl); }); return img.src = fileUrl; }; _Class.prototype.rmFile = function() { - var _base; QR.resetFileInput(); delete this.file; this.el.title = null; @@ -4534,7 +4540,10 @@ if (QR.spoiler) { $('label', this.el).hidden = true; } - return typeof (_base = window.URL || window.webkitURL).revokeObjectURL === "function" ? _base.revokeObjectURL(this.url) : void 0; + if (!window.URL) { + return; + } + return URL.revokeObjectURL(this.url); }; _Class.prototype.select = function() { @@ -4599,7 +4608,7 @@ }; _Class.prototype.rm = function() { - var index, _ref; + var index; QR.resetFileInput(); $.rm(this.el); index = QR.replies.indexOf(this); @@ -4609,7 +4618,10 @@ (QR.replies[index - 1] || QR.replies[index + 1]).select(); } QR.replies.splice(index, 1); - return (_ref = window.URL || window.webkitURL) != null ? _ref.revokeObjectURL(this.url) : void 0; + if (!window.URL) { + return; + } + return URL.revokeObjectURL(this.url); }; return _Class; diff --git a/lib/$.coffee b/lib/$.coffee index eb476c47f..a1262a1c6 100644 --- a/lib/$.coffee +++ b/lib/$.coffee @@ -83,9 +83,16 @@ $.extend $, cb() else setTimeout $.asap, 25, test, cb - addStyle: (css) -> - style = $.el 'style', - textContent: css + addStyle: (css, type) -> + style = if type is 'style' or !window.URL + $.el 'style', + textContent: css + else + $.el 'link', + rel: 'stylesheet' + href: URL.createObjectURL new Blob [css], + type: 'text/css' + title: g.NAMESPACE $.asap (-> d.head), -> $.add d.head, style style diff --git a/src/features.coffee b/src/features.coffee index 03e797fd9..25c646038 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -2134,7 +2134,7 @@ ImageExpand = if checked $.on window, 'resize', ImageExpand.resize unless ImageExpand.style - ImageExpand.style = $.addStyle '' + ImageExpand.style = $.addStyle null, 'style' ImageExpand.resize() else $.off window, 'resize', ImageExpand.resize diff --git a/src/qr.coffee b/src/qr.coffee index 3accbbe9f..a144d6ca5 100644 --- a/src/qr.coffee +++ b/src/qr.coffee @@ -351,9 +351,9 @@ QR = unless /^image/.test file.type @el.style.backgroundImage = null return - # XXX Opera does not support window.URL - return unless url = window.URL or window.webkitURL - url.revokeObjectURL @url + # XXX Opera does not support blob URL + return unless window.URL + URL.revokeObjectURL @url # Create a redimensioned thumbnail. fileUrl = url.createObjectURL file @@ -388,9 +388,9 @@ QR = for i in [0...l] ui8a[i] = data.charCodeAt i - @url = url.createObjectURL new Blob [ui8a], type: 'image/png' + @url = URL.createObjectURL new Blob [ui8a], type: 'image/png' @el.style.backgroundImage = "url(#{@url})" - url.revokeObjectURL? fileUrl + URL.revokeObjectURL fileUrl img.src = fileUrl rmFile: -> @@ -399,7 +399,8 @@ QR = @el.title = null @el.style.backgroundImage = null $('label', @el).hidden = true if QR.spoiler - (window.URL or window.webkitURL).revokeObjectURL? @url + return unless window.URL + URL.revokeObjectURL @url select: -> QR.selected?.el.id = null QR.selected = @ @@ -446,7 +447,8 @@ QR = else if @el.id is 'selected' (QR.replies[index-1] or QR.replies[index+1]).select() QR.replies.splice index, 1 - (window.URL or window.webkitURL)?.revokeObjectURL @url + return unless window.URL + URL.revokeObjectURL @url captcha: init: ->