diff options
| author | mrmr1993 | 2014-12-28 19:24:33 +0000 |
|---|---|---|
| committer | mrmr1993 | 2014-12-28 19:24:33 +0000 |
| commit | b26f4aed8585418d18dfc43262070c8c8741e5e3 (patch) | |
| tree | 1b5568908c42bda73554e1c145ec8e54ca9c84ee | |
| parent | 25473c3dea3d847d36bda9c56a190dd3b0e5a43f (diff) | |
| download | vimium-b26f4aed8585418d18dfc43262070c8c8741e5e3.tar.bz2 | |
Add UIComponent code for iframes
| -rw-r--r-- | content_scripts/ui_component.coffee | 66 | ||||
| -rw-r--r-- | manifest.json | 1 |
2 files changed, 67 insertions, 0 deletions
diff --git a/content_scripts/ui_component.coffee b/content_scripts/ui_component.coffee new file mode 100644 index 00000000..c0889e7f --- /dev/null +++ b/content_scripts/ui_component.coffee @@ -0,0 +1,66 @@ +class UIComponent + iframeElement: null + iframePort: null + messageEventListeners: [] + showStyle: "" + hideStyle: "" + + constructor: (iframeUrl, className) -> + @iframeElement = document.createElement "iframe" + @iframeElement.className = className + @iframeElement.seamless = "seamless" + @iframeElement.src = chrome.runtime.getURL iframeUrl + @iframeElement.addEventListener "load", => @openPort() + document.documentElement.appendChild @iframeElement + @hide() + + # Open a port and pass it to the iframe via window.postMessage. + openPort: -> + messageChannel = new MessageChannel() + @iframePort = messageChannel.port1 + @iframePort.onmessage = (event) => @handleMessage event + + # Get iframeMessageSecret so the iframe can determine that our message isn't the page impersonating us. + chrome.storage.local.get "iframeMessageSecret", ({iframeMessageSecret: secret}) => + @iframeElement.contentWindow.postMessage secret, chrome.runtime.getURL(""), [messageChannel.port2] + + postMessage: (data) -> @iframePort.postMessage data + + # Execute each event listener on the current event until we get a falsy return value. + handleMessage: (event) -> + for listener in @messageEventListeners + retVal = listener.call this, event + return false unless retVal + true + + addEventListener: (type, listener) -> + if type == "message" + @messageEventListeners.push listener + undefined + + removeEventListener: (type, listener) -> + if type == "message" + listenerIndex = @messageEventListeners.indexOf listener + if listenerIndex > -1 + @messageEventListeners = @messageEventListeners.splice listenerIndex, 1 + undefined + + setHideStyle: (@hideStyle) -> + @hide() if @showing == false + + setShowStyle: (@showStyle) -> + @show() if @showing == true + + show: -> + return unless @iframeElement? + @iframeElement.setAttribute "style", @showStyle + @iframeElement.focus() + @showing = true + + hide: -> + return unless @iframeElement? + @iframeElement.setAttribute "style", @hideStyle + @showing = false + +root = exports ? window +root.UIComponent = UIComponent diff --git a/manifest.json b/manifest.json index 3cd88d1e..a92ed0da 100644 --- a/manifest.json +++ b/manifest.json @@ -37,6 +37,7 @@ "lib/dom_utils.js", "lib/handler_stack.js", "lib/clipboard.js", + "content_scripts/ui_component.js", "content_scripts/link_hints.js", "content_scripts/vomnibar.js", "content_scripts/scroller.js", |
