mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-18 17:02:27 +02:00
Add ability to control auto-commenting at filter creation time
Related issues: - https://github.com/uBlockOrigin/uBlock-issues/issues/372 - https://github.com/gorhill/uBlock/issues/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.
This commit is contained in:
parent
40096bea28
commit
64bea27881
@ -40,6 +40,7 @@ const µBlock = (function() { // jshint ignore:line
|
|||||||
|
|
||||||
const hiddenSettingsDefault = {
|
const hiddenSettingsDefault = {
|
||||||
assetFetchTimeout: 30,
|
assetFetchTimeout: 30,
|
||||||
|
autoCommentFilterTemplate: '{{date}} {{origin}}',
|
||||||
autoUpdateAssetFetchPeriod: 120,
|
autoUpdateAssetFetchPeriod: 120,
|
||||||
autoUpdatePeriod: 7,
|
autoUpdatePeriod: 7,
|
||||||
cacheStorageCompression: true,
|
cacheStorageCompression: true,
|
||||||
|
@ -882,13 +882,14 @@ var netFilteringManager = (function() {
|
|||||||
}
|
}
|
||||||
createdStaticFilters[value] = true;
|
createdStaticFilters[value] = true;
|
||||||
if ( value !== '' ) {
|
if ( value !== '' ) {
|
||||||
var d = new Date();
|
|
||||||
messaging.send(
|
messaging.send(
|
||||||
'loggerUI',
|
'loggerUI',
|
||||||
{
|
{
|
||||||
what: 'createUserFilter',
|
what: 'createUserFilter',
|
||||||
|
autoComment: true,
|
||||||
|
filters: value,
|
||||||
|
origin: targetPageDomain,
|
||||||
pageDomain: targetPageDomain,
|
pageDomain: targetPageDomain,
|
||||||
filters: '! ' + d.toLocaleString() + ' ' + targetPageDomain + '\n' + value
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ var getDomainNames = function(targets) {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var onMessage = function(request, sender, callback) {
|
const onMessage = function(request, sender, callback) {
|
||||||
// Async
|
// Async
|
||||||
switch ( request.what ) {
|
switch ( request.what ) {
|
||||||
case 'getAssetContent':
|
case 'getAssetContent':
|
||||||
@ -84,7 +84,7 @@ var onMessage = function(request, sender, callback) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tabId = sender && sender.tab ? sender.tab.id : 0;
|
const tabId = sender && sender.tab ? sender.tab.id : 0;
|
||||||
|
|
||||||
// Sync
|
// Sync
|
||||||
var response;
|
var response;
|
||||||
@ -103,7 +103,7 @@ var onMessage = function(request, sender, callback) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'createUserFilter':
|
case 'createUserFilter':
|
||||||
µb.appendUserFilters(request.filters);
|
µb.appendUserFilters(request.filters, request);
|
||||||
// https://github.com/gorhill/uBlock/issues/1786
|
// https://github.com/gorhill/uBlock/issues/1786
|
||||||
µb.cosmeticFilteringEngine.removeFromSelectorCache(request.pageDomain);
|
µb.cosmeticFilteringEngine.removeFromSelectorCache(request.pageDomain);
|
||||||
break;
|
break;
|
||||||
@ -608,15 +608,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
|
// Async
|
||||||
switch ( request.what ) {
|
switch ( request.what ) {
|
||||||
case 'elementPickerArguments':
|
case 'elementPickerArguments':
|
||||||
var xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', 'epicker.html', true);
|
xhr.open('GET', 'epicker.html', true);
|
||||||
xhr.overrideMimeType('text/html;charset=utf-8');
|
xhr.overrideMimeType('text/html;charset=utf-8');
|
||||||
xhr.responseType = 'text';
|
xhr.responseType = 'text';
|
||||||
@ -632,8 +630,8 @@ var onMessage = function(request, sender, callback) {
|
|||||||
cosmeticFilters: vAPI.i18n('pickerCosmeticFilters'),
|
cosmeticFilters: vAPI.i18n('pickerCosmeticFilters'),
|
||||||
cosmeticFiltersHint: vAPI.i18n('pickerCosmeticFiltersHint')
|
cosmeticFiltersHint: vAPI.i18n('pickerCosmeticFiltersHint')
|
||||||
};
|
};
|
||||||
var reStrings = /\{\{(\w+)\}\}/g;
|
const reStrings = /\{\{(\w+)\}\}/g;
|
||||||
var replacer = function(a0, string) {
|
const replacer = function(a0, string) {
|
||||||
return i18n[string];
|
return i18n[string];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -657,7 +655,7 @@ var onMessage = function(request, sender, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sync
|
// Sync
|
||||||
var response;
|
let response;
|
||||||
|
|
||||||
switch ( request.what ) {
|
switch ( request.what ) {
|
||||||
case 'elementPickerEprom':
|
case 'elementPickerEprom':
|
||||||
|
@ -1144,7 +1144,7 @@ var filterChoiceFromEvent = function(ev) {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var onDialogClicked = function(ev) {
|
const onDialogClicked = function(ev) {
|
||||||
if ( ev.isTrusted === false ) { return; }
|
if ( ev.isTrusted === false ) { return; }
|
||||||
|
|
||||||
// If the dialog is hidden, clicking on it force it to become visible.
|
// If the dialog is hidden, clicking on it force it to become visible.
|
||||||
@ -1163,12 +1163,13 @@ var onDialogClicked = function(ev) {
|
|||||||
filterToDOMInterface.preview(false);
|
filterToDOMInterface.preview(false);
|
||||||
userFilterFromCandidate(function(filter) {
|
userFilterFromCandidate(function(filter) {
|
||||||
if ( !filter ) { return; }
|
if ( !filter ) { return; }
|
||||||
var d = new Date();
|
|
||||||
vAPI.messaging.send(
|
vAPI.messaging.send(
|
||||||
'elementPicker',
|
'elementPicker',
|
||||||
{
|
{
|
||||||
what: 'createUserFilter',
|
what: 'createUserFilter',
|
||||||
filters: '! ' + d.toLocaleString() + ' ' + window.location.href + '\n' + filter,
|
autoComment: true,
|
||||||
|
filters: filter,
|
||||||
|
origin: window.location.origin,
|
||||||
pageDomain: window.location.hostname
|
pageDomain: window.location.hostname
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -404,9 +404,27 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.appendUserFilters = function(filters) {
|
µBlock.appendUserFilters = function(filters, options) {
|
||||||
|
filters = filters.trim();
|
||||||
if ( filters.length === 0 ) { return; }
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
const onSaved = ( ) => {
|
const onSaved = ( ) => {
|
||||||
const compiledFilters = this.compileFilters(
|
const compiledFilters = this.compileFilters(
|
||||||
filters,
|
filters,
|
||||||
@ -435,11 +453,22 @@
|
|||||||
|
|
||||||
const onLoaded = details => {
|
const onLoaded = details => {
|
||||||
if ( details.error ) { return; }
|
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
|
// https://github.com/chrisaljoudi/uBlock/issues/976
|
||||||
// If we reached this point, the filter quite probably needs to be
|
// 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
|
// added for sure: do not try to be too smart, trying to avoid
|
||||||
// duplicates at this point may lead to more issues.
|
// duplicates at this point may lead to more issues.
|
||||||
this.saveUserFilters(details.content.trim() + '\n\n' + filters.trim(), onSaved);
|
this.saveUserFilters(details.content.trim() + '\n' + filters, onSaved);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.loadUserFilters(onLoaded);
|
this.loadUserFilters(onLoaded);
|
||||||
|
Loading…
Reference in New Issue
Block a user