From c631966bc0019614cdd6a2bcf08761cdf0e5ab3b Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Fri, 13 May 2016 08:05:03 +0200 Subject: [PATCH] Fix frame scripts being loaded more than once sometimes 1. Open several tabs. 2. Set Firefox to remember your tabs between sessions. 3. Restart Firefox. All but the current tab will now be in a "pending" (unloaded) state. 4. Update/re-install VimFx. 5. Visit one of the pending tabs. Now, the frame script will be loaded _twice_ in that tab, which for example causes `f` to produce double-clicks. This is especially noticeable during development, when you update/re-install VimFx all the time. The reason is that Firefox does not seem to load frame scripts in pending tabs immediately. Instead, the URI to the frame script we asked to load is remembered and then loaded when you actually visit the pending tab in question. If you update VimFx before doing so, yet one frame script URI will be pushed to that stack, causing _two_ instances of it to be loaded when visiting the tab. Both of those will be from the new VimFx version; the URIs are the same, and the file at that URI has now been updated. This commit attempts to solve this problem by generating a `bootstrap-frame-$BUILD_TIME.js` file (which simply runs `bootstrap.coffee`) and loading that as a frame script. This means that old saved frame script URIs will point to non-existing files and thus be harmless. --- extension/bootstrap-frame.js.tmpl | 5 +++++ extension/bootstrap.coffee | 2 +- extension/lib/main.coffee | 8 +++++++- gulpfile.coffee | 16 +++++++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 extension/bootstrap-frame.js.tmpl diff --git a/extension/bootstrap-frame.js.tmpl b/extension/bootstrap-frame.js.tmpl new file mode 100644 index 0000000..8201ce0 --- /dev/null +++ b/extension/bootstrap-frame.js.tmpl @@ -0,0 +1,5 @@ +Services.scriptloader.loadSubScript( + Services.io.newURI('{{{ ADDON_PATH }}}/content/bootstrap.js', null, null).spec, + this, + 'UTF-8' +) diff --git a/extension/bootstrap.coffee b/extension/bootstrap.coffee index b19a6b7..c4c2747 100644 --- a/extension/bootstrap.coffee +++ b/extension/bootstrap.coffee @@ -31,7 +31,7 @@ do (global = this) -> {classes: Cc, interfaces: Ci, utils: Cu} = Components - ADDON_PATH = 'chrome://vimfx' + ADDON_PATH = do -> # @echo ADDON_PATH IS_FRAME_SCRIPT = (typeof content != 'undefined') BUILD_TIME = do -> # @echo BUILD_TIME REQUIRE_DATA = do -> # @echo REQUIRE_DATA diff --git a/extension/lib/main.coffee b/extension/lib/main.coffee index 13de516..5744c40 100644 --- a/extension/lib/main.coffee +++ b/extension/lib/main.coffee @@ -123,7 +123,13 @@ module.exports = (data, reason) -> callback(true) ) - messageManager.load("#{ADDON_PATH}/content/bootstrap.js") + # For tabs not visited yet since a session restore (“pending” tabs), Firefox + # seems to not load the frame script immediately, but instead remember the URI + # and load it when the user eventually visits that tab. If VimFx is updated + # during that time this means that the below URI is saved several times, and + # will be loaded that many times. Therefore the URI is changed with each + # build, causing remembered URIs to point to non-existent files. + messageManager.load("#{ADDON_PATH}/content/bootstrap-frame-#{BUILD_TIME}.js") # @if TESTS test(vimfx) diff --git a/gulpfile.coffee b/gulpfile.coffee index 3f70832..89c7e91 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -45,6 +45,9 @@ TEST = 'extension/test' BASE_LOCALE = 'en-US' UPDATE_ALL = /\s*UPDATE_ALL$/ +ADDON_PATH = 'chrome://vimfx' +BUILD_TIME = Date.now() + argv = process.argv.slice(2) {join} = path @@ -79,7 +82,8 @@ gulp.task('coffee', -> ].concat(if test then 'extension/test/**/*.coffee' else []), {base: 'extension'}) .pipe(preprocess({context: { - BUILD_TIME: Date.now() + BUILD_TIME + ADDON_PATH: JSON.stringify(ADDON_PATH) REQUIRE_DATA: JSON.stringify(precompute('.'), null, 2) TESTS: if test @@ -94,6 +98,15 @@ gulp.task('coffee', -> .pipe(gulp.dest(DEST)) ) +gulp.task('bootstrap-frame.js', -> + gulp.src('extension/bootstrap-frame.js.tmpl') + .pipe(mustache({ADDON_PATH})) + .pipe(tap((file) -> + file.path = file.path.replace(/\.js\.tmpl$/, "-#{BUILD_TIME}.js") + )) + .pipe(gulp.dest(DEST)) +) + gulp.task('chrome.manifest', -> gulp.src('extension/chrome.manifest.tmpl') .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})})) @@ -125,6 +138,7 @@ gulp.task('install.rdf', -> ) gulp.task('templates', [ + 'bootstrap-frame.js' 'chrome.manifest' 'install.rdf' ]) -- 2.39.3