Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private onFrame = async (frame: puppeteer.Frame): Promise => {
// DOM features
const { ids, hrefs, classes } = await frame.$$eval(
'[id],[class],[href]',
extractFeaturesFromDOM,
);
// Source features
const url = frame.url();
const parsed = parse(url);
const hostname = parsed.hostname || '';
const domain = parsed.domain || '';
// Get cosmetics to inject into the Frame
const { active, scripts, styles } = this.getCosmeticsFilters({
domain,
hostname,
url,
// DOM information
classes,
hrefs,
ids,
});
// Abort if cosmetics are disabled
public static fromRawDetails({
requestId = '0',
tabId = 0,
url = '',
hostname,
domain,
sourceUrl = '',
sourceHostname,
sourceDomain,
type = 'main_frame',
}: Partial): Request {
url = url.toLowerCase();
if (hostname === undefined || domain === undefined) {
const parsed = parse(url, TLDTS_OPTIONS);
hostname = hostname || parsed.hostname || '';
domain = domain || parsed.domain || '';
}
// Initialize source URL
if (sourceHostname === undefined || sourceDomain === undefined) {
const parsed = parse(sourceUrl, TLDTS_OPTIONS);
sourceHostname = sourceHostname || parsed.hostname || '';
sourceDomain = sourceDomain || parsed.domain || '';
}
// source URL
return new Request({
requestId,
tabId,
private onRuntimeMessage = (
msg: IBackgroundCallback & { action?: string },
sender: chrome.runtime.MessageSender,
sendResponse: (response?: any) => void,
): void => {
if (sender.tab === undefined || sender.tab.id === undefined || sender.frameId === undefined) {
return;
}
// Make sure we only listen to messages coming from our content-script
// based on the value of `action`.
if (msg.action === 'getCosmeticsFilters') {
// Extract hostname from sender's URL
const { url = '', frameId } = sender;
const parsed = parse(url);
const hostname = parsed.hostname || '';
const domain = parsed.domain || '';
// Once per tab/page load we inject base stylesheets. These are always
// the same for all frames of a given page because they do not depend on
// a particular domain and cannot be cancelled using unhide rules.
// Because of this, we specify `allFrames: true` when injecting them so
// that we do not need to perform this operation for sub-frames.
if (frameId === 0 && msg.lifecycle === 'start') {
const { active, styles } = this.getCosmeticsFilters({
domain,
hostname,
url,
classes: msg.classes,
hrefs: msg.hrefs,
sourceUrl = '',
sourceHostname,
sourceDomain,
type = 'main_frame',
}: Partial): Request {
url = url.toLowerCase();
if (hostname === undefined || domain === undefined) {
const parsed = parse(url, TLDTS_OPTIONS);
hostname = hostname || parsed.hostname || '';
domain = domain || parsed.domain || '';
}
// Initialize source URL
if (sourceHostname === undefined || sourceDomain === undefined) {
const parsed = parse(sourceUrl, TLDTS_OPTIONS);
sourceHostname = sourceHostname || parsed.hostname || '';
sourceDomain = sourceDomain || parsed.domain || '';
}
// source URL
return new Request({
requestId,
tabId,
domain,
hostname,
url,
sourceDomain,
sourceHostname,
sourceUrl,
private onGetCosmeticFilters = (
event: Electron.IpcMainEvent,
url: string,
msg: IBackgroundCallback,
): void => {
// Extract hostname from sender's URL
const parsed = parse(url);
const hostname = parsed.hostname || '';
const domain = parsed.domain || '';
const { active, styles, scripts } = this.getCosmeticsFilters({
domain,
hostname,
url,
classes: msg.classes,
hrefs: msg.hrefs,
ids: msg.ids,
// This needs to be done only once per frame
getBaseRules: msg.lifecycle === 'start',
getInjectionRules: msg.lifecycle === 'start',
getRulesFromHostname: msg.lifecycle === 'start',
match({ url, frameUrl }) {
return parse(url) && parse(frameUrl);
}
};
get domainInfo() {
if (!this._domainInfo) {
this._domainInfo = parse(this.hostname, {
extractHostname: false,
});
}
return this._domainInfo;
}