[mv3][safari] Workaround for user styles issue with bfcache

Related issue:
https://github.com/uBlockOrigin/uBOL-home/issues/518
This commit is contained in:
Raymond Hill 2025-10-11 14:46:35 -04:00
parent f9bb6d7f7e
commit 22d2ecc472
No known key found for this signature in database
GPG key ID: F5630CAE62A14316
9 changed files with 150 additions and 33 deletions

View file

@ -194,7 +194,7 @@ function registerGeneric(context, genericDetails) {
if ( js.length === 0 ) { return; }
js.unshift('/js/scripting/isolated-api.js');
js.unshift('/js/scripting/css-api.js', '/js/scripting/isolated-api.js');
js.push('/js/scripting/css-generic.js');
const { none, basic, optimal, complete } = filteringModeDetails;
@ -306,7 +306,7 @@ function registerProcedural(context) {
normalizeMatches(matches);
js.unshift('/js/scripting/isolated-api.js');
js.unshift('/js/scripting/css-api.js', '/js/scripting/isolated-api.js');
js.push('/js/scripting/css-procedural.js');
const excludeMatches = [];
@ -373,7 +373,7 @@ function registerSpecific(context) {
normalizeMatches(matches);
js.unshift('/js/scripting/isolated-api.js');
js.unshift('/js/scripting/css-api.js', '/js/scripting/isolated-api.js');
js.push('/js/scripting/css-specific.js');
const excludeMatches = [];

View file

@ -0,0 +1,33 @@
/*******************************************************************************
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
Copyright (C) 2025-present Raymond Hill
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
*/
(api => {
if ( typeof api === 'object' ) { return; }
self.cssAPI = {
insert(css) {
chrome.runtime.sendMessage({
what: 'insertCSS',
css,
}).catch(( ) => {
});
},
};
})(self.cssAPI);

View file

@ -188,7 +188,7 @@ const uBOL_processNodes = ( ) => {
if ( styleSheetTimer !== undefined ) { return; }
styleSheetTimer = self.requestAnimationFrame(( ) => {
styleSheetTimer = undefined;
uBOL_injectCSS(`${styleSheetSelectors.join(',')}{display:none!important;}`);
self.cssAPI.insert(`${styleSheetSelectors.join(',')}{display:none!important;}`);
styleSheetSelectors.length = 0;
});
};
@ -214,16 +214,6 @@ const uBOL_processChanges = mutations => {
/******************************************************************************/
const uBOL_injectCSS = css => {
chrome.runtime.sendMessage({
what: 'insertCSS',
css,
}).catch(( ) => {
});
};
/******************************************************************************/
const stopAll = ( ) => {
if ( domChangeTimer !== undefined ) {
self.clearTimeout(domChangeTimer);

View file

@ -29,14 +29,6 @@ if ( self.ProceduralFiltererAPI !== undefined ) {
/******************************************************************************/
const uBOL_injectCSS = css => {
chrome.runtime.sendMessage({
what: 'insertCSS',
css,
}).catch(( ) => {
});
};
const nonVisualElements = {
head: true,
link: true,
@ -688,7 +680,7 @@ class ProceduralFilterer {
if ( styleToken !== undefined ) { return styleToken; }
styleToken = randomToken();
this.styleTokenMap.set(style, styleToken);
uBOL_injectCSS(`[${styleToken}]\n{${style}}\n`);
self.cssAPI.insert(`[${styleToken}]\n{${style}}\n`);
return styleToken;
}

View file

@ -103,11 +103,7 @@ if ( declaratives.length !== 0 ) {
sheetText.push(ruleText);
}
if ( sheetText.length !== 0 ) {
chrome.runtime.sendMessage({
what: 'insertCSS',
css: sheetText.join('\n'),
}).catch(( ) => {
});
self.cssAPI.insert(sheetText.join('\n'));
}
}

View file

@ -65,11 +65,7 @@ const exceptedSelectors = exceptions.length !== 0
: selectors;
if ( exceptedSelectors.length === 0 ) { return; }
chrome.runtime.sendMessage({
what: 'insertCSS',
css: `${exceptedSelectors.join(',')}{display:none!important;}`,
}).catch(( ) => {
});
self.cssAPI.insert(`${exceptedSelectors.join(',')}{display:none!important;}`);
/******************************************************************************/

View file

@ -0,0 +1,46 @@
/*******************************************************************************
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
Copyright (C) 2025-present Raymond Hill
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
*/
(api => {
if ( typeof api === 'object' ) { return; }
const inserted = new Set();
self.cssAPI = {
insert(css) {
chrome.runtime.sendMessage({
what: 'insertCSS',
css,
}).catch(( ) => {
});
inserted.add(css);
},
};
self.addEventListener('pageshow', ( ) => {
chrome.runtime.sendMessage({
what: 'insertCSS',
css: Array.from(inserted).join('\n'),
}).catch(( ) => {
});
});
})(self.cssAPI);

View file

@ -0,0 +1,62 @@
/*******************************************************************************
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
Copyright (C) 2019-present Raymond Hill
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
*/
(async function uBOL_cssUser() {
/******************************************************************************/
const docURL = new URL(document.baseURI);
const details = await chrome.runtime.sendMessage({
what: 'injectCustomFilters',
hostname: docURL.hostname,
}).catch(( ) => {
});
if ( details?.proceduralSelectors?.length ) {
if ( self.ProceduralFiltererAPI ) {
self.customProceduralFiltererAPI = new self.ProceduralFiltererAPI();
self.customProceduralFiltererAPI.addSelectors(
details.proceduralSelectors.map(a => JSON.parse(a))
);
}
}
const inserted = new Set();
if ( Array.isArray(details?.plainSelectors) ) {
inserted.add(`${details.plainSelectors.join(',\n')}{display:none!important;}`);
}
self.addEventListener('pageshow', ( ) => {
chrome.runtime.sendMessage({
what: 'insertCSS',
css: Array.from(inserted).join('\n'),
}).catch(( ) => {
});
});
self.customFilters = details;
/******************************************************************************/
})();
void 0;

View file

@ -101,6 +101,8 @@ cp platform/mv3/extension/*.json "$UBOL_DIR"/
cp platform/mv3/extension/css/* "$UBOL_DIR"/css/
cp -R platform/mv3/extension/js/* "$UBOL_DIR"/js/
cp platform/mv3/"$PLATFORM"/ext-compat.js "$UBOL_DIR"/js/ 2>/dev/null || :
cp platform/mv3/"$PLATFORM"/css-api.js "$UBOL_DIR"/js/scripting/ 2>/dev/null || :
cp platform/mv3/"$PLATFORM"/css-user.js "$UBOL_DIR"/js/scripting/ 2>/dev/null || :
cp platform/mv3/extension/img/* "$UBOL_DIR"/img/
cp platform/mv3/"$PLATFORM"/img/* "$UBOL_DIR"/img/ 2>/dev/null || :
cp -R platform/mv3/extension/_locales "$UBOL_DIR"/