aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2017-11-25 07:29:09 +0000
committerGitHub2017-11-25 07:29:09 +0000
commit58f45ac17938e2984b3d3b83c8aed452f1287498 (patch)
tree505bbdf7678a6b0019785874b7e14f3f56515048
parentdef334e3647136c23c8825d0f04a5f04eba3d9bc (diff)
parentc8a395481eeb950c8725e3d6e0fa58510cb407aa (diff)
downloadvimium-58f45ac17938e2984b3d3b83c8aed452f1287498.tar.bz2
Merge pull request #2601 from mrmr1993/ff-copy-paste
Enable Firefox clipboard commands
-rw-r--r--background_scripts/commands.coffee4
-rw-r--r--content_scripts/hud.coffee29
-rw-r--r--content_scripts/link_hints.coffee2
-rw-r--r--content_scripts/mode_normal.coffee11
-rw-r--r--content_scripts/mode_visual.coffee2
-rw-r--r--lib/clipboard.coffee9
-rw-r--r--manifest.json1
-rw-r--r--pages/help_dialog.coffee2
-rw-r--r--pages/hud.coffee14
-rw-r--r--pages/hud.html1
10 files changed, 65 insertions, 10 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee
index 7e171d31..4d2e1606 100644
--- a/background_scripts/commands.coffee
+++ b/background_scripts/commands.coffee
@@ -340,8 +340,8 @@ commandDescriptions =
toggleViewSource: ["View page source", { noRepeat: true }]
copyCurrentUrl: ["Copy the current URL to the clipboard", { noRepeat: true }]
- openCopiedUrlInCurrentTab: ["Open the clipboard's URL in the current tab", { background: true, noRepeat: true }]
- openCopiedUrlInNewTab: ["Open the clipboard's URL in a new tab", { background: true, repeatLimit: 20 }]
+ openCopiedUrlInCurrentTab: ["Open the clipboard's URL in the current tab", { noRepeat: true }]
+ openCopiedUrlInNewTab: ["Open the clipboard's URL in a new tab", { repeatLimit: 20 }]
enterInsertMode: ["Enter insert mode", { noRepeat: true }]
passNextKey: ["Pass the next key to the page"]
diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee
index 7c983cfa..42a960da 100644
--- a/content_scripts/hud.coffee
+++ b/content_scripts/hud.coffee
@@ -9,6 +9,8 @@ HUD =
findMode: null
abandon: -> @hudUI?.hide false
+ pasteListener: null # Set by @pasteFromClipboard to handle the value returned by pasteResponse
+
# This HUD is styled to precisely mimick the chrome HUD on Mac. Use the "has_popup_and_link_hud.html"
# test harness to tweak these styles to match Chrome's. One limitation of our HUD display is that
# it doesn't sit on top of horizontal scrollbars like Chrome's HUD does.
@@ -82,6 +84,33 @@ HUD =
@findMode.exit()
postExit?()
+ # These commands manage copying and pasting from the clipboard in the HUD frame.
+ # NOTE(mrmr1993): We need this to copy and paste on Firefox:
+ # * an element can't be focused in the background page, so copying/pasting doesn't work
+ # * we don't want to disrupt the focus in the page, in case the page is listening for focus/blur events.
+ # * the HUD shouldn't be active for this frame while any of the copy/paste commands are running.
+ copyToClipboard: (text) ->
+ DomUtils.documentComplete =>
+ @init()
+ @hudUI?.postMessage {name: "copyToClipboard", data: text}
+
+ pasteFromClipboard: (@pasteListener) ->
+ DomUtils.documentComplete =>
+ @init()
+ # Show the HUD frame, so Firefox will actually perform the paste.
+ @hudUI.toggleIframeElementClasses "vimiumUIComponentHidden", "vimiumUIComponentVisible"
+ @tween.fade 0, 0
+ @hudUI.postMessage {name: "pasteFromClipboard"}
+
+ pasteResponse: ({data}) ->
+ # Hide the HUD frame again.
+ @hudUI.toggleIframeElementClasses "vimiumUIComponentVisible", "vimiumUIComponentHidden"
+ @unfocusIfFocused()
+ @pasteListener data
+
+ unfocusIfFocused: ->
+ document.activeElement.blur() if document.activeElement == @hudUI?.iframeElement
+
class Tween
opacity: 0
intervalId: -1
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee
index ca000c90..d4a95d36 100644
--- a/content_scripts/link_hints.coffee
+++ b/content_scripts/link_hints.coffee
@@ -31,7 +31,7 @@ COPY_LINK_URL =
indicator: "Copy link URL to Clipboard"
linkActivator: (link) ->
if link.href?
- chrome.runtime.sendMessage handler: "copyToClipboard", data: link.href
+ HUD.copyToClipboard link.href
url = link.href
url = url[0..25] + "...." if 28 < url.length
HUD.showForDuration "Yanked #{url}", 2000
diff --git a/content_scripts/mode_normal.coffee b/content_scripts/mode_normal.coffee
index 027ad22f..e91feecf 100644
--- a/content_scripts/mode_normal.coffee
+++ b/content_scripts/mode_normal.coffee
@@ -91,10 +91,19 @@ NormalModeCommands =
copyCurrentUrl: ->
chrome.runtime.sendMessage { handler: "getCurrentTabUrl" }, (url) ->
- chrome.runtime.sendMessage { handler: "copyToClipboard", data: url }
+ HUD.copyToClipboard url
url = url[0..25] + "...." if 28 < url.length
HUD.showForDuration("Yanked #{url}", 2000)
+ openCopiedUrlInNewTab: (count) ->
+ HUD.pasteFromClipboard (url) ->
+ for i in [0...count] by 1
+ chrome.runtime.sendMessage { handler: "openUrlInNewTab", url }
+
+ openCopiedUrlInCurrentTab: ->
+ HUD.pasteFromClipboard (url) ->
+ chrome.runtime.sendMessage { handler: "openUrlInCurrentTab", url }
+
# Mode changes.
enterInsertMode: ->
# If a focusable element receives the focus, then we exit and leave the permanently-installed insert-mode
diff --git a/content_scripts/mode_visual.coffee b/content_scripts/mode_visual.coffee
index f99e42f9..4c6578cd 100644
--- a/content_scripts/mode_visual.coffee
+++ b/content_scripts/mode_visual.coffee
@@ -312,7 +312,7 @@ class VisualMode extends KeyHandlerMode
yank: (args = {}) ->
@yankedText = @selection.toString()
@exit()
- chrome.runtime.sendMessage handler: "copyToClipboard", data: @yankedText
+ HUD.copyToClipboard @yankedText
message = @yankedText.replace /\s+/g, " "
message = message[...12] + "..." if 15 < @yankedText.length
diff --git a/lib/clipboard.coffee b/lib/clipboard.coffee
index 1d378e76..a9e2e82e 100644
--- a/lib/clipboard.coffee
+++ b/lib/clipboard.coffee
@@ -1,8 +1,9 @@
Clipboard =
- _createTextArea: ->
- textArea = document.createElement "textarea"
+ _createTextArea: (tagName = "textarea") ->
+ textArea = document.createElement tagName
textArea.style.position = "absolute"
textArea.style.left = "-100%"
+ textArea.contentEditable = "true"
textArea
# http://groups.google.com/group/chromium-extensions/browse_thread/thread/49027e7f3b04f68/f6ab2457dee5bf55
@@ -16,11 +17,11 @@ Clipboard =
document.body.removeChild(textArea)
paste: ->
- textArea = @_createTextArea()
+ textArea = @_createTextArea "div" # Use a <div> so Firefox pastes rich text.
document.body.appendChild(textArea)
textArea.focus()
document.execCommand("Paste")
- value = textArea.value
+ value = textArea.innerText
document.body.removeChild(textArea)
value
diff --git a/manifest.json b/manifest.json
index 353582d1..7a8d8699 100644
--- a/manifest.json
+++ b/manifest.json
@@ -32,6 +32,7 @@
"bookmarks",
"history",
"clipboardRead",
+ "clipboardWrite",
"storage",
"sessions",
"notifications",
diff --git a/pages/help_dialog.coffee b/pages/help_dialog.coffee
index f36155e4..08180a72 100644
--- a/pages/help_dialog.coffee
+++ b/pages/help_dialog.coffee
@@ -83,7 +83,7 @@ HelpDialog =
commandNameElement.textContent = command.command
commandNameElement.title = "Click to copy \"#{command.command}\" to clipboard."
commandNameElement.addEventListener "click", ->
- chrome.runtime.sendMessage handler: "copyToClipboard", data: commandNameElement.textContent
+ HUD.copyToClipboard commandNameElement.textContent
HUD.showForDuration("Yanked #{commandNameElement.textContent}.", 2000)
@showAdvancedCommands(@getShowAdvancedCommands())
diff --git a/pages/hud.coffee b/pages/hud.coffee
index 0d2ec2f7..99aaa2ac 100644
--- a/pages/hud.coffee
+++ b/pages/hud.coffee
@@ -95,5 +95,19 @@ handlers =
" (No matches)"
countElement.textContent = if showMatchText then countText else ""
+ copyToClipboard: (data) ->
+ focusedElement = document.activeElement
+ Clipboard.copy data
+ focusedElement?.focus()
+ window.parent.focus()
+ UIComponentServer.postMessage {name: "unfocusIfFocused", data}
+
+ pasteFromClipboard: ->
+ focusedElement = document.activeElement
+ data = Clipboard.paste()
+ focusedElement?.focus()
+ window.parent.focus()
+ UIComponentServer.postMessage {name: "pasteResponse", data}
+
UIComponentServer.registerHandler ({data}) -> handlers[data.name ? data]? data
FindModeHistory.init()
diff --git a/pages/hud.html b/pages/hud.html
index 3e8cf976..7bd27171 100644
--- a/pages/hud.html
+++ b/pages/hud.html
@@ -7,6 +7,7 @@
<script type="text/javascript" src="../lib/settings.js"></script>
<script type="text/javascript" src="../lib/keyboard_utils.js"></script>
<script type="text/javascript" src="../lib/find_mode_history.js"></script>
+ <script type="text/javascript" src="../lib/clipboard.js"></script>
<script type="text/javascript" src="ui_component_server.js"></script>
<script type="text/javascript" src="hud.js"></script>
</head>