From 2c6d67c05b6ef57aabda288a164dd2885894ae89 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Fri, 18 Sep 2015 18:45:41 +0100 Subject: Load UIComponent stylesheet via XMLHttpRequest, use background as needed --- background_scripts/main.coffee | 1 + content_scripts/ui_component.coffee | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index df841028..10f7500e 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -668,6 +668,7 @@ sendRequestHandlers = setIcon: setIcon sendMessageToFrames: sendMessageToFrames log: bgLog + fetchFileContents: (request, sender) -> fetchFileContents request.fileName # We always remove chrome.storage.local/findModeRawQueryListIncognito on startup. chrome.storage.local.remove "findModeRawQueryListIncognito" diff --git a/content_scripts/ui_component.coffee b/content_scripts/ui_component.coffee index 2348d00b..2218c3c7 100644 --- a/content_scripts/ui_component.coffee +++ b/content_scripts/ui_component.coffee @@ -4,15 +4,16 @@ class UIComponent showing: null options: null shadowDOM: null + styleSheetGetter: null constructor: (iframeUrl, className, @handleMessage) -> styleSheet = DomUtils.createElement "style" styleSheet.type = "text/css" # Default to everything hidden while the stylesheet loads. - styleSheet.innerHTML = """ - @import url("#{chrome.runtime.getURL("content_scripts/vimium.css")}"); - iframe {display: none;} - """ + styleSheet.innerHTML = "iframe {display: none;}" + + UIComponent::styleSheetGetter ?= new AsyncDataFetcher @fetchStyles + @styleSheetGetter.use (styles) -> styleSheet.innerHTML = styles @iframeElement = DomUtils.createElement "iframe" extend @iframeElement, @@ -115,5 +116,22 @@ class UIComponent window.removeEventListener "focus", handler refocusSourceFrame() + fetchStyles: (callback) -> + stylesheetRequest = new XMLHttpRequest() + + stylesheetRequest.onload = -> + return stylesheetRequest.onerror() unless stylesheetRequest.status == 200 + callback stylesheetRequest.responseText + + stylesheetRequest.onerror = -> + chrome.runtime.sendMessage + handler: "fetchFileContents" + fileName: "content_scripts/vimium.css" + , callback + + stylesheetRequest.open "GET", (chrome.runtime.getURL "content_scripts/vimium.css"), true + stylesheetRequest.send() + + root = exports ? window root.UIComponent = UIComponent -- cgit v1.2.3 From 2eb6d9a7d3412e4b46e081caa7054f925ffe7e92 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 19 Sep 2015 12:05:24 +0100 Subject: Tidy up #1833. --- content_scripts/ui_component.coffee | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/content_scripts/ui_component.coffee b/content_scripts/ui_component.coffee index 2218c3c7..72627f69 100644 --- a/content_scripts/ui_component.coffee +++ b/content_scripts/ui_component.coffee @@ -12,7 +12,7 @@ class UIComponent # Default to everything hidden while the stylesheet loads. styleSheet.innerHTML = "iframe {display: none;}" - UIComponent::styleSheetGetter ?= new AsyncDataFetcher @fetchStyles + UIComponent::styleSheetGetter ?= new AsyncDataFetcher @fetchFileContents "content_scripts/vimium.css" @styleSheetGetter.use (styles) -> styleSheet.innerHTML = styles @iframeElement = DomUtils.createElement "iframe" @@ -116,21 +116,26 @@ class UIComponent window.removeEventListener "focus", handler refocusSourceFrame() - fetchStyles: (callback) -> - stylesheetRequest = new XMLHttpRequest() + # Fetch a Vimium file/resource (such as "content_scripts/vimium.css"). + # We try making an XMLHttpRequest request. That can fail (see #1817), in which case we fetch the + # file/resource via the background page. + fetchFileContents: (file) -> (callback) -> + request = new XMLHttpRequest() - stylesheetRequest.onload = -> - return stylesheetRequest.onerror() unless stylesheetRequest.status == 200 - callback stylesheetRequest.responseText + request.onload = -> + if request.status == 200 + callback request.responseText + else + request.onerror() - stylesheetRequest.onerror = -> + request.onerror = -> chrome.runtime.sendMessage handler: "fetchFileContents" - fileName: "content_scripts/vimium.css" + fileName: file , callback - stylesheetRequest.open "GET", (chrome.runtime.getURL "content_scripts/vimium.css"), true - stylesheetRequest.send() + request.open "GET", (chrome.runtime.getURL file), true + request.send() root = exports ? window -- cgit v1.2.3