2022-09-07 08:15:36 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2022-09-13 15:44:24 -06:00
|
|
|
Copyright (C) 2022-present Raymond Hill
|
2022-09-07 08:15:36 -06:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
/* jshint esversion:11 */
|
|
|
|
|
2022-09-07 08:15:36 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-12-16 10:05:59 -07:00
|
|
|
import { browser, runtime, sendMessage } from './ext.js';
|
2022-09-15 11:14:08 -06:00
|
|
|
import { dom, qs$ } from './dom.js';
|
2022-09-13 15:44:24 -06:00
|
|
|
import { i18n$ } from './i18n.js';
|
|
|
|
import { simpleStorage } from './storage.js';
|
2022-09-07 08:15:36 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
const popupPanelData = {};
|
|
|
|
const currentTab = {};
|
2022-09-15 11:14:08 -06:00
|
|
|
let tabHostname = '';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
function normalizedHostname(hn) {
|
|
|
|
return hn.replace(/^www\./, '');
|
2022-09-15 11:14:08 -06:00
|
|
|
}
|
2022-09-08 08:04:08 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
const BLOCKING_MODE_MAX = 3;
|
2022-09-15 11:14:08 -06:00
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
function setFilteringMode(level, commit = false) {
|
|
|
|
const modeSlider = qs$('.filteringModeSlider');
|
|
|
|
modeSlider.dataset.level = level;
|
|
|
|
if ( qs$('.filteringModeSlider.moving') === null ) {
|
|
|
|
dom.text(
|
2022-11-12 07:51:22 -07:00
|
|
|
'#filteringModeText > span:nth-of-type(1)',
|
2022-10-10 10:28:24 -06:00
|
|
|
i18n$(`filteringMode${level}Name`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( commit !== true ) { return; }
|
|
|
|
commitFilteringMode();
|
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
async function commitFilteringMode() {
|
|
|
|
if ( tabHostname === '' ) { return; }
|
|
|
|
const targetHostname = normalizedHostname(tabHostname);
|
|
|
|
const modeSlider = qs$('.filteringModeSlider');
|
|
|
|
const afterLevel = parseInt(modeSlider.dataset.level, 10);
|
|
|
|
const beforeLevel = parseInt(modeSlider.dataset.levelBefore, 10);
|
|
|
|
if ( afterLevel > 1 ) {
|
|
|
|
let granted = false;
|
|
|
|
try {
|
|
|
|
granted = await browser.permissions.request({
|
|
|
|
origins: [ `*://*.${targetHostname}/*` ],
|
|
|
|
});
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
if ( granted !== true ) {
|
|
|
|
setFilteringMode(beforeLevel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dom.text(
|
2022-11-12 07:51:22 -07:00
|
|
|
'#filteringModeText > span:nth-of-type(1)',
|
2022-10-10 10:28:24 -06:00
|
|
|
i18n$(`filteringMode${afterLevel}Name`)
|
2022-09-08 08:04:08 -06:00
|
|
|
);
|
2022-10-10 10:28:24 -06:00
|
|
|
const actualLevel = await sendMessage({
|
|
|
|
what: 'setFilteringMode',
|
|
|
|
hostname: targetHostname,
|
|
|
|
level: afterLevel,
|
|
|
|
});
|
|
|
|
if ( actualLevel !== afterLevel ) {
|
|
|
|
setFilteringMode(actualLevel);
|
|
|
|
}
|
|
|
|
if ( actualLevel !== beforeLevel && popupPanelData.autoReload ) {
|
|
|
|
browser.tabs.reload(currentTab.id);
|
|
|
|
}
|
2022-09-07 08:15:36 -06:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
{
|
|
|
|
let mx0 = 0;
|
|
|
|
let mx1 = 0;
|
|
|
|
let l0 = 0;
|
|
|
|
let lMax = 0;
|
|
|
|
let timer;
|
|
|
|
|
|
|
|
const move = ( ) => {
|
|
|
|
timer = undefined;
|
|
|
|
const l1 = Math.min(Math.max(l0 + mx1 - mx0, 0), lMax);
|
|
|
|
let level = Math.floor(l1 * BLOCKING_MODE_MAX / lMax);
|
|
|
|
if ( qs$('body[dir="rtl"]') !== null ) {
|
|
|
|
level = 3 - level;
|
|
|
|
}
|
|
|
|
const modeSlider = qs$('.filteringModeSlider');
|
|
|
|
if ( `${level}` === modeSlider.dataset.level ) { return; }
|
|
|
|
dom.text(
|
2022-11-12 07:51:22 -07:00
|
|
|
'#filteringModeText > span:nth-of-type(2)',
|
2022-10-10 10:28:24 -06:00
|
|
|
i18n$(`filteringMode${level}Name`)
|
|
|
|
);
|
|
|
|
setFilteringMode(level);
|
|
|
|
};
|
|
|
|
|
|
|
|
const moveAsync = ev => {
|
|
|
|
if ( timer !== undefined ) { return; }
|
|
|
|
mx1 = ev.pageX;
|
|
|
|
timer = self.requestAnimationFrame(move);
|
|
|
|
};
|
|
|
|
|
|
|
|
const stop = ev => {
|
|
|
|
if ( ev.button !== 0 ) { return; }
|
|
|
|
const modeSlider = qs$('.filteringModeSlider');
|
|
|
|
if ( dom.cl.has(modeSlider, 'moving') === false ) { return; }
|
|
|
|
dom.cl.remove(modeSlider, 'moving');
|
|
|
|
self.removeEventListener('mousemove', moveAsync, { capture: true });
|
|
|
|
self.removeEventListener('mouseup', stop, { capture: true });
|
2022-11-12 07:51:22 -07:00
|
|
|
dom.text('#filteringModeText > span:nth-of-type(2)', '');
|
2022-10-10 10:28:24 -06:00
|
|
|
commitFilteringMode();
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
if ( timer !== undefined ) {
|
|
|
|
self.cancelAnimationFrame(timer);
|
|
|
|
timer = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const startSliding = ev => {
|
|
|
|
if ( ev.button !== 0 ) { return; }
|
|
|
|
const modeButton = qs$('.filteringModeButton');
|
|
|
|
if ( ev.currentTarget !== modeButton ) { return; }
|
|
|
|
const modeSlider = qs$('.filteringModeSlider');
|
|
|
|
if ( dom.cl.has(modeSlider, 'moving') ) { return; }
|
|
|
|
modeSlider.dataset.levelBefore = modeSlider.dataset.level;
|
|
|
|
mx0 = ev.pageX;
|
|
|
|
const buttonRect = modeButton.getBoundingClientRect();
|
|
|
|
l0 = buttonRect.left + buttonRect.width / 2;
|
|
|
|
const sliderRect = modeSlider.getBoundingClientRect();
|
|
|
|
lMax = sliderRect.width - buttonRect.width ;
|
|
|
|
dom.cl.add(modeSlider, 'moving');
|
|
|
|
self.addEventListener('mousemove', moveAsync, { capture: true });
|
|
|
|
self.addEventListener('mouseup', stop, { capture: true });
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
};
|
|
|
|
|
2022-11-12 07:51:22 -07:00
|
|
|
dom.on('.filteringModeButton', 'mousedown', startSliding);
|
2022-09-07 08:15:36 -06:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
dom.on(
|
2022-11-12 07:51:22 -07:00
|
|
|
'.filteringModeSlider',
|
2022-10-10 10:28:24 -06:00
|
|
|
'click',
|
|
|
|
'.filteringModeSlider span[data-level]',
|
|
|
|
ev => {
|
|
|
|
const modeSlider = qs$('.filteringModeSlider');
|
|
|
|
modeSlider.dataset.levelBefore = modeSlider.dataset.level;
|
|
|
|
const span = ev.target;
|
|
|
|
const level = parseInt(span.dataset.level, 10);
|
|
|
|
setFilteringMode(level, true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
dom.on(
|
2022-11-12 07:51:22 -07:00
|
|
|
'.filteringModeSlider',
|
2022-10-10 10:28:24 -06:00
|
|
|
'mouseenter',
|
|
|
|
'.filteringModeSlider span[data-level]',
|
|
|
|
ev => {
|
|
|
|
const span = ev.target;
|
|
|
|
const level = parseInt(span.dataset.level, 10);
|
|
|
|
dom.text(
|
2022-11-12 07:51:22 -07:00
|
|
|
'#filteringModeText > span:nth-of-type(2)',
|
2022-10-10 10:28:24 -06:00
|
|
|
i18n$(`filteringMode${level}Name`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
dom.on(
|
2022-11-12 07:51:22 -07:00
|
|
|
'.filteringModeSlider',
|
2022-10-10 10:28:24 -06:00
|
|
|
'mouseleave',
|
|
|
|
'.filteringModeSlider span[data-level]',
|
|
|
|
( ) => {
|
2022-11-12 07:51:22 -07:00
|
|
|
dom.text('#filteringModeText > span:nth-of-type(2)', '');
|
2022-10-10 10:28:24 -06:00
|
|
|
}
|
|
|
|
);
|
2022-09-07 08:15:36 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2022-09-08 08:04:08 -06:00
|
|
|
|
|
|
|
// The popup panel is made of sections. Visibility of sections can be
|
|
|
|
// toggled on/off.
|
|
|
|
|
2022-09-15 11:14:08 -06:00
|
|
|
const maxNumberOfSections = 2;
|
2022-09-08 08:04:08 -06:00
|
|
|
|
|
|
|
const sectionBitsFromAttribute = function() {
|
2022-09-15 11:14:08 -06:00
|
|
|
const value = dom.body.dataset.section;
|
|
|
|
if ( value === '' ) { return 0; }
|
2022-09-08 08:04:08 -06:00
|
|
|
let bits = 0;
|
2022-09-15 11:14:08 -06:00
|
|
|
for ( const c of value.split(' ') ) {
|
2022-09-08 08:04:08 -06:00
|
|
|
bits |= 1 << (c.charCodeAt(0) - 97);
|
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
};
|
|
|
|
|
|
|
|
const sectionBitsToAttribute = function(bits) {
|
|
|
|
if ( typeof bits !== 'number' ) { return; }
|
|
|
|
if ( isNaN(bits) ) { return; }
|
2022-09-15 11:14:08 -06:00
|
|
|
const value = [];
|
2022-09-08 08:04:08 -06:00
|
|
|
for ( let i = 0; i < maxNumberOfSections; i++ ) {
|
|
|
|
const bit = 1 << i;
|
|
|
|
if ( (bits & bit) === 0 ) { continue; }
|
2022-09-15 11:14:08 -06:00
|
|
|
value.push(String.fromCharCode(97 + i));
|
2022-09-08 08:04:08 -06:00
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
dom.body.dataset.section = value.join(' ');
|
2022-09-08 08:04:08 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
async function toggleSections(more) {
|
|
|
|
let currentBits = sectionBitsFromAttribute();
|
|
|
|
let newBits = currentBits;
|
|
|
|
for ( let i = 0; i < maxNumberOfSections; i++ ) {
|
|
|
|
const bit = 1 << (more ? i : maxNumberOfSections - i - 1);
|
|
|
|
if ( more ) {
|
|
|
|
newBits |= bit;
|
|
|
|
} else {
|
|
|
|
newBits &= ~bit;
|
|
|
|
}
|
|
|
|
if ( newBits !== currentBits ) { break; }
|
|
|
|
}
|
|
|
|
if ( newBits === currentBits ) { return; }
|
|
|
|
sectionBitsToAttribute(newBits);
|
2022-09-13 15:44:24 -06:00
|
|
|
simpleStorage.setItem('popupPanelSections', newBits);
|
2022-09-08 08:04:08 -06:00
|
|
|
}
|
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
simpleStorage.getItem('popupPanelSections').then(s => {
|
|
|
|
sectionBitsToAttribute(parseInt(s, 10) || 0);
|
|
|
|
});
|
2022-09-08 08:04:08 -06:00
|
|
|
|
2022-11-12 07:51:22 -07:00
|
|
|
dom.on('#moreButton', 'click', ( ) => {
|
2022-09-08 08:04:08 -06:00
|
|
|
toggleSections(true);
|
|
|
|
});
|
|
|
|
|
2022-11-12 07:51:22 -07:00
|
|
|
dom.on('#lessButton', 'click', ( ) => {
|
2022-09-08 08:04:08 -06:00
|
|
|
toggleSections(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2022-12-16 10:05:59 -07:00
|
|
|
|
|
|
|
dom.on('[data-i18n-title="popupTipDashboard"]', 'click', ev => {
|
|
|
|
if ( ev.isTrusted !== true ) { return; }
|
|
|
|
if ( ev.button !== 0 ) { return; }
|
|
|
|
runtime.openOptionsPage();
|
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2022-09-08 08:04:08 -06:00
|
|
|
|
2022-09-15 11:14:08 -06:00
|
|
|
async function init() {
|
2022-11-25 07:22:45 -07:00
|
|
|
const [ tab ] = await browser.tabs.query({
|
|
|
|
active: true,
|
2022-11-25 14:01:58 -07:00
|
|
|
currentWindow: true,
|
2022-11-25 07:22:45 -07:00
|
|
|
});
|
2022-09-15 11:14:08 -06:00
|
|
|
if ( tab instanceof Object === false ) { return true; }
|
2022-10-10 10:28:24 -06:00
|
|
|
Object.assign(currentTab, tab);
|
2022-09-15 11:14:08 -06:00
|
|
|
|
|
|
|
let url;
|
|
|
|
try {
|
|
|
|
url = new URL(currentTab.url);
|
|
|
|
tabHostname = url.hostname || '';
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( url !== undefined ) {
|
2022-10-10 10:28:24 -06:00
|
|
|
const response = await sendMessage({
|
2022-09-15 11:14:08 -06:00
|
|
|
what: 'popupPanelData',
|
|
|
|
origin: url.origin,
|
2022-10-10 10:28:24 -06:00
|
|
|
hostname: normalizedHostname(tabHostname),
|
2022-09-15 11:14:08 -06:00
|
|
|
});
|
2022-10-10 10:28:24 -06:00
|
|
|
if ( response instanceof Object ) {
|
|
|
|
Object.assign(popupPanelData, response);
|
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
setFilteringMode(popupPanelData.level);
|
2022-09-15 11:14:08 -06:00
|
|
|
|
2022-11-12 07:51:22 -07:00
|
|
|
dom.text('#hostname', tabHostname);
|
2022-09-15 11:14:08 -06:00
|
|
|
|
|
|
|
const parent = qs$('#rulesetStats');
|
|
|
|
for ( const details of popupPanelData.rulesetDetails || [] ) {
|
2022-11-12 07:51:22 -07:00
|
|
|
const div = dom.clone('#templates .rulesetDetails');
|
|
|
|
dom.text(qs$(div, 'h1'), details.name);
|
2022-09-15 11:14:08 -06:00
|
|
|
const { rules, filters, css } = details;
|
2022-10-16 10:05:24 -06:00
|
|
|
let ruleCount = rules.plain + rules.regex;
|
2022-09-29 17:51:33 -06:00
|
|
|
if ( popupPanelData.hasOmnipotence ) {
|
2022-11-05 09:09:06 -06:00
|
|
|
ruleCount += rules.removeparam + rules.redirect + rules.csp;
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
[mv3] Add ability to handle entity-based CSS and scriptlet injection filters
This commit adds the ability to inject entity-based plain CSS
filters and also a set of the most commonly used entity-based
scriptlet injection filters.
Since the scripting API is not compatible with entity patterns,
the entity-related content scripts are injected in all documents
and the entity-matching is done by the content script themselves.
Given this, entity-based content scripts are enabled only when
working in the Complete filtering mode, there won't be any
entity-based filters injected in lower modes.
Also, since there is no way to reasonably have access to the
Public Suffix List in the content scripts, the entity-matching
algorithm is an approximation, though I expect false positives
to be rare (time will tell). In the event of such false
positive, simply falling back to Optimal mode will fix the
issue.
The following issues have been fixed at the same time:
Fixed the no-filtering mode related rules having lower priority
then redirect rules, i.e. redirect rules would still be applied
despite disabling all filtering on a site.
Fixed improper detection of changes to the generic-related CSS
content script, potentially causing undue delays when for example
trying to access the popup panel while working in Complete mode.
The scripting MV3 can be quite slow when registering/updating
large content scripts, so uBOL does its best to call the API only
if really needed, but there had been a regression in the recent
builds preventing uBO from properly detecting unchanged content
script parameters.
2022-10-20 13:48:06 -06:00
|
|
|
let specificCount = 0;
|
|
|
|
if ( css.specific instanceof Object ) {
|
|
|
|
specificCount += css.specific.domainBased;
|
|
|
|
specificCount += css.specific.entityBased;
|
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
dom.text(
|
2022-11-12 07:51:22 -07:00
|
|
|
qs$(div, 'p'),
|
2022-09-15 11:14:08 -06:00
|
|
|
i18n$('perRulesetStats')
|
2022-09-29 17:51:33 -06:00
|
|
|
.replace('{{ruleCount}}', ruleCount.toLocaleString())
|
2022-09-15 11:14:08 -06:00
|
|
|
.replace('{{filterCount}}', filters.accepted.toLocaleString())
|
[mv3] Add ability to handle entity-based CSS and scriptlet injection filters
This commit adds the ability to inject entity-based plain CSS
filters and also a set of the most commonly used entity-based
scriptlet injection filters.
Since the scripting API is not compatible with entity patterns,
the entity-related content scripts are injected in all documents
and the entity-matching is done by the content script themselves.
Given this, entity-based content scripts are enabled only when
working in the Complete filtering mode, there won't be any
entity-based filters injected in lower modes.
Also, since there is no way to reasonably have access to the
Public Suffix List in the content scripts, the entity-matching
algorithm is an approximation, though I expect false positives
to be rare (time will tell). In the event of such false
positive, simply falling back to Optimal mode will fix the
issue.
The following issues have been fixed at the same time:
Fixed the no-filtering mode related rules having lower priority
then redirect rules, i.e. redirect rules would still be applied
despite disabling all filtering on a site.
Fixed improper detection of changes to the generic-related CSS
content script, potentially causing undue delays when for example
trying to access the popup panel while working in Complete mode.
The scripting MV3 can be quite slow when registering/updating
large content scripts, so uBOL does its best to call the API only
if really needed, but there had been a regression in the recent
builds preventing uBO from properly detecting unchanged content
script parameters.
2022-10-20 13:48:06 -06:00
|
|
|
.replace('{{cssSpecificCount}}', specificCount.toLocaleString())
|
2022-09-15 11:14:08 -06:00
|
|
|
);
|
|
|
|
parent.append(div);
|
|
|
|
}
|
|
|
|
|
|
|
|
dom.cl.remove(dom.body, 'loading');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function tryInit() {
|
|
|
|
try {
|
|
|
|
await init();
|
|
|
|
} catch(ex) {
|
|
|
|
setTimeout(tryInit, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tryInit();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|