Skip to content

Commit

Permalink
fix: polyfills adding incorrect data
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Dec 6, 2024
1 parent 3988e0e commit 824da44
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 49 deletions.
5 changes: 5 additions & 0 deletions browser-emulator-builder/lib/json-creators/DomPolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ function filterNotSupportedByPolyfillPlugin(
return true;
}

const languages = ['window.navigator.languages', 'window.navigator.language'];
if (languages.includes(path)) {
return true;
}

// Currently we don't support creating otherInvocations with new()
if (propertyName.includes('_$otherInvocation') && propertyName.includes('new()')) {
return true;
Expand Down
25 changes: 13 additions & 12 deletions browser-emulator-builder/scripts/generateEmulatorData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async function generate(): Promise<void> {
(a, b) => Number(a.split('-').slice(1).join('.')) - Number(b.split('-').slice(1).join('.')),
)) {
if (!browserId.startsWith('chrome') && !browserId.startsWith('safari')) continue;
if (process.env.BROWSER_ID && !browserId.includes(process.env.BROWSER_ID)) continue;

const browserEngineId = EmulatorData.extractBrowserEngineId(browserId);
const browserEngineOption = chromeEngines.find(x => x.id === browserEngineId);
Expand All @@ -60,23 +61,23 @@ async function generate(): Promise<void> {
new UserAgentHintsJson(config, userAgentIds).save(browserDir);

if (config.browserEngineOption) {
console.log('- Codecs');
new CodecsJson(config, userAgentIds).save(browserDir);
console.log('- Codecs');
new CodecsJson(config, userAgentIds).save(browserDir);

console.log('- Speech');
new SpeechSynthesisJson(config, userAgentIds).save(browserDir);
console.log('- Speech');
new SpeechSynthesisJson(config, userAgentIds).save(browserDir);

console.log('- Fonts');
new FontsJson(config, userAgentIds).save(browserDir);
console.log('- Fonts');
new FontsJson(config, userAgentIds).save(browserDir);

console.log('- WindowChrome');
new WindowChromeJson(config, userAgentIds).save(browserDir);
console.log('- WindowChrome');
new WindowChromeJson(config, userAgentIds).save(browserDir);

console.log('- WindowFraming');
new WindowFramingJson(config, userAgentIds).save(browserDir);
console.log('- WindowFraming');
new WindowFramingJson(config, userAgentIds).save(browserDir);

console.log('- WindowNavigator');
new WindowNavigatorJson(config, userAgentIds).save(browserDir);
console.log('- WindowNavigator');
new WindowNavigatorJson(config, userAgentIds).save(browserDir);

const hasAllPolyfills = DomPolyfillJson.hasAllDomPolyfills(
browserId,
Expand Down
35 changes: 30 additions & 5 deletions browser-profiler/dom-bridger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const devtoolsIndicators = BrowserProfiler.loadDataFile<IBridgeDefinitions>(
const headlessIndicators = BrowserProfiler.loadDataFile<IBridgeDefinitions>(
'dom-bridges/path-patterns/headless-indicators.json',
);
const browserstackIndicators = BrowserProfiler.loadDataFile<IBridgeDefinitions>(
'dom-bridges/path-patterns/browserstack-indicators.json',
);

export default class DomBridger {
public static removeDevtoolsFromPolyfill(polyfill: IDomPolyfill): void {
Expand All @@ -35,13 +38,23 @@ export default class DomBridger {
injectDevtoolsIndicatorPolyfills(polyfill);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public static removeBrowserstackFromPolyfill(polyfill: IDomPolyfill): void {
// nothing to remove outside navigator, which is already being cut
polyfill.add = polyfill.add.filter(x => {
return (
!browserstackIndicators.added.some(pattern => pathIsPatternMatch(x.path, pattern)) &&
!browserstackIndicators.extraAdded.some(pattern => pathIsPatternMatch(x.path, pattern))
);
});
polyfill.modify = polyfill.modify.filter(x => {
return (
!browserstackIndicators.changed.some(pattern => pathIsPatternMatch(x.path, pattern)) &&
!browserstackIndicators.extraChanged.some(pattern => pathIsPatternMatch(x.path, pattern))
);
});
}

public static removeHeadlessFromPolyfill(polyfill: IDomPolyfill): void {
polyfill.add = polyfill.add.filter(x => !isHeadlessIndicator(x.path, x.propertyName));
polyfill.add = polyfill.add.filter(x => !isHeadlessAdded(x.path, x.propertyName));

polyfill.remove = injectHeadlessIndicatorsToRemove(polyfill.remove);
}
Expand All @@ -50,6 +63,9 @@ export default class DomBridger {
polyfill.modify = polyfill.modify.filter(x => !isVariationChange(x.path, x.propertyName));
polyfill.remove = polyfill.remove.filter(x => !isVariationChange(x.path, x.propertyName));
polyfill.add = polyfill.add.filter(x => !isVariationChange(x.path, x.propertyName));
polyfill.reorder = polyfill.reorder.filter(
x => !isVariationChange(x.path, x.propertyName) && !isVariationChange(x.path, x.prevProperty),
);
}

public static removeCustomCallbackFromPolyfill(
Expand All @@ -75,6 +91,14 @@ function isVariationChange(path: string, propertyName: string): boolean {
return true;
if (windowVariations.changed.some(pattern => pathIsPatternMatch(path, pattern))) return true;
if (windowVariations.extraChanged.some(pattern => pathIsPatternMatch(path, pattern))) return true;
if (devtoolsIndicators.changed.some(pattern => pathIsPatternMatch(path, pattern))) return true;
if (devtoolsIndicators.extraChanged.some(pattern => pathIsPatternMatch(path, pattern)))
return true;
if (browserstackIndicators.changed.some(pattern => pathIsPatternMatch(path, pattern)))
return true;
if (browserstackIndicators.extraChanged.some(pattern => pathIsPatternMatch(path, pattern)))
return true;

return false;
}

Expand All @@ -91,9 +115,10 @@ function isDevtoolsIndicator(path: string, propertyName: string): boolean {
return false;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function isHeadlessIndicator(path: string, propertyName: string): boolean {
function isHeadlessAdded(path: string, _propertyName: string): boolean {
if (headlessIndicators.added.some(pattern => pathIsPatternMatch(path, pattern))) return true;
if (headlessIndicators.extraAdded.some(pattern => pathIsPatternMatch(path, pattern))) return true;

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ export default class BrowserstackIndicatorExtractor extends BaseExtractor {
public static override definitePatterns = [
'window.chrome.runtime',
'window.HTMLAnchorElement.prototype.hrefTranslate',
'window.GPU.prototype.requestAdapter',
'window.navigator.gpu.requestAdapter',
];

public static override extraAddPatterns = [];
public static override extraChangePatterns = [];
public static override extraAddPatterns = ['window.navigator.languages'];
public static override extraChangePatterns = [
'window.navigator.languages.length',
'window.GPU.prototype.requestAdapter',
'window.navigator.gpu.requestAdapter',
];

public static override ignoredExtraPatterns = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import BaseExtractor from './BaseExtractor';

export default class HeadlessIndicatorExtractor extends BaseExtractor {
public static override definitePatterns = [
'window.navigator.languages',
'window.navigator.plugins',
'window.navigator.mimeTypes',
'window.HTMLLinkElement.prototype.import',
];
public static override definitePatterns = ['window.HTMLLinkElement.prototype.import'];

public static override extraAddPatterns = [];
public static override extraChangePatterns = [];

public static override ignoredExtraPatterns = [];

public static override regexps = [
/window.HTMLLinkElement.prototype.import/
];
public static override regexps = [/window.HTMLLinkElement.prototype.import/];
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,41 @@ export default class InstanceChangeExtractor extends BaseExtractor {
'window.navigator.requestMIDIAccess',
'window.navigator.clipboard.readText',
'window.Element.prototype.requestPointerLock',
'window.Animation.new().finished.startTime'
'window.Animation.new().finished.startTime',
];

public static override extraAddPatterns = [];

// NOTE: instance to instance changes won't always include the things you're looking for.
// For instance, if languages are different from browser stack to local, it won't show up here since those are instances of the same machine
public static override extraChangePatterns = [
'window.Intl.DateTimeFormat.new().resolvedOptions',
'window.Intl.DateTimeFormat.new().format',
'window.console.memory.usedJSHeapSize',
'window.BaseAudioContext.prototype.state',
'window.BaseAudioContext.prototype.onstatechange',
'window.AudioContext.new().destination.context.state',
'window.AudioContext.new().destination.context.onstatechange',
'window.AudioContext.new().destination.context.baseLatency',
'window.AudioContext.new().destination.context.currentTime',
'window.AudioContext.new().state',
'window.AudioContext.new().onstatechange',
'window.document.readyState',
'window.AudioContext.new().baseLatency',
'window.AudioContext.new().currentTime',
'window.BarcodeDetector.getSupportedFormats',
'window.BaseAudioContext.prototype.state',
'window.BaseAudioContext.prototype.currentTime',
'window.BaseAudioContext.prototype.onstatechange',
'window.Intl.DateTimeFormat.new().resolvedOptions',
'window.Intl.DateTimeFormat.new().format',
'window.ScrollTimeline',
'window.console.memory.usedJSHeapSize',
'window.console.memory.jsHeapSizeLimit',
'window.navigator.connection',
'window.navigator.connection.onchange',
'window.navigator.connection.effectiveType',
'window.navigator.connection.saveData',
'window.document.readyState',
'window.document.styleSheets',
'window.document.scripts',
'window.console.memory.jsHeapSizeLimit',
'window.document.fonts.ready',
'window.document.visibilityState',
'window.document.webkitVisibilityState',
'window.navigator.connection',
'window.BarcodeDetector.getSupportedFormats',
'window.BaseAudioContext.prototype.currentTime',
'window.AudioContext.new().baseLatency',
'window.AudioContext.new().destination.context.baseLatency',
'window.AudioContext.new().destination.context.currentTime',
'window.AudioContext.new().currentTime',
'window.document.fonts.ready',
'window.ScrollTimeline',
];

public static override ignoredExtraPatterns = [];
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1938,10 +1938,10 @@
dependencies:
"@ulixee/js-path" "^2.0.0-alpha.18"

"@ulixee/chrome-130-0@^6723.92.11":
version "6723.92.11"
resolved "https://registry.yarnpkg.com/@ulixee/chrome-130-0/-/chrome-130-0-6723.92.11.tgz#d1c11090911817d1838deb65958abc05bc2cfb94"
integrity sha512-omNgBjC4DhFRQq90TmoT0t9CqoNsOEf9DTovdhYGdfp1PZw57zbfFh/sBFI5NVUKpp2JiXugR4m1sd2Gdyi2/Q==
"@ulixee/chrome-131-0@^6778.109.11":
version "6778.109.11"
resolved "https://registry.npmjs.org/@ulixee/chrome-131-0/-/chrome-131-0-6778.109.11.tgz#ca8dc474cd1c7f4e293bac54c0406a9e4a00b1b2"
integrity sha512-2aNYjkm06vaFHKonWg1HxFItbRr1LuTDLMXvHo2D26pWfbFv+meP8PJu5rTIoeBczl2iLou0Q2zABIM1SCoBsA==
dependencies:
"@ulixee/chrome-app" "^1.0.3"

Expand Down

0 comments on commit 824da44

Please sign in to comment.