Skip to content

Commit

Permalink
reorder functions in acis scriptlet for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
AitakattaSora committed Jan 27, 2025
1 parent 5a25958 commit b8749d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion internal/scriptlet/bundle.js

Large diffs are not rendered by default.

78 changes: 39 additions & 39 deletions scriptlets/src/abort-current-inline-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@ import { parseRegexpFromString, parseRegexpLiteral } from './helpers/parseRegexp

const logger = createLogger('abort-current-inline-script');

export function abortCurrentInlineScript(property: string, search?: string | null): void {
if (typeof property !== 'string' || property.length === 0) {
logger.warn('property should be a non-empty string');
return;
}

let searchRe: RegExp | null;
if (typeof search === 'string' && search.length > 0) {
searchRe = parseRegexpLiteral(search) || parseRegexpFromString(search);
}
const rid = generateRandomId();
const currentScript = document.currentScript;

const abort = () => {
const element = document.currentScript;
if (
element instanceof HTMLScriptElement &&
element !== currentScript &&
(!searchRe || searchRe.test(element.textContent || ''))
) {
logger.info(`Blocked ${property} in currentScript`);
throw new ReferenceError(`Aborted script with ID: ${rid}`);
}
};

defineProxyChain(window, property, abort);

// Enhance error handling for the thrown ReferenceError
window.addEventListener('error', (event) => {
if (event.error instanceof ReferenceError && event.error.message.includes(rid)) {
event.preventDefault();
}
});
}

function generateRandomId(): string {
return Math.random().toString(36).substring(2, 15);
}

type AnyObject = { [key: string]: any };

function defineProxyChain(root: AnyObject, chain: string, callbackFn: () => void): void {
Expand Down Expand Up @@ -109,42 +148,3 @@ function isPropertyConfigurable(o: AnyObject, prop: string): boolean {

return Boolean(descriptor.configurable);
}

export function abortCurrentInlineScript(property: string, search?: string | null): void {
if (typeof property !== 'string' || property.length === 0) {
logger.warn('property should be a non-empty string');
return;
}

let searchRe: RegExp | null;
if (typeof search === 'string' && search.length > 0) {
searchRe = parseRegexpLiteral(search) || parseRegexpFromString(search);
}
const rid = generateRandomId();
const currentScript = document.currentScript;

const abort = () => {
const element = document.currentScript;
if (
element instanceof HTMLScriptElement &&
element !== currentScript &&
(!searchRe || searchRe.test(element.textContent || ''))
) {
logger.info(`Blocked ${property} in currentScript`);
throw new ReferenceError(`Aborted script with ID: ${rid}`);
}
};

defineProxyChain(window, property, abort);

// Enhance error handling for the thrown ReferenceError
window.addEventListener('error', (event) => {
if (event.error instanceof ReferenceError && event.error.message.includes(rid)) {
event.preventDefault();
}
});
}

function generateRandomId(): string {
return Math.random().toString(36).substring(2, 15);
}

0 comments on commit b8749d4

Please sign in to comment.