Skip to content

Commit

Permalink
Add ability to use full URL in auto-generated comment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Oct 7, 2020
1 parent bfe5e2d commit 46ec969
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
21 changes: 8 additions & 13 deletions src/js/epicker-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -102,17 +102,19 @@ 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
const opts = [];

// 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 ) {
Expand Down Expand Up @@ -416,8 +418,7 @@ const onCreateClicked = function() {
what: 'createUserFilter',
autoComment: true,
filters: filter,
origin: filterOrigin,
pageDomain: filterHostname,
docURL: docURL.href,
killCache: /^#[$?]?#/.test(candidate) === false,
});
}
Expand Down Expand Up @@ -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');
Expand Down
9 changes: 5 additions & 4 deletions src/js/logger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1872,8 +1875,6 @@ const reloadTab = function(ev) {
);
})();

// https://www.youtube.com/watch?v=XyNYrmmdUd4

/******************************************************************************/
/******************************************************************************/

Expand Down
3 changes: 1 addition & 2 deletions src/js/scriptlets/epicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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)
);
};

/******************************************************************************/
Expand Down

0 comments on commit 46ec969

Please sign in to comment.