From b05276ed8264e5a71f20a7068690ba2a414ee6d8 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 7 Mar 2015 16:49:32 +0000 Subject: Reload content scripts after install/update. This is @mrmr1993's work from #1041. Reload content scripts when vimium is installed or updates. (@mrmr1993: The automatic merge was really messy (or, at least, I couldn't figure out what was going on). Since the bulk of #1041 was actually quite compact, I took the liberty of just copying it in. Hope you don't mind.) --- background_scripts/main.coffee | 19 +++++++++++++++++++ content_scripts/vimium_frontend.coffee | 3 ++- tests/dom_tests/dom_tests.coffee | 4 ++-- tests/unit_tests/test_chrome_stubs.coffee | 2 ++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 23950e6e..7b360efd 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -2,6 +2,25 @@ root = exports ? window currentVersion = Utils.getCurrentVersion() +# Iterate over existing tabs and inject the necessary javascript/css into each. +injectContentScriptsIntoOpenTabs = -> + manifest = chrome.runtime.getManifest() + # Content scripts loaded on every page should be in the same group. We assume it is the first. + contentScripts = manifest.content_scripts[0] + chrome.tabs.query { status: "complete" }, (tabs) -> + jobs = [ [ chrome.tabs.executeScript, contentScripts.js ], [ chrome.tabs.insertCSS, contentScripts.css ] ] + # Chrome complains if we don't evaluate chrome.runtime.lastError on errors (and we get errors for tabs on + # which Vimium cannot run). + checkLastRuntimeError = -> chrome.runtime.lastError + for tab in tabs + for [ func, files ] in jobs + for file in files + func tab.id, { file: file, allFrames: contentScripts.allFrames }, checkLastRuntimeError + +# The browser may have tabs already open. We inject the content scripts immediately so that they work straight +# away. +chrome.runtime.onInstalled.addListener injectContentScriptsIntoOpenTabs + tabQueue = {} # windowId -> Array tabInfoMap = {} # tabId -> object with various tab properties keyQueue = "" # Queue of keys typed diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index b2d9f735..66922084 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -210,6 +210,7 @@ window.initializeWhenEnabled = -> for type in [ "keydown", "keypress", "keyup", "click", "focus", "blur", "mousedown" ] do (type) -> installListener window, type, (event) -> handlerStack.bubbleEvent type, event installListener document, "DOMActivate", (event) -> handlerStack.bubbleEvent 'DOMActivate', event + installListener document, "focus", detectFocus installedListeners = true FindModeHistory.init() @@ -229,7 +230,7 @@ getActiveState = -> # # The backend needs to know which frame has focus. # -window.addEventListener "focus", -> +detectFocus = -> # settings may have changed since the frame last had focus settings.load() chrome.runtime.sendMessage({ handler: "frameFocused", frameId: frameId }) diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index 600616a9..ff95d3f4 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -542,7 +542,7 @@ context "Mode badges", should "have an I badge in insert mode by focus", -> document.getElementById("first").focus() - assert.isTrue chromeMessages[0].badge == "I" + assert.isTrue chromeMessages[1].badge == "I" should "have no badge after leaving insert mode by focus", -> document.getElementById("first").focus() @@ -575,5 +575,5 @@ context "Mode badges", passKeys: "" document.getElementById("first").focus() - assert.isTrue chromeMessages[0].badge == "" + assert.isTrue chromeMessages[1].badge == "" diff --git a/tests/unit_tests/test_chrome_stubs.coffee b/tests/unit_tests/test_chrome_stubs.coffee index c61d7246..bc50521a 100644 --- a/tests/unit_tests/test_chrome_stubs.coffee +++ b/tests/unit_tests/test_chrome_stubs.coffee @@ -16,6 +16,8 @@ exports.chrome = addListener: () -> true onMessage: addListener: () -> true + onInstalled: + addListener: -> tabs: onSelectionChanged: -- cgit v1.2.3 From d3cd1fd3d0f8b2cc4249ad8fdbd76bbb94a82997 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 8 Mar 2015 10:43:53 +0000 Subject: Reload content scripts: minor refactoring. --- background_scripts/main.coffee | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 7b360efd..4ab88019 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -1,26 +1,22 @@ root = exports ? window -currentVersion = Utils.getCurrentVersion() - -# Iterate over existing tabs and inject the necessary javascript/css into each. -injectContentScriptsIntoOpenTabs = -> +# The browser may have tabs already open. We inject the content scripts immediately so that they work straight +# away. +chrome.runtime.onInstalled.addListener -> manifest = chrome.runtime.getManifest() # Content scripts loaded on every page should be in the same group. We assume it is the first. contentScripts = manifest.content_scripts[0] + jobs = [ [ chrome.tabs.executeScript, contentScripts.js ], [ chrome.tabs.insertCSS, contentScripts.css ] ] + # Chrome complains if we don't evaluate chrome.runtime.lastError on errors (and we get errors for tabs on + # which Vimium cannot run). + checkLastRuntimeError = -> chrome.runtime.lastError chrome.tabs.query { status: "complete" }, (tabs) -> - jobs = [ [ chrome.tabs.executeScript, contentScripts.js ], [ chrome.tabs.insertCSS, contentScripts.css ] ] - # Chrome complains if we don't evaluate chrome.runtime.lastError on errors (and we get errors for tabs on - # which Vimium cannot run). - checkLastRuntimeError = -> chrome.runtime.lastError for tab in tabs for [ func, files ] in jobs for file in files func tab.id, { file: file, allFrames: contentScripts.allFrames }, checkLastRuntimeError -# The browser may have tabs already open. We inject the content scripts immediately so that they work straight -# away. -chrome.runtime.onInstalled.addListener injectContentScriptsIntoOpenTabs - +currentVersion = Utils.getCurrentVersion() tabQueue = {} # windowId -> Array tabInfoMap = {} # tabId -> object with various tab properties keyQueue = "" # Queue of keys typed -- cgit v1.2.3 From b15d5231e1660aff220885a3feab62eaa5b9861d Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 8 Mar 2015 10:46:19 +0000 Subject: Reload content scripts: document why changes are needed in tests. --- tests/dom_tests/dom_tests.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index ff95d3f4..4afa9d7d 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -542,6 +542,7 @@ context "Mode badges", should "have an I badge in insert mode by focus", -> document.getElementById("first").focus() + # Focus triggers an event in the handler stack, so we check element "1", here. assert.isTrue chromeMessages[1].badge == "I" should "have no badge after leaving insert mode by focus", -> @@ -575,5 +576,6 @@ context "Mode badges", passKeys: "" document.getElementById("first").focus() + # Focus triggers an event in the handler stack, so we check element "1", here. assert.isTrue chromeMessages[1].badge == "" -- cgit v1.2.3