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

View File

@ -1263,13 +1263,16 @@ const reloadTab = function(ev) {
// Avoid duplicates
if ( createdStaticFilters.hasOwnProperty(value) ) { return; }
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 !== '' ) {
messaging.send('loggerUI', {
what: 'createUserFilter',
autoComment: true,
filters: value,
origin: targetPageDomain,
pageDomain: targetPageDomain,
docURL: `https://${targetFrameHostname}/`,
});
}
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) {
vAPI.MessagingConnection.sendTo(epickerConnectionId, {
what: 'showDialog',
hostname: self.location.hostname,
origin: self.location.origin,
url: self.location.href,
netFilters: netFilterCandidates,
cosmeticFilters: cosmeticFilterCandidates,
filter: bestCandidateFilter,

View File

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