From aba697f2f09cde4c9e5e56820adcbcfdcd1d18f0 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 13 May 2018 19:00:02 -0400 Subject: [PATCH] fix https://github.com/uBlockOrigin/uMatrix-issues/issues/8 --- src/js/hosts-files.js | 24 ++++++++++------- src/js/storage.js | 62 ++++++++++++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/js/hosts-files.js b/src/js/hosts-files.js index a087490..5c930e2 100644 --- a/src/js/hosts-files.js +++ b/src/js/hosts-files.js @@ -48,6 +48,9 @@ vAPI.messaging.addListener(function onMessage(msg) { case 'loadHostsFilesCompleted': renderHostsFiles(); break; + case 'loadRecipeFilesCompleted': + renderHostsFiles(); + break; default: break; } @@ -70,9 +73,7 @@ var renderHostsFiles = function(soft) { // Assemble a pretty list name if possible var listNameFromListKey = function(collection, listKey) { let list = collection.get(listKey); - let listTitle = list ? list.title : ''; - if ( listTitle === '' ) { return listKey; } - return listTitle; + return list && list.title || listKey; }; var liFromListEntry = function(collection, listKey, li) { @@ -132,13 +133,22 @@ var renderHostsFiles = function(soft) { return li; }; var onRenderAssetFiles = function(collection, listSelector) { + // Incremental rendering: this will allow us to easily discard unused + // DOM list entries. + uDom(listSelector + ' .listEntry:not(.notAnAsset)').addClass('discard'); + var assetKeys = Array.from(collection.keys()); // Sort works this way: // - Send /^https?:/ items at the end (custom hosts file URL) assetKeys.sort(function(a, b) { - var ta = collection.get(a).title || a, - tb = collection.get(b).title || b; + let ea = collection.get(a), + eb = collection.get(b); + if ( ea.submitter !== eb.submitter ) { + return ea.submitter !== 'user' ? -1 : 1; + } + let ta = ea.title || a, + tb = eb.title || b; if ( reExternalHostFile.test(ta) === reExternalHostFile.test(tb) ) { return ta.localeCompare(tb); } @@ -178,10 +188,6 @@ var renderHostsFiles = function(soft) { listDetails.contributor === true ); - // Incremental rendering: this will allow us to easily discard unused - // DOM list entries. - uDom('#hosts .listEntry:not(.notAnAsset)').addClass('discard'); - onRenderAssetFiles(details.hosts, '#hosts'); onRenderAssetFiles(details.recipes, '#recipes'); diff --git a/src/js/storage.js b/src/js/storage.js index b639ead..20ae6e1 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -273,33 +273,63 @@ } let µm = this, - countdownCount = µm.userSettings.selectedRecipeFiles.length; + countdownCount = 0; if ( reset ) { µm.recipeManager.reset(); } - var onLoaded = function(details) { + let recipeMetadata; + + let onDone = function() { + vAPI.messaging.broadcast({ what: 'loadRecipeFilesCompleted' }); + µm.getBytesInUse(); + callback(); + }; + + let onLoaded = function(details) { if ( details.content ) { + let entry = recipeMetadata.get(details.assetKey); + if ( entry.submitter === 'user' ) { + let match = /^! +Title: *(.+)$/im.exec( + details.content.slice(2048) + ); + if ( match !== null && match[1] !== entry.title ) { + µm.assets.registerAssetSource( + details.assetKey, + { title: match[1] } + ); + } + } µm.recipeManager.fromString(details.content); } countdownCount -= 1; if ( countdownCount === 0 ) { - callback(); + onDone(); } }; - for ( let assetKey of µm.userSettings.selectedRecipeFiles ) { - this.assets.get(assetKey, onLoaded); - } - - let userRecipes = µm.userSettings.userRecipes; - if ( userRecipes.enabled ) { - µm.recipeManager.fromString( - '! uMatrix: Ruleset recipes 1.0\n' + userRecipes.content - ); - } + let onMetadataReady = function(metadata) { + recipeMetadata = metadata; + for ( let entry of metadata ) { + let assetKey = entry[0]; + let recipeFile = entry[1]; + if ( recipeFile.selected !== true ) { continue; } + µm.assets.get(assetKey, onLoaded); + countdownCount += 1; + } + let userRecipes = µm.userSettings.userRecipes; + if ( userRecipes.enabled ) { + µm.recipeManager.fromString( + '! uMatrix: Ruleset recipes 1.0\n' + userRecipes.content + ); + } + if ( countdownCount === 0 ) { + onDone(); + } + }; + this.getAvailableRecipeFiles(onMetadataReady); }; /******************************************************************************/ @@ -406,8 +436,7 @@ type: 'recipes', contentURL: assetKey, external: true, - submitter: 'user', - title: assetKey + submitter: 'user' }; this.assets.registerAssetSource(assetKey, entry); availableRecipeFiles.set(assetKey, entry); @@ -717,9 +746,6 @@ 'externalRecipeFiles', 'userRecipes' ); - if ( recipesChanged ) { - µm.recipeManager.reset(); - } if ( typeof callback === 'function' ) { callback({ hostsChanged: hostsChanged,