Skip to content

Commit

Permalink
Add ability to control auto-commenting at filter creation time
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
hawkeye116477 and gorhill committed Aug 11, 2020
1 parent 9bd4d38 commit 149e30f
Showing 5 changed files with 48 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
@@ -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,
5 changes: 3 additions & 2 deletions src/js/logger-ui.js
Original file line number Diff line number Diff line change
@@ -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
}
);
}
20 changes: 9 additions & 11 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions src/js/scriptlets/element-picker.js
Original file line number Diff line number Diff line change
@@ -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
}
);
33 changes: 31 additions & 2 deletions src/js/storage.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 149e30f

Please sign in to comment.