Skip to content

Commit

Permalink
fix #1891: preload using nav destination url
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jan 17, 2025
1 parent 99a0e86 commit 7def10e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/background/style-manager/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IMPORT_THROTTLE, k_size, kUrl, UCD} from '@/js/consts';
import {IMPORT_THROTTLE, k_size, kStyleViaXhr, kUrl, UCD} from '@/js/consts';
import * as prefs from '@/js/prefs';
import {calcStyleDigest, styleCodeEmpty} from '@/js/sections-util';
import {calcObjSize, mapObj} from '@/js/util';
Expand Down Expand Up @@ -243,14 +243,18 @@ export function getSectionsByUrl(url, id, isInitialApply) {
if (isInitialApply === 'cfg') {
return {cfg};
}
if (frameId === 0) {
let res, cache;
if (frameId === 0
&& isInitialApply !== kStyleViaXhr
&& (res = td[kUrl])
&& (res = res[0]) !== url
&& res.split('#', 1)[0] === url.split('#', 1)[0]) {
/* Chrome hides text frament from location.href of the page e.g. #:~:text=foo
so we'll use the real URL reported by webNavigation API.
TODO: if FF will do the same, this won't work as is: FF reports onCommitted too late */
url = td[kUrl]?.[0] || url;
url = res || url;
}
let res;
let cache = styleCache.get(url);
cache = styleCache.get(url);
if (!cache) {
cache = {url, sections: {}};
styleCache.add(cache);
Expand Down
3 changes: 2 additions & 1 deletion src/background/style-via-webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async function prepareStyles(req) {
}
const oldData = toSend[key];
const data = oldData || {};
const payload = data.payload = getSectionsByUrl.call({sender: req}, url, null, true);
const payload = data.payload = getSectionsByUrl.call({sender: req}, url, null, kStyleViaXhr);
const willStyle = payload.sections.length;
data.url = url;
if (oldData) removePreloadedStyles(null, key, data, willStyle);
Expand Down Expand Up @@ -200,6 +200,7 @@ async function prepareStylesMV3(tabId, frameId, url, data, key, payload) {
id: ruleId,
condition: {
tabIds: [tabId],
urlFilter: '|' + url + '|',
resourceTypes: [frameId ? kSubFrame : kMainFrame],
// Forcing the rule to be evaluated later, when response headers are received.
excludedResponseHeaders: [{header: kSetCookie, values: [cookie]}],
Expand Down

0 comments on commit 7def10e

Please sign in to comment.