mirror of
https://github.com/bytedream/stream-bypass.git
synced 2026-08-04 21:10:40 +02:00
re-inject content script on 3XX redirects
This commit is contained in:
@@ -2,6 +2,30 @@ import { BackgroundMessageData, BackgroundMessageType } from '@/lib/communicatio
|
|||||||
import { getHost } from '@/lib/host';
|
import { getHost } from '@/lib/host';
|
||||||
import { HostSettings, UrlReferer } from '@/lib/settings';
|
import { HostSettings, UrlReferer } from '@/lib/settings';
|
||||||
|
|
||||||
|
async function reInjectContentScript(domain: string) {
|
||||||
|
const contentScript: Browser.scripting.RegisteredContentScript = {
|
||||||
|
id: 'temporary-hosts',
|
||||||
|
js: ['content-scripts/content.js'],
|
||||||
|
matches: [`*://${domain}/*`],
|
||||||
|
persistAcrossSessions: false,
|
||||||
|
allFrames: true,
|
||||||
|
runAt: 'document_end'
|
||||||
|
};
|
||||||
|
|
||||||
|
const contentScripts = await browser.scripting.getRegisteredContentScripts({ ids: [contentScript.id] });
|
||||||
|
if (contentScripts.length !== 0) {
|
||||||
|
const registeredContentScript = contentScripts[0];
|
||||||
|
|
||||||
|
// do not double register for same domain
|
||||||
|
if (registeredContentScript.matches!.indexOf(contentScript.matches![0]) !== -1) return;
|
||||||
|
|
||||||
|
contentScript.matches!.push(...contentScripts[0].matches!);
|
||||||
|
await browser.scripting.updateContentScripts([contentScript]);
|
||||||
|
} else {
|
||||||
|
await browser.scripting.registerContentScripts([contentScript]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default defineBackground(() => {
|
export default defineBackground(() => {
|
||||||
browser.runtime.onMessage.addListener(async (message, sender) => {
|
browser.runtime.onMessage.addListener(async (message, sender) => {
|
||||||
const type = message.type as BackgroundMessageType;
|
const type = message.type as BackgroundMessageType;
|
||||||
@@ -25,28 +49,7 @@ export default defineBackground(() => {
|
|||||||
if (import.meta.env.MANIFEST_VERSION === 3) break;
|
if (import.meta.env.MANIFEST_VERSION === 3) break;
|
||||||
|
|
||||||
data = data as BackgroundMessageData<typeof type>;
|
data = data as BackgroundMessageData<typeof type>;
|
||||||
|
await reInjectContentScript(data.domain);
|
||||||
const contentScript: Browser.scripting.RegisteredContentScript = {
|
|
||||||
id: 'temporary-hosts',
|
|
||||||
js: ['content-scripts/content.js'],
|
|
||||||
matches: [`*://${data.domain}/*`],
|
|
||||||
persistAcrossSessions: false,
|
|
||||||
allFrames: true,
|
|
||||||
runAt: 'document_end'
|
|
||||||
};
|
|
||||||
|
|
||||||
const contentScripts = await browser.scripting.getRegisteredContentScripts({ ids: [contentScript.id] });
|
|
||||||
if (contentScripts.length !== 0) {
|
|
||||||
const registeredContentScript = contentScripts[0];
|
|
||||||
|
|
||||||
// do not double register for same domain
|
|
||||||
if (registeredContentScript.matches!.indexOf(contentScript.matches![0]) !== -1) break;
|
|
||||||
|
|
||||||
contentScript.matches!.push(...contentScripts[0].matches!);
|
|
||||||
await browser.scripting.updateContentScripts([contentScript]);
|
|
||||||
} else {
|
|
||||||
await browser.scripting.registerContentScripts([contentScript]);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -80,10 +83,17 @@ export default defineBackground(() => {
|
|||||||
|
|
||||||
browser.webRequest.onBeforeRedirect.addListener(
|
browser.webRequest.onBeforeRedirect.addListener(
|
||||||
async (details) => {
|
async (details) => {
|
||||||
const host = await getHost(new URL(details.url).hostname);
|
const domain = new URL(details.url).host;
|
||||||
|
const host = await getHost(domain);
|
||||||
if (!host) return;
|
if (!host) return;
|
||||||
|
|
||||||
await HostSettings.addTemporaryHostDomain(host.id, new URL(details.redirectUrl).hostname);
|
const newDomain = new URL(details.redirectUrl).host;
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
HostSettings.addTemporaryHostDomain(host.id, newDomain, { registerContentScript: false }),
|
||||||
|
// must be called manually, a message sent from a background script is only received on non-background pages
|
||||||
|
reInjectContentScript(newDomain)
|
||||||
|
]);
|
||||||
},
|
},
|
||||||
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] }
|
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] }
|
||||||
);
|
);
|
||||||
|
|||||||
+8
-3
@@ -39,10 +39,15 @@ export class HostSettings {
|
|||||||
/* tmp */
|
/* tmp */
|
||||||
private static temporaryHostDomain = new Setting<Record<string, string>>('local:temporaryHostDomain', {});
|
private static temporaryHostDomain = new Setting<Record<string, string>>('local:temporaryHostDomain', {});
|
||||||
|
|
||||||
static addTemporaryHostDomain = async (hostId: HostId, domain: string) => {
|
static addTemporaryHostDomain = async (
|
||||||
|
hostId: HostId,
|
||||||
|
domain: string,
|
||||||
|
options = {
|
||||||
// only has an effect with mv2
|
// only has an effect with mv2
|
||||||
const backgroundScriptInject =
|
registerContentScript: import.meta.env.MANIFEST_VERSION === 2
|
||||||
import.meta.env.MANIFEST_VERSION === 2
|
}
|
||||||
|
) => {
|
||||||
|
const backgroundScriptInject = options.registerContentScript
|
||||||
? sendBackgroundMessage(BackgroundMessageType.RegisterContentScript, { domain: domain })
|
? sendBackgroundMessage(BackgroundMessageType.RegisterContentScript, { domain: domain })
|
||||||
: Promise.resolve();
|
: Promise.resolve();
|
||||||
const temporaryHostDomainUpdate = this.temporaryHostDomain.update((val) => {
|
const temporaryHostDomainUpdate = this.temporaryHostDomain.update((val) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user