From 149e30f20e7437a382ea4f970eec4d9e42ee5e09 Mon Sep 17 00:00:00 2001 From: hawkeye116477 Date: Tue, 11 Aug 2020 13:27:39 +0200 Subject: [PATCH] Add ability to control auto-commenting at filter creation time Related issues: - uBlockOrigin/uBlock-issues#372 -gorhill/uBlock#93 A new advanced settings has been added: `autoCommentFilterTemplate`. Default value is `{{date}} {{origin}}`. Placeholders are identified by `{{...}}`. There are currently only three placeholders supported: - `{{date}}`: will be replaced with current date - `{{time}}`: will be replaced with current time - `{{origin}}`: will be replaced with site information on which the filter(s) was created If no placeholder is found in `autoCommentFilterTemplate`, this will disable auto-commenting. So one can use `-` to disable auto-commenting. Additionally, if auto-commenting is enabled, uBO will not emit a comment if an emitted comment would be a duplicate of the last one found in the user filter list. Co-authored-by: gorhill <585534+gorhill@users.noreply.github.com> --- src/js/background.js | 1 + src/js/logger-ui.js | 5 +++-- src/js/messaging.js | 20 ++++++++--------- src/js/scriptlets/element-picker.js | 7 +++--- src/js/storage.js | 33 +++++++++++++++++++++++++++-- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index a4209f3f7..506bfc691 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -43,6 +43,7 @@ var µBlock = (function() { // jshint ignore:line var hiddenSettingsDefault = { assetConvertMyFilters: false, assetFetchTimeout: 30, + autoCommentFilterTemplate: '{{date}} {{origin}}', autoUpdateAssetFetchPeriod: 120, autoUpdatePeriod: 7, ignoreRedirectFilters: false, diff --git a/src/js/logger-ui.js b/src/js/logger-ui.js index bae91a0f9..a604e79a8 100644 --- a/src/js/logger-ui.js +++ b/src/js/logger-ui.js @@ -818,13 +818,14 @@ var netFilteringManager = (function() { } createdStaticFilters[value] = true; if ( value !== '' ) { - var d = new Date(); messaging.send( 'loggerUI', { what: 'createUserFilter', + autoComment: true, + filters: value, + origin: targetPageDomain, pageDomain: targetPageDomain, - filters: '! ' + d.toLocaleString() + ' ' + targetPageDomain + '\n' + value } ); } diff --git a/src/js/messaging.js b/src/js/messaging.js index af8ddf080..69da9787a 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -52,7 +52,7 @@ var getDomainNames = function(targets) { /******************************************************************************/ -var onMessage = function(request, sender, callback) { +const onMessage = function(request, sender, callback) { // Async switch ( request.what ) { case 'getAssetContent': @@ -114,7 +114,7 @@ var onMessage = function(request, sender, callback) { break; case 'createUserFilter': - µb.appendUserFilters(request.filters); + µb.appendUserFilters(request.filters, request); // https://github.com/gorhill/uBlock/issues/1786 µb.cosmeticFilteringEngine.removeFromSelectorCache(request.pageDomain); break; @@ -572,15 +572,13 @@ vAPI.messaging.listen('contentscript', onMessage); /******************************************************************************/ -var µb = µBlock; - -/******************************************************************************/ +const onMessage = function(request, sender, callback) { + const µb = µBlock; -var onMessage = function(request, sender, callback) { // Async switch ( request.what ) { case 'elementPickerArguments': - var xhr = new XMLHttpRequest(); + const xhr = new XMLHttpRequest(); xhr.open('GET', 'epicker.html', true); xhr.overrideMimeType('text/html;charset=utf-8'); xhr.responseType = 'text'; @@ -596,8 +594,8 @@ var onMessage = function(request, sender, callback) { cosmeticFilters: vAPI.i18n('pickerCosmeticFilters'), cosmeticFiltersHint: vAPI.i18n('pickerCosmeticFiltersHint') }; - var reStrings = /\{\{(\w+)\}\}/g; - var replacer = function(a0, string) { + const reStrings = /\{\{(\w+)\}\}/g; + const replacer = function(a0, string) { return i18n[string]; }; @@ -621,7 +619,7 @@ var onMessage = function(request, sender, callback) { } // Sync - var response; + let response; switch ( request.what ) { case 'elementPickerEprom': @@ -1236,7 +1234,7 @@ var logCosmeticFilters = function(tabId, details) { /******************************************************************************/ var onMessage = function(request, sender, callback) { - var tabId = sender && sender.tab ? sender.tab.id : 0; + const tabId = sender && sender.tab ? sender.tab.id : 0; var pageStore = µb.pageStoreFromTabId(tabId); // Async diff --git a/src/js/scriptlets/element-picker.js b/src/js/scriptlets/element-picker.js index 4fd51274e..de065c0f2 100644 --- a/src/js/scriptlets/element-picker.js +++ b/src/js/scriptlets/element-picker.js @@ -1091,7 +1091,7 @@ var filterChoiceFromEvent = function(ev) { /******************************************************************************/ -var onDialogClicked = function(ev) { +const onDialogClicked = function(ev) { if ( ev.isTrusted === false ) { return; } // If the dialog is hidden, clicking on it force it to become visible. @@ -1110,12 +1110,13 @@ var onDialogClicked = function(ev) { filterToDOMInterface.preview(false); userFilterFromCandidate(function(filter) { if ( !filter ) { return; } - var d = new Date(); vAPI.messaging.send( 'elementPicker', { what: 'createUserFilter', - filters: '! ' + d.toLocaleString() + ' ' + window.location.href + '\n' + filter, + autoComment: true, + filters: filter, + origin: window.location.origin, pageDomain: window.location.hostname } ); diff --git a/src/js/storage.js b/src/js/storage.js index 5159ad80a..4bf3b0765 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -407,9 +407,27 @@ /******************************************************************************/ -µBlock.appendUserFilters = function(filters) { +µBlock.appendUserFilters = function(filters, options) { + filters = filters.trim(); if ( filters.length === 0 ) { return; } + // https://github.com/uBlockOrigin/uBlock-issues/issues/372 + // Auto comment using user-defined template. + let comment = ''; + if ( + options instanceof Object && + options.autoComment === true && + this.hiddenSettings.autoCommentFilterTemplate.indexOf('{{') !== -1 + ) { + const d = new Date(); + comment = + '! ' + + this.hiddenSettings.autoCommentFilterTemplate + .replace('{{date}}', d.toLocaleDateString()) + .replace('{{time}}', d.toLocaleTimeString()) + .replace('{{origin}}', options.origin); + } + var µb = this; var onSaved = function() { @@ -433,11 +451,22 @@ var onLoaded = function(details) { if ( details.error ) { return; } + // The comment, if any, will be applied if and only if it is different + // from the last comment found in the user filter list. + if ( comment !== '' ) { + const pos = details.content.lastIndexOf(comment); + if ( + pos === -1 || + details.content.indexOf('\n!', pos + 1) !== -1 + ) { + filters = '\n' + comment + '\n' + filters; + } + } // https://github.com/chrisaljoudi/uBlock/issues/976 // If we reached this point, the filter quite probably needs to be // added for sure: do not try to be too smart, trying to avoid // duplicates at this point may lead to more issues. - µb.saveUserFilters(details.content.trim() + '\n\n' + filters.trim(), onSaved); + µb.saveUserFilters(details.content.trim() + '\n' + filters, onSaved); }; this.loadUserFilters(onLoaded);