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