From f815223f27ac9678f17a84ed9f1fd467079357a7 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 30 Apr 2015 10:05:00 -0400 Subject: [PATCH] https://github.com/gorhill/uBlock/issues/150 needs to be fixed here too --- src/js/uritools.js | 7 ++++++ src/lib/publicsuffixlist.js | 45 ++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/js/uritools.js b/src/js/uritools.js index 35f94bc..2b48815 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -366,11 +366,18 @@ var domainCachePrune = function() { } }; +var domainCacheReset = function() { + domainCache = {}; + domainCacheCount = 0; +}; + var domainCache = {}; var domainCacheCount = 0; var domainCacheCountLowWaterMark = 75; var domainCacheCountHighWaterMark = 100; +psl.onChanged.addListener(domainCacheReset); + /******************************************************************************/ URI.domainFromURI = function(uri) { diff --git a/src/lib/publicsuffixlist.js b/src/lib/publicsuffixlist.js index fa18037..a98fbf8 100644 --- a/src/lib/publicsuffixlist.js +++ b/src/lib/publicsuffixlist.js @@ -33,6 +33,8 @@ ;(function(root) { +'use strict'; + /******************************************************************************/ var exceptions = {}; @@ -45,6 +47,8 @@ var selfieMagic = 'iscjsfsaolnm'; var cutoffLength = 256; var mustPunycode = /[^a-z0-9.-]/; +var onChangedListeners = []; + /******************************************************************************/ // In the context of this code, a domain is defined as: @@ -234,6 +238,8 @@ function parse(text, toAscii) { } crystallize(exceptions); crystallize(rules); + + callListeners(onChangedListeners); } /******************************************************************************/ @@ -302,11 +308,47 @@ function fromSelfie(selfie) { } rules = selfie.rules; exceptions = selfie.exceptions; + callListeners(onChangedListeners); 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; @@ -317,7 +359,8 @@ root.publicSuffixList = { 'getDomain': getDomain, 'getPublicSuffix': getPublicSuffix, 'toSelfie': toSelfie, - 'fromSelfie': fromSelfie + 'fromSelfie': fromSelfie, + 'onChanged': onChanged }; /******************************************************************************/