使用 regexFilter + 替换将原始 URL 附加到扩展页面 URL:
const EXT_PAGE = chrome.runtime.getURL('/forbidden.html');
const RULES = [{
id: 1,
action: {
type: 'redirect',
redirect: { regexSubstitution: EXT_PAGE + '#\\0' },
},
condition: {
requestDomains: ['example.com'], // remove this line to match all sites
regexFilter: '^.+$',
resourceTypes: ['main_frame', 'sub_frame'],
},
}];
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: RULES.map(r => r.id),
addRules: RULES,
});
现在您的扩展页面 (forbidden.html) 可以读取以下 URL:
const origUrl = location.hash.slice(1);
您还可以在地址栏中隐藏原始 URL:
history.replaceState(document.title, null, location.href.split('#')[0]);
希望有更好的解决方案,如果https://crbug.com/1241397 https://crbug.com/1241397已实施。