From d5f40b90f639bfeadaa28c811a0fefecd5c384d1 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 22 Jul 2018 08:14:02 -0400 Subject: [PATCH] fix #2179 --- src/_locales/en/messages.json | 8 ++- src/css/common.css | 1 - src/css/logger-ui.css | 12 ++++ src/js/i18n.js | 45 ++++++++++++-- src/js/logger-ui.js | 105 +++++++++++++++------------------ src/js/reverselookup-worker.js | 1 + src/logger-ui.html | 6 +- 7 files changed, 114 insertions(+), 64 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index d76595780..58e9d7a26 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -588,8 +588,12 @@ "description":"Used in the static filtering wizard" }, "loggerStaticFilteringFinderSentence1":{ - "message":"Static filter {{filter}} found in:", - "description":"Below this sentence, the filter lists in which the filter was found" + "message":"Static filter {{filter}} found in:", + "description":"Below this sentence, the filter list(s) in which the filter was found" + }, + "loggerStaticFilteringFinderSentence2":{ + "message":"Static filter {{filter}} could not be found in any of the currently enabled filter lists", + "description":"Message to show when a filter cannot be found in any filter lists" }, "aboutChangelog":{ "message":"Change log", diff --git a/src/css/common.css b/src/css/common.css index 6927997ef..a483215d8 100644 --- a/src/css/common.css +++ b/src/css/common.css @@ -13,7 +13,6 @@ user-select: none; -moz-user-select: none; -webkit-user-select: none; - vertical-align: middle; } body { background-color: white; diff --git a/src/css/logger-ui.css b/src/css/logger-ui.css index 5a90028af..272042c12 100644 --- a/src/css/logger-ui.css +++ b/src/css/logger-ui.css @@ -599,6 +599,18 @@ body.colorBlind #netFilteringDialog .dialog > div.containers > div.dynamic tr.e #filterFinderDialog .dialog ul { font-size: larger; } +#filterFinderDialog .filterFinderListEntry a { + text-decoration: none; + } +#filterFinderDialog .filterFinderListEntry a.fa { + opacity: 0.8; + } +#filterFinderDialog .filterFinderListEntry a.fa:hover { + opacity: 1; + } +#filterFinderDialog .filterFinderListEntry a[href=""]:nth-of-type(2) { + display: none; + } #filterFinderDialog .dialog > *:first-child { margin-top: 0; } diff --git a/src/js/i18n.js b/src/js/i18n.js index c4ef90cd8..d10be88d8 100644 --- a/src/js/i18n.js +++ b/src/js/i18n.js @@ -1,7 +1,7 @@ /******************************************************************************* uBlock Origin - a browser extension to block requests. - Copyright (C) 2014-2016 Raymond Hill + Copyright (C) 2014-present 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 @@ -94,7 +94,8 @@ var safeTextToDOM = function(text, parent) { if ( text === '' ) { return; } // Fast path (most common). if ( text.indexOf('<') === -1 ) { - return parent.appendChild(safeTextToTextNode(text)); + parent.appendChild(safeTextToTextNode(text)); + return; } // Slow path. // `

` no longer allowed. Code below can be remove once all

's are @@ -102,7 +103,7 @@ var safeTextToDOM = function(text, parent) { text = text.replace(/^

|<\/p>/g, '') .replace(/

/g, '\n\n'); // Parse allowed HTML tags. - var matches = reSafeTags.exec(text); + let matches = reSafeTags.exec(text); if ( matches === null ) { matches = reSafeLink.exec(text); if ( matches === null ) { @@ -114,7 +115,7 @@ var safeTextToDOM = function(text, parent) { } } safeTextToDOM(matches[1], parent); - var node = safeTextToTagNode(matches[2]) || parent; + let node = safeTextToTagNode(matches[2]) || parent; safeTextToDOM(matches[3], node); parent.appendChild(node); safeTextToDOM(matches[4], parent); @@ -122,6 +123,42 @@ var safeTextToDOM = function(text, parent) { /******************************************************************************/ +vAPI.i18n.safeTemplateToDOM = function(id, dict, parent) { + if ( parent === undefined ) { + parent = document.createDocumentFragment(); + } + let textin = vAPI.i18n(id); + if ( textin === '' ) { + return parent; + } + if ( textin.indexOf('{{') === -1 ) { + safeTextToDOM(textin, parent); + return parent; + } + let re = /\{\{\w+\}\}/g; + let textout = ''; + for (;;) { + let match = re.exec(textin); + if ( match === null ) { + textout += textin; + break; + } + textout += textin.slice(0, match.index); + let prop = match[0].slice(2, -2); + if ( dict.hasOwnProperty(prop) ) { + textout += dict[prop].replace(//g, '>'); + } else { + textout += prop; + } + textin = textin.slice(re.lastIndex); + } + safeTextToDOM(textout, parent); + return parent; +}; + +/******************************************************************************/ + // Helper to deal with the i18n'ing of HTML files. vAPI.i18n.render = function(context) { var docu = document; diff --git a/src/js/logger-ui.js b/src/js/logger-ui.js index b8329788f..4e31996c2 100644 --- a/src/js/logger-ui.js +++ b/src/js/logger-ui.js @@ -1,7 +1,7 @@ /******************************************************************************* uBlock Origin - a browser extension to block requests. - Copyright (C) 2015-2018 Raymond Hill + Copyright (C) 2015-present 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 @@ -21,12 +21,12 @@ /* global uDom */ +'use strict'; + /******************************************************************************/ (function() { -'use strict'; - /******************************************************************************/ var logger = self.logger = { @@ -1266,21 +1266,18 @@ var netFilteringManager = (function() { /******************************************************************************/ var reverseLookupManager = (function() { - var reSentence1 = /\{\{filter\}\}/g; - var sentence1Template = vAPI.i18n('loggerStaticFilteringFinderSentence1'); - var filterFinderDialog = uDom.nodeFromId('filterFinderDialog'); + let filterFinderDialog = uDom.nodeFromId('filterFinderDialog'); + let rawFilter = ''; - var removeAllChildren = function(node) { + let removeAllChildren = function(node) { while ( node.firstChild ) { node.removeChild(node.firstChild); } }; - var onClick = function(ev) { - var target = ev.target; - - // click outside the dialog proper - if ( target.classList.contains('modalDialog') ) { + // Clicking outside the dialog will close the dialog + let onClick = function(ev) { + if ( ev.target.classList.contains('modalDialog') ) { toggleOff(); return; } @@ -1288,42 +1285,30 @@ var reverseLookupManager = (function() { ev.stopPropagation(); }; - var nodeFromFilter = function(filter, lists) { + let nodeFromFilter = function(filter, lists) { if ( Array.isArray(lists) === false || lists.length === 0 ) { - return null; + return; } - var node, - p = document.createElement('p'); - reSentence1.lastIndex = 0; - var matches = reSentence1.exec(sentence1Template); - if ( matches === null ) { - node = document.createTextNode(sentence1Template); - } else { - node = uDom.nodeFromSelector('#filterFinderDialogSentence1 > span').cloneNode(true); - node.childNodes[0].textContent = sentence1Template.slice(0, matches.index); - // https://github.com/gorhill/uBlock/issues/2753 - node.childNodes[1].textContent = filter.length <= 1024 - ? filter - : filter.slice(0, 1023) + '…'; - node.childNodes[2].textContent = sentence1Template.slice(reSentence1.lastIndex); - } - p.appendChild(node); + let p = document.createElement('p'); - var ul = document.createElement('ul'); - var list, li; - for ( var i = 0; i < lists.length; i++ ) { - list = lists[i]; - li = document.createElement('li'); + vAPI.i18n.safeTemplateToDOM( + 'loggerStaticFilteringFinderSentence1', + { filter: filter }, + p + ); + + let ul = document.createElement('ul'); + for ( let list of lists ) { + let li = document.querySelector('#filterFinderListEntry > li') + .cloneNode(true); + let a = li.querySelector('a:nth-of-type(1)'); + a.href += encodeURIComponent(list.assetKey); + a.textContent = list.title; if ( list.supportURL ) { - node = document.createElement('a'); - node.textContent = list.title; - node.setAttribute('href', list.supportURL); - node.setAttribute('target', '_blank'); - } else { - node = document.createTextNode(list.title); + a = li.querySelector('a:nth-of-type(2)'); + a.setAttribute('href', list.supportURL); } - li.appendChild(node); ul.appendChild(li); } p.appendChild(ul); @@ -1331,30 +1316,37 @@ var reverseLookupManager = (function() { return p; }; - var reverseLookupDone = function(response) { - if ( typeof response !== 'object' ) { - return; + let reverseLookupDone = function(response) { + if ( response instanceof Object === false ) { + response = {}; } - var dialog = filterFinderDialog.querySelector('.dialog'); + let dialog = filterFinderDialog.querySelector('.dialog'); removeAllChildren(dialog); - for ( var filter in response ) { - var p = nodeFromFilter(filter, response[filter]); - if ( p === null ) { continue; } + for ( let filter in response ) { + let p = nodeFromFilter(filter, response[filter]); + if ( p === undefined ) { continue; } dialog.appendChild(p); } + // https://github.com/gorhill/uBlock/issues/2179 + if ( dialog.childElementCount === 0 ) { + vAPI.i18n.safeTemplateToDOM( + 'loggerStaticFilteringFinderSentence2', + { filter: rawFilter }, + dialog + ); + } + document.body.appendChild(filterFinderDialog); filterFinderDialog.addEventListener('click', onClick, true); }; - var toggleOn = function(ev) { - var row = ev.target.parentElement; - var rawFilter = row.cells[2].textContent; - if ( rawFilter === '' ) { - return; - } + let toggleOn = function(ev) { + let row = ev.target.parentElement; + rawFilter = row.cells[2].textContent; + if ( rawFilter === '' ) { return; } if ( row.classList.contains('cat_net') ) { messaging.send( @@ -1379,9 +1371,10 @@ var reverseLookupManager = (function() { } }; - var toggleOff = function() { + let toggleOff = function() { filterFinderDialog.removeEventListener('click', onClick, true); document.body.removeChild(filterFinderDialog); + rawFilter = ''; }; return { diff --git a/src/js/reverselookup-worker.js b/src/js/reverselookup-worker.js index 5df97745f..8377097eb 100644 --- a/src/js/reverselookup-worker.js +++ b/src/js/reverselookup-worker.js @@ -238,6 +238,7 @@ var fromCosmeticFilter = function(details) { response[found] = []; } response[found].push({ + assetKey: assetKey, title: entry.title, supportURL: entry.supportURL }); diff --git a/src/logger-ui.html b/src/logger-ui.html index 804a1363f..2dc96a2b9 100644 --- a/src/logger-ui.html +++ b/src/logger-ui.html @@ -106,13 +106,17 @@

+
-