diff options
| author | Stephen Blott | 2015-01-06 05:39:25 +0000 |
|---|---|---|
| committer | Stephen Blott | 2015-01-06 05:39:25 +0000 |
| commit | 30dee76c6ab1de9e2a62701dacffc29fa5be0866 (patch) | |
| tree | 1aab7586b612a92222a4cfe85f4d4f5173e236bc /content_scripts/ui_component.coffee | |
| parent | 3620fec662ab89bd4f7827e66deec49ff4d11b8e (diff) | |
| parent | fc2201b996e47ca06090e10e4ebfcd9f4b345fde (diff) | |
| download | vimium-30dee76c6ab1de9e2a62701dacffc29fa5be0866.tar.bz2 | |
Merge pull request #1407 from smblott-github/post-1.46
Merge post-1.46 in its entirety
Diffstat (limited to 'content_scripts/ui_component.coffee')
| -rw-r--r-- | content_scripts/ui_component.coffee | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/content_scripts/ui_component.coffee b/content_scripts/ui_component.coffee new file mode 100644 index 00000000..c4ed3bf6 --- /dev/null +++ b/content_scripts/ui_component.coffee @@ -0,0 +1,54 @@ +class UIComponent + iframeElement: null + iframePort: null + showing: null + + constructor: (iframeUrl, className, @handleMessage) -> + @iframeElement = document.createElement "iframe" + @iframeElement.className = className + @iframeElement.seamless = "seamless" + @iframeElement.src = chrome.runtime.getURL iframeUrl + @iframeElement.addEventListener "load", => @openPort() + document.documentElement.appendChild @iframeElement + @showing = true # The iframe is visible now. + # Hide the iframe, but don't interfere with the focus. + @hide false + + # 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 vimiumSecret so the iframe can determine that our message isn't the page impersonating us. + chrome.storage.local.get "vimiumSecret", ({vimiumSecret: secret}) => + @iframeElement.contentWindow.postMessage secret, chrome.runtime.getURL(""), [messageChannel.port2] + + postMessage: (message) -> + @iframePort.postMessage message + + activate: (message) -> + @postMessage message if message? + if @showing + # NOTE(smblott) Experimental. Not sure this is a great idea. If the iframe was already showing, then + # the user gets no visual feedback when it is re-focused. So flash its border. + @iframeElement.classList.add "vimiumUIComponentReactivated" + setTimeout((=> @iframeElement.classList.remove "vimiumUIComponentReactivated"), 200) + else + @show() + @iframeElement.focus() + + show: (message) -> + @postMessage message if message? + @iframeElement.classList.remove "vimiumUIComponentHidden" + @iframeElement.classList.add "vimiumUIComponentShowing" + @showing = true + + hide: (focusWindow = true)-> + @iframeElement.classList.remove "vimiumUIComponentShowing" + @iframeElement.classList.add "vimiumUIComponentHidden" + window.focus() if focusWindow + @showing = false + +root = exports ? window +root.UIComponent = UIComponent |
