Add ability to use full URL in auto-generated comment

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/1281

New supported placeholder: `{{url}}`, which will be
replaced by the full URL of the page for which a filter
is created.
This commit is contained in:
Raymond Hill 2020-10-07 11:52:38 -04:00
parent bfe5e2daa1
commit 46ec969411
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 23 additions and 25 deletions

View File

@ -53,9 +53,9 @@ const epickerId = (( ) => {
})(); })();
if ( epickerId === null ) { return; } if ( epickerId === null ) { return; }
const docURL = new URL(vAPI.getURL(''));
let epickerConnectionId; let epickerConnectionId;
let filterHostname = '';
let filterOrigin = '';
let resultsetOpt; let resultsetOpt;
let netFilterCandidates = []; let netFilterCandidates = [];
@ -102,9 +102,11 @@ const renderRange = function(id, value, invert = false) {
const userFilterFromCandidate = function(filter) { const userFilterFromCandidate = function(filter) {
if ( filter === '' || filter === '!' ) { return; } if ( filter === '' || filter === '!' ) { return; }
const hn = vAPI.hostnameFromURI(docURL.href);
// Cosmetic filter? // Cosmetic filter?
if ( filter.startsWith('##') ) { if ( filter.startsWith('##') ) {
return filterHostname + filter; return hn + filter;
} }
// Assume net filter // Assume net filter
@ -112,7 +114,7 @@ const userFilterFromCandidate = function(filter) {
// If no domain included in filter, we need domain option // If no domain included in filter, we need domain option
if ( filter.startsWith('||') === false ) { if ( filter.startsWith('||') === false ) {
opts.push(`domain=${filterHostname}`); opts.push(`domain=${hn}`);
} }
if ( resultsetOpt !== undefined ) { if ( resultsetOpt !== undefined ) {
@ -416,8 +418,7 @@ const onCreateClicked = function() {
what: 'createUserFilter', what: 'createUserFilter',
autoComment: true, autoComment: true,
filters: filter, filters: filter,
origin: filterOrigin, docURL: docURL.href,
pageDomain: filterHostname,
killCache: /^#[$?]?#/.test(candidate) === false, killCache: /^#[$?]?#/.test(candidate) === false,
}); });
} }
@ -672,13 +673,7 @@ const showDialog = function(details) {
} }
cosmeticFilterCandidates = cosmeticFilters; cosmeticFilterCandidates = cosmeticFilters;
// https://github.com/gorhill/uBlock/issues/738 docURL.href = details.url;
// Trim dots.
filterHostname = details.hostname;
if ( filterHostname.slice(-1) === '.' ) {
filterHostname = filterHostname.slice(0, -1);
}
filterOrigin = details.origin;
populateCandidates(netFilters, '#netFilters'); populateCandidates(netFilters, '#netFilters');
populateCandidates(cosmeticFilters, '#cosmeticFilters'); populateCandidates(cosmeticFilters, '#cosmeticFilters');

View File

@ -1263,13 +1263,16 @@ const reloadTab = function(ev) {
// Avoid duplicates // Avoid duplicates
if ( createdStaticFilters.hasOwnProperty(value) ) { return; } if ( createdStaticFilters.hasOwnProperty(value) ) { return; }
createdStaticFilters[value] = true; createdStaticFilters[value] = true;
// https://github.com/uBlockOrigin/uBlock-issues/issues/1281#issuecomment-704217175
// TODO:
// Figure a way to use the actual document URL. Currently using
// a synthetic URL derived from the document hostname.
if ( value !== '' ) { if ( value !== '' ) {
messaging.send('loggerUI', { messaging.send('loggerUI', {
what: 'createUserFilter', what: 'createUserFilter',
autoComment: true, autoComment: true,
filters: value, filters: value,
origin: targetPageDomain, docURL: `https://${targetFrameHostname}/`,
pageDomain: targetPageDomain,
}); });
} }
updateWidgets(); updateWidgets();
@ -1872,8 +1875,6 @@ const reloadTab = function(ev) {
); );
})(); })();
// https://www.youtube.com/watch?v=XyNYrmmdUd4
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/

View File

@ -843,8 +843,7 @@ const onOptmizeCandidate = function(details) {
const showDialog = function(options) { const showDialog = function(options) {
vAPI.MessagingConnection.sendTo(epickerConnectionId, { vAPI.MessagingConnection.sendTo(epickerConnectionId, {
what: 'showDialog', what: 'showDialog',
hostname: self.location.hostname, url: self.location.href,
origin: self.location.origin,
netFilters: netFilterCandidates, netFilters: netFilterCandidates,
cosmeticFilters: cosmeticFilterCandidates, cosmeticFilters: cosmeticFilterCandidates,
filter: bestCandidateFilter, filter: bestCandidateFilter,

View File

@ -399,12 +399,15 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
// Date in YYYY-MM-DD format - https://stackoverflow.com/a/50130338 // Date in YYYY-MM-DD format - https://stackoverflow.com/a/50130338
const ISO8061Date = new Date(d.getTime() + const ISO8061Date = new Date(d.getTime() +
(d.getTimezoneOffset()*60000)).toISOString().split('T')[0]; (d.getTimezoneOffset()*60000)).toISOString().split('T')[0];
const url = new URL(options.docURL);
comment = comment =
'! ' + '! ' +
this.hiddenSettings.autoCommentFilterTemplate this.hiddenSettings.autoCommentFilterTemplate
.replace('{{date}}', ISO8061Date) .replace('{{date}}', ISO8061Date)
.replace('{{time}}', d.toLocaleTimeString()) .replace('{{time}}', d.toLocaleTimeString())
.replace('{{origin}}', options.origin); .replace('{{hostname}}', url.hostname)
.replace('{{origin}}', url.origin)
.replace('{{url}}', url.href);
} }
const details = await this.loadUserFilters(); const details = await this.loadUserFilters();
@ -414,10 +417,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
// from the last comment found in the user filter list. // from the last comment found in the user filter list.
if ( comment !== '' ) { if ( comment !== '' ) {
const pos = details.content.lastIndexOf(comment); const pos = details.content.lastIndexOf(comment);
if ( if ( pos === -1 || details.content.indexOf('\n!', pos + 1) !== -1 ) {
pos === -1 ||
details.content.indexOf('\n!', pos + 1) !== -1
) {
filters = '\n' + comment + '\n' + filters; filters = '\n' + comment + '\n' + filters;
} }
} }
@ -462,7 +462,10 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
µBlock.createUserFilters = function(details) { µBlock.createUserFilters = function(details) {
this.appendUserFilters(details.filters, details); this.appendUserFilters(details.filters, details);
// https://github.com/gorhill/uBlock/issues/1786 // https://github.com/gorhill/uBlock/issues/1786
this.cosmeticFilteringEngine.removeFromSelectorCache(details.pageDomain); if ( details.docURL === undefined ) { return; }
this.cosmeticFilteringEngine.removeFromSelectorCache(
vAPI.hostnameFromURI(details.docURL)
);
}; };
/******************************************************************************/ /******************************************************************************/