From 386e8bee9c17937df4903c2c91ef06a36e9549a7 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 9 Nov 2017 12:53:05 -0500 Subject: [PATCH] fix #3210 --- assets/assets.json | 1 + src/js/storage.js | 32 +++++++++++++++++++++++++++++--- src/js/utils.js | 20 ++++++++------------ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/assets/assets.json b/assets/assets.json index c787b1882..23e6f29db 100644 --- a/assets/assets.json +++ b/assets/assets.json @@ -114,6 +114,7 @@ "group": "ads", "off": true, "title": "Adguard Mobile Filters", + "ua": "mobile", "contentURL": "https://filters.adtidy.org/extension/ublock/filters/11.txt", "supportURL": "https://github.com/AdguardTeam/AdguardFilters#adguard-filters", "instructionURL": "https://kb.adguard.com/en/general/adguard-ad-filters" diff --git a/src/js/storage.js b/src/js/storage.js index 10fb2eb28..0060c64db 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -295,7 +295,7 @@ return haystack.replace( new RegExp( '(^|\\n)' + - needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + + this.escapeRegex(needle) + '(\\n|$)', 'g'), '\n' ).trim(); @@ -439,7 +439,7 @@ selectedListKeys.push(key); continue; } - if ( this.matchCurrentLanguage(list.lang) ) { + if ( this.listMatchesEnvironment(list) ) { selectedListKeys.push(key); list.off = false; } @@ -1069,6 +1069,32 @@ /******************************************************************************/ +// https://github.com/gorhill/uBlock/issues/2344 +// Support mutliple locales per filter list. + +// https://github.com/gorhill/uBlock/issues/3210 +// Support ability to auto-enable a filter list based on user agent. + +µBlock.listMatchesEnvironment = function(details) { + var re; + // Matches language? + if ( typeof details.lang === 'string' ) { + if ( this.listMatchesEnvironment.reLang === undefined ) { + re = new RegExp('\\b' + self.navigator.language.slice(0, 2) + '\\b'); + this.listMatchesEnvironment.reLang = re; + } + if ( re.test(details.lang) ) { return true; } + } + // Matches user agent? + if ( typeof details.ua === 'string' ) { + re = new RegExp('\\b' + this.escapeRegex(details.ua) + '\\b', 'i'); + if ( re.test(self.navigator.userAgent) ) { return true; } + } + return false; +}; + +/******************************************************************************/ + µBlock.scheduleAssetUpdater = (function() { var timer, next = 0; return function(updateDelay) { @@ -1196,7 +1222,7 @@ if ( details.entry.content === 'filters' ) { if ( details.entry.off !== true || - this.matchCurrentLanguage(details.entry.lang) + this.listMatchesEnvironment(details.entry) ) { this.saveSelectedFilterLists([ details.assetKey ], true); } diff --git a/src/js/utils.js b/src/js/utils.js index b802835d8..fdde0d3ec 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -347,18 +347,6 @@ /******************************************************************************/ -// https://github.com/gorhill/uBlock/issues/2344 - -µBlock.matchCurrentLanguage = function(s) { - if ( typeof s !== 'string' ) { return false; } - if ( this.matchCurrentLanguage.reLang === undefined ) { - this.matchCurrentLanguage.reLang = new RegExp('\\b' + self.navigator.language.slice(0, 2) + '\\b'); - } - return this.matchCurrentLanguage.reLang.test(s); -}; - -/******************************************************************************/ - µBlock.MRUCache = function(size) { this.size = size; this.array = []; @@ -396,3 +384,11 @@ }; /******************************************************************************/ + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions + +µBlock.escapeRegex = function(s) { + return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +}; + +/******************************************************************************/