Skip to content

Commit 46ec969

Browse files
committed
Add ability to use full URL in auto-generated comment
Related issue: - uBlockOrigin/uBlock-issues#1281 New supported placeholder: `{{url}}`, which will be replaced by the full URL of the page for which a filter is created.
1 parent bfe5e2d commit 46ec969

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

src/js/epicker-ui.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ const epickerId = (( ) => {
5353
})();
5454
if ( epickerId === null ) { return; }
5555

56+
const docURL = new URL(vAPI.getURL(''));
57+
5658
let epickerConnectionId;
57-
let filterHostname = '';
58-
let filterOrigin = '';
5959
let resultsetOpt;
6060

6161
let netFilterCandidates = [];
@@ -102,17 +102,19 @@ const renderRange = function(id, value, invert = false) {
102102
const userFilterFromCandidate = function(filter) {
103103
if ( filter === '' || filter === '!' ) { return; }
104104

105+
const hn = vAPI.hostnameFromURI(docURL.href);
106+
105107
// Cosmetic filter?
106108
if ( filter.startsWith('##') ) {
107-
return filterHostname + filter;
109+
return hn + filter;
108110
}
109111

110112
// Assume net filter
111113
const opts = [];
112114

113115
// If no domain included in filter, we need domain option
114116
if ( filter.startsWith('||') === false ) {
115-
opts.push(`domain=${filterHostname}`);
117+
opts.push(`domain=${hn}`);
116118
}
117119

118120
if ( resultsetOpt !== undefined ) {
@@ -416,8 +418,7 @@ const onCreateClicked = function() {
416418
what: 'createUserFilter',
417419
autoComment: true,
418420
filters: filter,
419-
origin: filterOrigin,
420-
pageDomain: filterHostname,
421+
docURL: docURL.href,
421422
killCache: /^#[$?]?#/.test(candidate) === false,
422423
});
423424
}
@@ -672,13 +673,7 @@ const showDialog = function(details) {
672673
}
673674
cosmeticFilterCandidates = cosmeticFilters;
674675

675-
// https://github.com/gorhill/uBlock/issues/738
676-
// Trim dots.
677-
filterHostname = details.hostname;
678-
if ( filterHostname.slice(-1) === '.' ) {
679-
filterHostname = filterHostname.slice(0, -1);
680-
}
681-
filterOrigin = details.origin;
676+
docURL.href = details.url;
682677

683678
populateCandidates(netFilters, '#netFilters');
684679
populateCandidates(cosmeticFilters, '#cosmeticFilters');

src/js/logger-ui.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,13 +1263,16 @@ const reloadTab = function(ev) {
12631263
// Avoid duplicates
12641264
if ( createdStaticFilters.hasOwnProperty(value) ) { return; }
12651265
createdStaticFilters[value] = true;
1266+
// https://github.com/uBlockOrigin/uBlock-issues/issues/1281#issuecomment-704217175
1267+
// TODO:
1268+
// Figure a way to use the actual document URL. Currently using
1269+
// a synthetic URL derived from the document hostname.
12661270
if ( value !== '' ) {
12671271
messaging.send('loggerUI', {
12681272
what: 'createUserFilter',
12691273
autoComment: true,
12701274
filters: value,
1271-
origin: targetPageDomain,
1272-
pageDomain: targetPageDomain,
1275+
docURL: `https://${targetFrameHostname}/`,
12731276
});
12741277
}
12751278
updateWidgets();
@@ -1872,8 +1875,6 @@ const reloadTab = function(ev) {
18721875
);
18731876
})();
18741877

1875-
// https://www.youtube.com/watch?v=XyNYrmmdUd4
1876-
18771878
/******************************************************************************/
18781879
/******************************************************************************/
18791880

src/js/scriptlets/epicker.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,7 @@ const onOptmizeCandidate = function(details) {
843843
const showDialog = function(options) {
844844
vAPI.MessagingConnection.sendTo(epickerConnectionId, {
845845
what: 'showDialog',
846-
hostname: self.location.hostname,
847-
origin: self.location.origin,
846+
url: self.location.href,
848847
netFilters: netFilterCandidates,
849848
cosmeticFilters: cosmeticFilterCandidates,
850849
filter: bestCandidateFilter,

src/js/storage.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,15 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
399399
// Date in YYYY-MM-DD format - https://stackoverflow.com/a/50130338
400400
const ISO8061Date = new Date(d.getTime() +
401401
(d.getTimezoneOffset()*60000)).toISOString().split('T')[0];
402+
const url = new URL(options.docURL);
402403
comment =
403404
'! ' +
404405
this.hiddenSettings.autoCommentFilterTemplate
405406
.replace('{{date}}', ISO8061Date)
406407
.replace('{{time}}', d.toLocaleTimeString())
407-
.replace('{{origin}}', options.origin);
408+
.replace('{{hostname}}', url.hostname)
409+
.replace('{{origin}}', url.origin)
410+
.replace('{{url}}', url.href);
408411
}
409412

410413
const details = await this.loadUserFilters();
@@ -414,10 +417,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
414417
// from the last comment found in the user filter list.
415418
if ( comment !== '' ) {
416419
const pos = details.content.lastIndexOf(comment);
417-
if (
418-
pos === -1 ||
419-
details.content.indexOf('\n!', pos + 1) !== -1
420-
) {
420+
if ( pos === -1 || details.content.indexOf('\n!', pos + 1) !== -1 ) {
421421
filters = '\n' + comment + '\n' + filters;
422422
}
423423
}
@@ -462,7 +462,10 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
462462
µBlock.createUserFilters = function(details) {
463463
this.appendUserFilters(details.filters, details);
464464
// https://github.com/gorhill/uBlock/issues/1786
465-
this.cosmeticFilteringEngine.removeFromSelectorCache(details.pageDomain);
465+
if ( details.docURL === undefined ) { return; }
466+
this.cosmeticFilteringEngine.removeFromSelectorCache(
467+
vAPI.hostnameFromURI(details.docURL)
468+
);
466469
};
467470

468471
/******************************************************************************/

0 commit comments

Comments
 (0)