From 20fa0828cbb0b71159cf0a519341d120b78c5466 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Fri, 11 Mar 2016 09:57:52 +0000 Subject: Global link hints... TODO: - fix tests --- background_scripts/main.coffee | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'background_scripts') diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 4ce8c03a..44bb4e4a 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -319,6 +319,30 @@ cycleToFrame = (frames, frameId, count = 0) -> count = (count + Math.max 0, frames.indexOf frameId) % frames.length [frames[count..]..., frames[0...count]...] +HintCoordinator = + tabState: {} + + onMessage: (request, sender) -> + if request.name of this + this[request.name] extend request, tabId: sender.tab.id + else + # The message is not for us. It's for all frames, so we bounce it there. + @sendMessage request.name, sender.tab.id, request + + sendMessage: (handler, tabId, request = {}) -> + chrome.tabs.sendMessage tabId, extend request, {name: "linkHintsMessage", handler} + + activateMode: ({tabId, frameId, modeIndex}) -> + @tabState[tabId] = {frameIds: frameIdsForTab[tabId], hints: [], modeIndex, frameId} + @sendMessage "getHints", tabId + + postHints: ({tabId, frameId, hints}) -> + @tabState[tabId].hints.push hints... + @tabState[tabId].frameIds = @tabState[tabId].frameIds.filter (fId) -> fId != frameId + if @tabState[tabId].frameIds.length == 0 + @sendMessage "activateLinkHintsMode", tabId, @tabState[tabId] + delete @tabState[tabId] # We won't be needing this any more. + # Port handler mapping portHandlers = completions: handleCompletions @@ -344,6 +368,7 @@ sendRequestHandlers = # Send a message to all frames in the current tab. sendMessageToFrames: (request, sender) -> chrome.tabs.sendMessage sender.tab.id, request.message fetchFileContents: (request, sender) -> fetchFileContents request.fileName + linkHintsMessage: HintCoordinator.onMessage.bind HintCoordinator # For debugging only. This allows content scripts to log messages to the extension's logging page. log: ({frameId, message}, sender) -> BgUtils.log "#{frameId} #{message}", sender -- cgit v1.2.3 From c8414d92886c7705dc0892cdedf3939f75336889 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 12 Mar 2016 12:00:03 +0000 Subject: Global link hints; rename message name. We cannot use "request" and "name" to describe a link-hints message. The message is then accepted (and fails) on the options page where there is no handler. So, here, use "messageType" instead of "name". --- background_scripts/main.coffee | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'background_scripts') diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 44bb4e4a..883a9afc 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -323,14 +323,14 @@ HintCoordinator = tabState: {} onMessage: (request, sender) -> - if request.name of this - this[request.name] extend request, tabId: sender.tab.id + if request.messageType of this + this[request.messageType] extend request, tabId: sender.tab.id else # The message is not for us. It's for all frames, so we bounce it there. - @sendMessage request.name, sender.tab.id, request + @sendMessage request.messageType, sender.tab.id, request - sendMessage: (handler, tabId, request = {}) -> - chrome.tabs.sendMessage tabId, extend request, {name: "linkHintsMessage", handler} + sendMessage: (messageType, tabId, request = {}) -> + chrome.tabs.sendMessage tabId, extend request, {name: "linkHintsMessage", messageType} activateMode: ({tabId, frameId, modeIndex}) -> @tabState[tabId] = {frameIds: frameIdsForTab[tabId], hints: [], modeIndex, frameId} -- cgit v1.2.3 From 1cc4ab0bd5f3a7b1292ea70e06755fef6f9c7750 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 13 Mar 2016 05:16:11 +0000 Subject: Global link hints; self code review. - Better comments in places. - Better variable and message names in some places. --- background_scripts/main.coffee | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'background_scripts') diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 883a9afc..2fe2fa99 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -322,25 +322,26 @@ cycleToFrame = (frames, frameId, count = 0) -> HintCoordinator = tabState: {} - onMessage: (request, sender) -> + onMessage: (request, {tab: {id: tabId}}) -> if request.messageType of this - this[request.messageType] extend request, tabId: sender.tab.id + this[request.messageType] tabId, request else - # The message is not for us. It's for all frames, so we bounce it there. - @sendMessage request.messageType, sender.tab.id, request + # If there's no handler here, then the message is bounced to all frames in the sender's tab. + @sendMessage request.messageType, tabId, request sendMessage: (messageType, tabId, request = {}) -> chrome.tabs.sendMessage tabId, extend request, {name: "linkHintsMessage", messageType} - activateMode: ({tabId, frameId, modeIndex}) -> - @tabState[tabId] = {frameIds: frameIdsForTab[tabId], hints: [], modeIndex, frameId} - @sendMessage "getHints", tabId + prepareToActivateMode: (tabId, {frameId: originatingFrameId, modeIndex}) -> + @tabState[tabId] = {frameIds: frameIdsForTab[tabId], hintDescriptors: [], originatingFrameId, modeIndex} + @sendMessage "getHintDescriptors", tabId - postHints: ({tabId, frameId, hints}) -> - @tabState[tabId].hints.push hints... + # Receive hint descriptors from all frames and activate link-hints mode when we have them all. + postHintDescriptors: (tabId, {frameId, hintDescriptors}) -> + @tabState[tabId].hintDescriptors.push hintDescriptors... @tabState[tabId].frameIds = @tabState[tabId].frameIds.filter (fId) -> fId != frameId if @tabState[tabId].frameIds.length == 0 - @sendMessage "activateLinkHintsMode", tabId, @tabState[tabId] + @sendMessage "activateMode", tabId, @tabState[tabId] delete @tabState[tabId] # We won't be needing this any more. # Port handler mapping -- cgit v1.2.3