Skip to content

Commit

Permalink
fix: headless fixes only in headless browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed Nov 21, 2022
1 parent ccf4c6f commit 94435cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ jobs:
if: steps.changed_packages.outputs.changed_packages != '0'
run: npm run build

# - name: Create a GH release from tag
# uses: "marvinpinto/action-automatic-releases@latest"
# with:
# repo_token: "${{ secrets.GH_TOKEN }}"
# prerelease: true
- name: Create a GH release from tag
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GH_TOKEN }}"
prerelease: true

- name: Release dev version for testing
if: steps.changed_packages.outputs.changed_packages != '0'
Expand Down
16 changes: 10 additions & 6 deletions packages/fingerprint-injector/src/fingerprint-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ export class FingerprintInjector {

/**
* Adds init script to the browser context, so the fingerprint is changed before every document creation.
* DISCLAIMER: Since the playwright does not support changing viewport and User-agent after the context is created,
* you have to set it manually when the context is created. Check the playwright usage example.
* DISCLAIMER: Since Playwright does not support changing viewport and `user-agent` after the context is created,
* you have to set it manually when the context is created. Check the Playwright usage example for more details.
* @param browserContext Playwright browser context to be injected with the fingerprint.
* @param fingerprint Fingerprint from [`fingerprint-generator`](https://github.com/apify/fingerprint-generator).
* @param fingerprint Browser fingerprint from [`fingerprint-generator`](https://github.com/apify/fingerprint-generator).
*/
async attachFingerprintToPlaywright(browserContext: BrowserContext, browserFingerprintWithHeaders: BrowserFingerprintWithHeaders): Promise<void> {
const { fingerprint, headers } = browserFingerprintWithHeaders;
const enhancedFingerprint = this._enhanceFingerprint(fingerprint);

const content = this.getInjectableFingerprintFunction(enhancedFingerprint);
const content = this.getInjectableFingerprintFunction(
enhancedFingerprint,
);

// Override the language properly
await browserContext.setExtraHTTPHeaders({
Expand Down Expand Up @@ -100,7 +102,9 @@ export class FingerprintInjector {
* @param fingerprint Enhanced fingerprint.
* @returns Script overriding browser fingerprint.
*/
private getInjectableFingerprintFunction(fingerprint: EnhancedFingerprint): string {
private getInjectableFingerprintFunction(
fingerprint: EnhancedFingerprint,
): string {
function inject() {
const {
battery,
Expand Down Expand Up @@ -156,7 +160,7 @@ export class FingerprintInjector {
};

runHeadlessFixes();
// override internationalization API

overrideIntlAPI(navigatorProps.language);
overrideStatic();

Expand Down
17 changes: 12 additions & 5 deletions packages/fingerprint-injector/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const isHeadlessChromium = () => /headless/i.test(navigator.userAgent) && navigator.plugins.length === 0;
const isChrome = () => navigator.userAgent.includes("Chrome");
const isFirefox = () => navigator.userAgent.includes("Firefox");
const isSafari = () => navigator.userAgent.includes("Safari") && !navigator.userAgent.includes("Chrome");

// This file contains utils that are build and included on the window object with some randomized prefix.

// some protections can mess with these to prevent the overrides - our script is first so we can reference the old values.
Expand Down Expand Up @@ -454,7 +459,7 @@ function overrideUserAgentData(userAgentData) {
};

function fixWindowChrome(){
if (!window.chrome) {
if( isChrome() && !window.chrome ){
Object.defineProperty(window, 'chrome', {
writable: true,
enumerable: true,
Expand Down Expand Up @@ -622,10 +627,12 @@ function fixPluginArray() {
}

function runHeadlessFixes(){
fixWindowChrome();
fixPermissions();
fixIframeContentWindow();
fixPluginArray();
if( isHeadlessChromium() ){
fixWindowChrome();
fixPermissions();
fixIframeContentWindow();
fixPluginArray();
}
}

function overrideStatic(){
Expand Down

0 comments on commit 94435cc

Please sign in to comment.