aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--background_scripts/main.js41
-rw-r--r--content_scripts/vimium_frontend.coffee93
2 files changed, 58 insertions, 76 deletions
diff --git a/background_scripts/main.js b/background_scripts/main.js
index d770bb56..41fb3441 100644
--- a/background_scripts/main.js
+++ b/background_scripts/main.js
@@ -16,13 +16,13 @@ var namedKeyRegex = /^(<(?:[amc]-.|(?:[amc]-)?[a-z0-9]{2,5})>)(.*)$/;
// Port handler mapping
var portHandlers = {
keyDown: handleKeyDown,
- getCurrentTabUrl: getCurrentTabUrl,
settings: handleSettings,
filterCompleter: filterCompleter
};
var sendRequestHandlers = {
getCompletionKeys: getCompletionKeysRequest,
+ getCurrentTabUrl: getCurrentTabUrl,
openUrlInNewTab: openUrlInNewTab,
openUrlInCurrentTab: openUrlInCurrentTab,
openOptionsPageInNewTab: openOptionsPageInNewTab,
@@ -84,15 +84,16 @@ chrome.extension.onRequest.addListener(function (request, sender, sendResponse)
var senderTabId = sender.tab ? sender.tab.id : null;
if (sendRequestHandlers[request.handler])
sendResponse(sendRequestHandlers[request.handler](request, sender));
+ // Ensure the sendResponse callback is freed.
+ return false;
});
/*
* Used by the content scripts to get their full URL. This is needed for URLs like "view-source:http:// .."
* because window.location doesn't know anything about the Chrome-specific "view-source:".
*/
-function getCurrentTabUrl(args, port) {
- var returnPort = chrome.tabs.connect(port.sender.tab.id, { name: "returnCurrentTabUrl" });
- returnPort.postMessage({ url: port.sender.tab.url });
+function getCurrentTabUrl(request, sender) {
+ return sender.tab.url;
}
/*
@@ -134,7 +135,7 @@ function saveHelpDialogSettings(request) {
function showHelp(callback, frameId) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id,
- { name: "showHelpDialog", dialogHtml: helpDialogHtml(), frameId:frameId });
+ { name: "toggleHelpDialog", dialogHtml: helpDialogHtml(), frameId:frameId });
});
}
@@ -366,8 +367,7 @@ function updateActiveState(tabId) {
// Default to disabled state in case we can't connect to Vimium, primarily for the "New Tab" page.
// TODO(philc): Re-enable once we've restyled the browser action icon.
// chrome.browserAction.setIcon({ path: disabledIcon });
- var returnPort = chrome.tabs.connect(tabId, { name: "getActiveState" });
- returnPort.onMessage.addListener(function(response) {
+ chrome.tabs.sendRequest(tabId, { name: "getActiveState" }, function(response) {
var isCurrentlyEnabled = response.enabled;
var shouldBeEnabled = isEnabledForUrl({url: tab.url}).isEnabledForUrl;
@@ -376,13 +376,12 @@ function updateActiveState(tabId) {
chrome.browserAction.setIcon({ path: enabledIcon });
} else {
chrome.browserAction.setIcon({ path: disabledIcon });
- chrome.tabs.connect(tabId, { name: "disableVimium" }).postMessage();
+ chrome.tabs.sendRequest(tabId, { name: "disableVimium" });
}
} else {
chrome.browserAction.setIcon({ path: disabledIcon });
}
});
- returnPort.postMessage();
});
}
@@ -462,10 +461,12 @@ function restoreTab(callback) {
// wait until that's over before we can call setScrollPosition.
chrome.tabs.create({ url: tabQueueEntry.url, index: tabQueueEntry.positionIndex }, function(tab) {
tabLoadedHandlers[tab.id] = function() {
- var scrollPort = chrome.tabs.connect(tab.id, {name: "setScrollPosition"});
- scrollPort.postMessage({ scrollX: tabQueueEntry.scrollX, scrollY: tabQueueEntry.scrollY });
+ var scrollPort = chrome.tabs.sendRequest(tab.id, {
+ name: "setScrollPosition",
+ scrollX: tabQueueEntry.scrollX,
+ scrollY: tabQueueEntry.scrollY
+ });
};
-
callback();
});
}
@@ -584,14 +585,14 @@ function checkKeyQueue(keysToCheck, tabId, frameId) {
registryEntry = Commands.keyToCommandRegistry[command];
if (!registryEntry.isBackgroundCommand) {
- var port = chrome.tabs.connect(tabId, { name: "executePageCommand" });
- port.postMessage({ command: registryEntry.command,
- frameId: frameId,
- count: count,
- passCountToFunction: registryEntry.passCountToFunction,
- completionKeys: generateCompletionKeys("")
- });
-
+ chrome.tabs.sendRequest(tabId, {
+ name: "executePageCommand",
+ command: registryEntry.command,
+ frameId: frameId,
+ count: count,
+ passCountToFunction: registryEntry.passCountToFunction,
+ completionKeys: generateCompletionKeys("")
+ });
refreshedCompletionKeys = true;
} else {
if(registryEntry.passCountToFunction){
diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee
index 8ba59b5c..0fc8d518 100644
--- a/content_scripts/vimium_frontend.coffee
+++ b/content_scripts/vimium_frontend.coffee
@@ -4,8 +4,6 @@
# background page that we're in domReady and ready to accept normal commands by connectiong to a port named
# "domReady".
#
-getCurrentUrlHandlers = [] # function(url)
-
insertModeLock = null
findMode = false
findModeQuery = { rawQuery: "" }
@@ -104,53 +102,23 @@ initializePreDomReady = ->
# Send the key to the key handler in the background page.
keyPort = chrome.extension.connect({ name: "keyDown" })
- chrome.extension.onRequest.addListener (request, sender, sendResponse) ->
- if (request.name == "hideUpgradeNotification")
- HUD.hideUpgradeNotification()
- else if (request.name == "showUpgradeNotification" && isEnabledForUrl)
- HUD.showUpgradeNotification(request.version)
- else if (request.name == "showHelpDialog")
- if (isShowingHelpDialog)
- hideHelpDialog()
- else
- showHelpDialog(request.dialogHtml, request.frameId)
- else if (request.name == "focusFrame")
- if (frameId == request.frameId)
- focusThisFrame(request.highlight)
- else if (request.name == "refreshCompletionKeys")
- refreshCompletionKeys(request)
- else if (request.name == "getScrollPosition")
- sendResponse
- scrollX: window.scrollX
- scrollY: window.scrollY
+ requestHandlers =
+ hideUpgradeNotification: -> HUD.hideUpgradeNotification()
+ showUpgradeNotification: -> HUD.showUpgradeNotification()
+ toggleHelpDialog: (request) -> toggleHelpDialog(request.dialogHtml, request.frameId)
+ focusFrame: (request) -> if (frameId == request.frameId) then focusThisFrame(request.highlight)
+ refreshCompletionKeys: refreshCompletionKeys
+ getScrollPosition: -> scrollX: window.scrollX, scrollY: window.scrollY
+ setScrollPosition: (request) -> setScrollPosition request.scrollX, request.scrollY
+ executePageCommand: executePageCommand
+ getActiveState: -> { enabled: isEnabledForUrl }
+ disableVimium: disableVimium
+ chrome.extension.onRequest.addListener (request, sender, sendResponse) ->
+ sendResponse requestHandlers[request.name](request, sender)
# Ensure the sendResponse callback is freed.
false
- chrome.extension.onConnect.addListener (port, name) ->
- if (port.name == "executePageCommand")
- port.onMessage.addListener (args) ->
- if (frameId == args.frameId)
- if (args.passCountToFunction)
- Utils.invokeCommandString(args.command, [args.count])
- else
- Utils.invokeCommandString(args.command) for i in [0...args.count]
-
- refreshCompletionKeys(args)
- else if (port.name == "setScrollPosition")
- port.onMessage.addListener (args) ->
- if (args.scrollX > 0 || args.scrollY > 0)
- DomUtils.documentReady(-> window.scrollBy(args.scrollX, args.scrollY))
- else if (port.name == "returnCurrentTabUrl")
- port.onMessage.addListener (args) ->
- getCurrentUrlHandlers.pop()(args.url) if (getCurrentUrlHandlers.length > 0)
- else if (port.name == "refreshCompletionKeys")
- port.onMessage.addListener (args) -> refreshCompletionKeys(args.completionKeys)
- else if (port.name == "getActiveState")
- port.onMessage.addListener (args) -> port.postMessage({ enabled: isEnabledForUrl })
- else if (port.name == "disableVimium")
- port.onMessage.addListener (args) -> disableVimium()
-
#
# This is called once the background page has told us that Vimium should be enabled for the current URL.
#
@@ -216,6 +184,16 @@ enterInsertModeIfElementIsFocused = ->
onDOMActivate = (event) -> activatedElement = event.target
+executePageCommand = (request) ->
+ return unless frameId == request.frameId
+
+ if (request.passCountToFunction)
+ Utils.invokeCommandString(request.command, [request.count])
+ else
+ Utils.invokeCommandString(request.command) for i in [0...request.count]
+
+ refreshCompletionKeys(request)
+
#
# activatedElement is different from document.activeElement -- the latter seems to be reserved mostly for
# input elements. This mechanism allows us to decide whether to scroll a div or to scroll the whole document.
@@ -260,6 +238,10 @@ scrollActivatedElementBy = (direction, amount) ->
if (rect.bottom < 0 || rect.top > window.innerHeight || rect.right < 0 || rect.left > window.innerWidth)
activatedElement = lastElement
+setScrollPosition = (scrollX, scrollY) ->
+ if (scrollX > 0 || scrollY > 0)
+ DomUtils.documentReady(-> window.scrollBy(scrollX, scrollY))
+
#
# Called from the backend in order to change frame focus.
#
@@ -302,26 +284,19 @@ extend window,
window.location.href = urlsplit.join('/')
toggleViewSource: ->
- toggleViewSourceCallback = (url) ->
+ chrome.extension.sendRequest { handler: "getCurrentTabUrl" }, (url) ->
if (url.substr(0, 12) == "view-source:")
url = url.substr(12, url.length - 12)
else
url = "view-source:" + url
chrome.extension.sendRequest({ handler: "openUrlInNewTab", url: url, selected: true })
- getCurrentUrlHandlers.push(toggleViewSourceCallback)
- getCurrentUrlPort = chrome.extension.connect({ name: "getCurrentTabUrl" })
- getCurrentUrlPort.postMessage({})
copyCurrentUrl: ->
# TODO(ilya): When the following bug is fixed, revisit this approach of sending back to the background page
# to copy.
# http://code.google.com/p/chromium/issues/detail?id=55188
- # getCurrentUrlHandlers.push(function (url) { Clipboard.copy(url); })
- getCurrentUrlHandlers.push((url) -> chrome.extension.sendRequest({ handler: "copyToClipboard", data: url }))
-
- # TODO(ilya): Convert to sendRequest.
- getCurrentUrlPort = chrome.extension.connect({ name: "getCurrentTabUrl" })
- getCurrentUrlPort.postMessage({})
+ chrome.extension.sendRequest { handler: "getCurrentTabUrl" }, (url) ->
+ chrome.extension.sendRequest { handler: "copyToClipboard", data: url }
HUD.showForDuration("Yanked URL", 1000)
@@ -863,7 +838,7 @@ exitFindMode = ->
findMode = false
HUD.hide()
-window.showHelpDialog = (html, fid) ->
+showHelpDialog = (html, fid) ->
return if (isShowingHelpDialog || !document.body || fid != frameId)
isShowingHelpDialog = true
container = document.createElement("div")
@@ -890,6 +865,12 @@ hideHelpDialog = (clickEvent) ->
if (clickEvent)
clickEvent.preventDefault()
+toggleHelpDialog = (html, fid) ->
+ if (isShowingHelpDialog)
+ hideHelpDialog()
+ else
+ showHelpDialog(html, fid)
+
#
# A heads-up-display (HUD) for showing Vimium page operations.
# Note: you cannot interact with the HUD until document.body is available.