Skip to content

Commit

Permalink
added auto-paste functionality to keyboard shortcuts #98
Browse files Browse the repository at this point in the history
  • Loading branch information
neocotic committed May 22, 2012
1 parent 441f722 commit 4ff233b
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 195 deletions.
56 changes: 36 additions & 20 deletions src/_locales/en/messages.json
Expand Up @@ -1662,7 +1662,7 @@
}
},
"opt_guide_options_menu_paste_text": {
"message": "Whether or not the Template output is automatically pasted into a contextual (right-clicked) field",
"message": "Whether or not the Template output is automatically pasted into the field where the context (right-click) menu was activated",
"description": "Description of the menuPaste variable on the Guide tab of the Options page under the Options section."
},
"opt_guide_options_notification_duration_text": {
Expand All @@ -1673,6 +1673,10 @@
"message": "Whether or not the option to show desktop notifications is enabled",
"description": "Description of the notifications variable on the Guide tab of the Options page under the Options section."
},
"opt_guide_options_shortcuts_paste_text": {
"message": "Whether or not the Template output is automatically pasted into the field where the keyboard shortcut was detected",
"description": "Description of the shortcutsPaste variable on the Guide tab of the Options page under the Options section."
},
"opt_guide_options_shortcuts_text": {
"message": "Whether or not the option to allow keyboard shortcuts is enabled",
"description": "Description of the shortcuts variable on the Guide tab of the Options page under the Options section."
Expand Down Expand Up @@ -2551,20 +2555,6 @@
"message": "Automatically Paste",
"description": "Label for the Automatically Paste field on the General tab of the Options page under the Context Menu section."
},
"opt_menu_paste_sub_text": {
"message": "This is not available on internal Chrome pages or the $O_LINK_WEBSTORE$Chrome Web Store$C_LINK$",
"description": "Help text for the Automatically Paste field on the General tab of the Options page under the Context Menu section.",
"placeholders": {
"c_link": {
"content": "</a>",
"example": "</a>"
},
"o_link_webstore": {
"content": "<a href=\"https://chrome.google.com/webstore\" target=\"_blank\">",
"example": "<a href=\"https://chrome.google.com/webstore\">"
}
}
},
"opt_menu_paste_text": {
"message": "Paste the Template result into the right-clicked editable field",
"description": "Text for the Automatically Paste field on the General tab of the Options page under the Context Menu section."
Expand Down Expand Up @@ -2611,13 +2601,39 @@
"message": "Automatically hide desktop notifications after this time",
"description": "Text for the Notification Duration field on the General tab of the Options page under the Desktop Notifications section."
},
"opt_shortcut_label": {
"opt_protected_sub_text": {
"message": "This is not available on internal Chrome pages or the $O_LINK_WEBSTORE$Chrome Web Store$C_LINK$",
"description": "Help text for fields on the Options page whose functionality does not work on protected pages.",
"placeholders": {
"c_link": {
"content": "</a>",
"example": "</a>"
},
"o_link_webstore": {
"content": "<a href=\"https://chrome.google.com/webstore\" target=\"_blank\">",
"example": "<a href=\"https://chrome.google.com/webstore\">"
}
}
},
"opt_shortcuts_header": {
"message": "Keyboard Shortcuts",
"description": "Label for the Keyboard Shortcuts field on the General tab of the Options page under the Miscellaneous section."
"description": "Header for the Keyboard Shortcuts section on the General tab of the Options page."
},
"opt_shortcuts_enabled_label": {
"message": "Hotkeys",
"description": "Label for the Hotkeys field on the General tab of the Options page under the Keyboard Shortcuts section."
},
"opt_shortcuts_enabled_text": {
"message": "Templates are accessible using keyboard shortcuts",
"description": "Text for the Hotkeys field on the General tab of the Options page under the Keyboard Shortcuts section."
},
"opt_shortcuts_paste_label": {
"message": "Automatically Paste",
"description": "Label for the Automatically Paste field on the General tab of the Options page under the Keyboard Shortcuts section."
},
"opt_shortcut_text": {
"message": "Keyboard shortcuts can be used to access templates",
"description": "Text for the Keyboard Shortcuts field on the General tab of the Options page under the Miscellaneous section."
"opt_shortcuts_paste_text": {
"message": "Paste the Template result into the editable field where the hotkey was used",
"description": "Text for the Automatically Paste field on the General tab of the Options page under the Keyboard Shortcuts section."
},
"opt_template_content_label": {
"message": "Content",
Expand Down
62 changes: 50 additions & 12 deletions src/lib/background.coffee
Expand Up @@ -357,7 +357,7 @@ nullIfEmpty = (object) ->
onRequest = (request, sender, sendResponse) ->
log.trace()
# Don't allow shortcut requests when shortcuts are disabled.
if request.type is 'shortcut' and not store.get 'shortcuts'
if request.type is 'shortcut' and not store.get 'shortcuts.enabled'
return sendResponse?()
# Select or create a tab for the Options page.
if request.type is 'options'
Expand All @@ -374,6 +374,7 @@ onRequest = (request, sender, sendResponse) ->
id = utils.keyGen '', null, 't', no
link = no
output = null
shortcut = no
shortenMap = {}
tab = null
template = null
Expand Down Expand Up @@ -428,6 +429,7 @@ onRequest = (request, sender, sendResponse) ->
template = getTemplateWithKey request.data.key
data = buildStandardData tab, shortCallback if template?
when 'shortcut'
shortcut = yes
template = getTemplateWithShortcut request.data.key
data = buildStandardData tab, shortCallback if template?
updateProgress 20
Expand All @@ -442,7 +444,7 @@ onRequest = (request, sender, sendResponse) ->
'result_bad_error_description'
success: no
runner.pushPacked null, addAdditionalData, ->
[tab, data, id, editable, link, ->
[tab, data, id, editable, shortcut, link, ->
updateProgress 40
# Ensure all properties of `data` are lower case.
transformData data
Expand Down Expand Up @@ -494,7 +496,7 @@ onRequest = (request, sender, sendResponse) ->
template: template
runner.run (result) ->
type = request.type[0].toUpperCase() + request.type.substr 1
value = request.data.key.charCodeAt 0 if request.type is 'shortcut'
value = request.data.key.charCodeAt 0 if shortcut
analytics.track 'Requests', 'Processed', type, value
updateProgress 100
if result?
Expand All @@ -510,7 +512,9 @@ onRequest = (request, sender, sendResponse) ->
ext.notification.description = result.message ?
i18n.get 'result_good_description', result.template.title
ext.copy result.contents
if editable and store.get('menu.paste') and not isProtectedPage tab
if not isProtectedPage(tab) and
(editable and store.get 'menu.paste') or
(shortcut and store.get 'shortcuts.paste')
chrome.tabs.sendRequest tab.id,
contents: result.contents, id: id, type: 'paste'
sendResponse? contents: result.contents
Expand Down Expand Up @@ -576,6 +580,29 @@ showNotification = ->
ext.reset()
updateProgress null, off

# Update hotkeys stored by the `content.coffee` script within all of the tabs
# (where valid) of each Chrome window.
updateHotkeys = ->
log.trace()
# Build list of shortcuts used by enabled templates.
hotkeys = (
template.shortcut for template in ext.templates when template.enabled
)
# Create a runner to help manage the asynchronous aspect.
runner = new utils.Runner()
runner.push chrome.windows, 'getAll', null, (windows) ->
log.info 'Retrieved the following windows...', windows
for win in windows
do (win) -> runner.push chrome.tabs, 'query', windowId: win.id, (tabs) ->
log.info 'Retrieved the following tabs...', tabs
# Check tabs are not displaying a *protected* page (i.e. one that
# will cause an error if an attempt is made to send a request to it).
for tab in tabs when not isProtectedPage tab
chrome.tabs.sendRequest tab.id, hotkeys: hotkeys
runner.next()
runner.next()
runner.run()

# Update the popup UI state to reflect the progress of the current request.
# Ignore `percent` if `toggle` is specified; otherwise update the percentage on
# the progress bar and reset the delay timer.
Expand Down Expand Up @@ -648,7 +675,7 @@ updateUrlShortenerUsage = (name, oauth) ->
# --------------

# Extract additional information from `tab` and add it to `data`.
addAdditionalData = (tab, data, id, editable, link, callback) ->
addAdditionalData = (tab, data, id, editable, shortcut, link, callback) ->
log.trace()
windowId = chrome.windows.WINDOW_ID_CURRENT
# Create a runner to simplify this process.
Expand Down Expand Up @@ -693,10 +720,11 @@ addAdditionalData = (tab, data, id, editable, link, callback) ->
# executed.
if isProtectedPage tab then runner.finish() else runner.next()
runner.push chrome.tabs, 'sendRequest', tab.id,
editable: editable, id: id, link: link, url: data.url, (response) ->
log.debug 'Retrieved the following data from the content script...',
response
runner.finish response
editable: editable, id: id, link: link, shortcut: shortcut, url: data.url,
(response) ->
log.debug 'Retrieved the following data from the content script...',
response
runner.finish response
runner.run (result = {}) ->
lastModified = if result.lastModified?
time = Date.parse result.lastModified
Expand Down Expand Up @@ -772,6 +800,7 @@ buildStandardData = (tab, shortCallback) ->
googl = store.get 'googl'
menu = store.get 'menu'
notifications = store.get 'notifications'
shortcuts = store.get 'shortcuts'
stats = store.get 'stats'
toolbar = store.get 'toolbar'
yourls = store.get 'yourls'
Expand Down Expand Up @@ -862,7 +891,8 @@ buildStandardData = (tab, shortCallback) ->
selectionmarkdown: -> md @selectionhtml
# Deprecated since 1.0.0, use `shorten` instead.
short: -> @shorten()
shortcuts: store.get 'shortcuts'
shortcuts: shortcuts.enabled
shortcutspaste: shortcuts.paste
shorten: -> shortCallback
tidy: -> (text, render) ->
render(text).replace(/([ \t]+)/g, ' ').trim()
Expand Down Expand Up @@ -960,7 +990,7 @@ buildTemplate = (template) ->
menu.append $ '<span/>',
class: 'text'
text: template.title
if store.get 'shortcuts'
if store.get 'shortcuts.enabled'
modifiers = ext.SHORTCUT_MODIFIERS
modifiers = ext.SHORTCUT_MAC_MODIFIERS if ext.isThisPlatform 'mac'
menu.append $ '<span/>',
Expand Down Expand Up @@ -1013,6 +1043,10 @@ init_update = ->
duration: store.get('notificationDuration') ? 3000
enabled: store.get('notifications') ? yes
store.remove 'notificationDuration'
updater.update '1.1.0', ->
log.info 'Updating general settings for 1.1.0'
store.set 'shortcuts',
enabled: store.get('shortcuts') ? yes

# Initialize the settings related to statistical information.
initStatistics = ->
Expand Down Expand Up @@ -1426,6 +1460,7 @@ ext = window.ext = new class Extension extends utils.Class
anchor: {}
menu: {}
notifications: {}
shortcuts: {}
stats: {}
templates: []
toolbar: {}
Expand All @@ -1440,7 +1475,9 @@ ext = window.ext = new class Extension extends utils.Class
store.modify 'notifications', (notifications) ->
notifications.duration ?= 3000
notifications.enabled ?= yes
store.init 'shortcuts', on
store.modify 'shortcuts', (shortcuts) ->
shortcuts.enabled ?= yes
shortcuts.paste ?= no
initTemplates()
initToolbar()
initStatistics()
Expand Down Expand Up @@ -1563,6 +1600,7 @@ ext = window.ext = new class Extension extends utils.Class
buildPopup()
@updateContextMenu()
updateStatistics()
updateHotkeys()

# Update the toolbar/browser action depending on the current settings.
updateToolbar: ->
Expand Down

0 comments on commit 4ff233b

Please sign in to comment.