From e428f6c331c346cadc3b598c41218c87d84427d1 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 14 Apr 2018 07:09:23 -0400 Subject: [PATCH] fix #995, #945; other minor fixes; opportunistic code review --- src/js/assets.js | 6 ++++- src/js/hosts-files.js | 3 ++- src/js/traffic.js | 10 ++++++- src/js/uritools.js | 19 +++++++------ src/lib/publicsuffixlist.js | 53 +++++-------------------------------- 5 files changed, 33 insertions(+), 58 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index 2808488c..d3cfd3ea 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -101,7 +101,11 @@ api.fetchText = function(url, onLoad, onError) { var onErrorReceived = function() { this.onload = this.onerror = this.ontimeout = null; - µMatrix.logger.writeOne('', 'error', errorCantConnectTo.replace('{{msg}}', actualUrl)); + µMatrix.logger.writeOne( + '', + 'error', + errorCantConnectTo.replace('{{url}}', actualUrl) + ); onError.call(null, { url: url, content: '' }); }; diff --git a/src/js/hosts-files.js b/src/js/hosts-files.js index 960de026..a0874908 100644 --- a/src/js/hosts-files.js +++ b/src/js/hosts-files.js @@ -1,6 +1,6 @@ /******************************************************************************* - uMatrix - a Chromium browser extension to black/white list requests. + uMatrix - a browser extension to black/white list requests. Copyright (C) 2014-2018 Raymond Hill This program is free software: you can redistribute it and/or modify @@ -43,6 +43,7 @@ vAPI.messaging.addListener(function onMessage(msg) { break; case 'assetsUpdated': document.body.classList.remove('updating'); + renderWidgets(); break; case 'loadHostsFilesCompleted': renderHostsFiles(); diff --git a/src/js/traffic.js b/src/js/traffic.js index 7bef0339..18a9bcf5 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -127,7 +127,15 @@ var onBeforeRequestHandler = function(details) { rootHostname = tabContext.rootHostname, specificity = 0; - if ( tabId < 0 && details.documentUrl !== undefined ) { + // https://github.com/gorhill/uMatrix/issues/995 + // For now we will not reclassify behind-the-scene contexts which are not + // network-based URIs. Once the logger is able to provide context + // information, the reclassification will be allowed. + if ( + tabId < 0 && + details.documentUrl !== undefined && + µmuri.isNetworkURI(details.documentUrl) + ) { tabId = µm.tabContextManager.tabIdFromURL(details.documentUrl); rootHostname = µmuri.hostnameFromURI( µm.normalizePageURL(0, details.documentUrl) diff --git a/src/js/uritools.js b/src/js/uritools.js index 92916100..2e45f7f2 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -1,7 +1,7 @@ /******************************************************************************* - uMatrix - a Chromium browser extension to black/white list requests. - Copyright (C) 2014-2017 Raymond Hill + uMatrix - a browser extension to black/white list requests. + Copyright (C) 2014-2018 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -246,11 +246,16 @@ URI.schemeFromURI = function(uri) { /******************************************************************************/ +const reNetworkScheme = /^(?:https?|wss?|ftps?)\b/; +const reNetworkURI = /^(?:ftps?|https?|wss?):\/\//; + URI.isNetworkScheme = function(scheme) { - return this.reNetworkScheme.test(scheme); + return reNetworkScheme.test(scheme); }; -URI.reNetworkScheme = /^(?:https?|wss?|ftps?)\b/; +URI.isNetworkURI = function(uri) { + return reNetworkURI.test(uri); +}; /******************************************************************************/ @@ -403,11 +408,9 @@ var domainCachePrune = function() { } }; -var domainCacheReset = function() { +window.addEventListener('publicSuffixList', function() { domainCache.clear(); -}; - -psl.onChanged.addListener(domainCacheReset); +}); /******************************************************************************/ diff --git a/src/lib/publicsuffixlist.js b/src/lib/publicsuffixlist.js index 79605a17..530f2241 100644 --- a/src/lib/publicsuffixlist.js +++ b/src/lib/publicsuffixlist.js @@ -21,6 +21,8 @@ /*! Home: https://github.com/gorhill/publicsuffixlist.js */ +'use strict'; + /* This code is mostly dumb: I consider this to be lower-level code, thus in order to ensure efficiency, the caller is responsible for sanitizing @@ -33,8 +35,6 @@ ;(function(root) { -'use strict'; - /******************************************************************************/ var exceptions = new Map(); @@ -46,8 +46,6 @@ var rules = new Map(); var cutoffLength = 256; var mustPunycode = /[^\w.*-]/; -var onChangedListeners = []; - /******************************************************************************/ // In the context of this code, a domain is defined as: @@ -242,7 +240,7 @@ function parse(text, toAscii) { crystallize(exceptions); crystallize(rules); - callListeners(onChangedListeners); + window.dispatchEvent(new CustomEvent('publicSuffixList')); } /******************************************************************************/ @@ -315,55 +313,17 @@ let toSelfie = function() { }; let fromSelfie = function(selfie) { - if ( - selfie instanceof Object === false || - selfie.magic !== selfieMagic - ) { + if ( selfie instanceof Object === false || selfie.magic !== selfieMagic ) { return false; } rules = new Map(selfie.rules); exceptions = new Map(selfie.exceptions); - callListeners(onChangedListeners); + window.dispatchEvent(new CustomEvent('publicSuffixList')); return true; }; /******************************************************************************/ -var addListener = function(listeners, callback) { - if ( typeof callback !== 'function' ) { - return; - } - if ( listeners.indexOf(callback) === -1 ) { - listeners.push(callback); - } -}; - -var removeListener = function(listeners, callback) { - var pos = listeners.indexOf(callback); - if ( pos !== -1 ) { - listeners.splice(pos, 1); - } -}; - -var callListeners = function(listeners) { - for ( var i = 0; i < listeners.length; i++ ) { - listeners[i](); - } -}; - -/******************************************************************************/ - -var onChanged = { - addListener: function(callback) { - addListener(onChangedListeners, callback); - }, - removeListener: function(callback) { - removeListener(onChangedListeners, callback); - } -}; - -/******************************************************************************/ - // Public API root = root || window; @@ -374,8 +334,7 @@ root.publicSuffixList = { 'getDomain': getDomain, 'getPublicSuffix': getPublicSuffix, 'toSelfie': toSelfie, - 'fromSelfie': fromSelfie, - 'onChanged': onChanged + 'fromSelfie': fromSelfie }; /******************************************************************************/