Skip to content

Commit

Permalink
feat: access missing navigator properties (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
franpeza authored Jan 24, 2024
1 parent 5d32454 commit 486b5a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/lib/web-worker/worker-navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { debug } from '../utils';
import { logWorker } from '../log';
import { resolveUrl } from './worker-exec';
import { webWorkerCtx } from './worker-constants';
import { getter } from './worker-proxy';

export const createNavigator = (env: WebWorkerEnvironment) => {
let key: any;
let nav: any = {
const nav: any = {
sendBeacon: (url: string, body?: any) => {
if (debug && webWorkerCtx.$config$.logSendBeaconRequests) {
try {
Expand Down Expand Up @@ -34,7 +34,7 @@ export const createNavigator = (env: WebWorkerEnvironment) => {
},
};

for (key in navigator) {
for (let key in navigator) {
nav[key] = (navigator as any)[key];
}

Expand All @@ -43,5 +43,12 @@ export const createNavigator = (env: WebWorkerEnvironment) => {
(navigator as any)[propName] = propValue;
return true;
},
get(target, prop) {
if (Object.prototype.hasOwnProperty.call(target, prop)) {
return target[prop];
}
const value = getter(env.$window$, ['navigator', prop]);
return value;
},
});
};
25 changes: 25 additions & 0 deletions tests/platform/navigator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ <h1>Navigator</h1>
})();
</script>
</li>
<li>
<strong>Properties defined in global navigator but not in the worker instance</strong>
<code id="testMainNavigatorProperties"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testMainNavigatorProperties');
navigator.undefinedProp = undefined;
navigator.nullProp = null;
navigator.falseProp = false;
navigator.emptyStringProp = "";
elm.textContent = JSON.stringify({
javaEnabled: navigator.javaEnabled(),
cookieEnabled: navigator.cookieEnabled,
undefinedProp: navigator.undefinedProp ? 'is_defined' : 'is_undefined',
nullProp: navigator.nullProp,
falseProp: navigator.falseProp,
emptyStringProp: navigator.emptyStringProp,
newProperty: navigator.newProperty ? 'is_defined' : 'is_undefined',
pdfViewerEnabled: typeof navigator.pdfViewerEnabled === 'boolean' ? 'available' : 'undefined',
plugins: typeof navigator.plugins.length === 'number' ? 'available' : 'undefined',
});
elm.className = 'testMainNavigatorProperties';
})();
</script>
</li>
</ul>

<hr />
Expand Down
6 changes: 6 additions & 0 deletions tests/platform/navigator/navigator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ test('navigator', async ({ page }) => {
await page.waitForSelector('.testNavKey');
const testNavKey = page.locator('#testNavKey');
await expect(testNavKey).toContainText('5');

await page.waitForSelector('.testMainNavigatorProperties');
const testMainNavigatorProperties = page.locator('#testMainNavigatorProperties');
await expect(testMainNavigatorProperties).toContainText(
'{"javaEnabled":false,"cookieEnabled":true,"undefinedProp":"is_undefined","nullProp":null,"falseProp":false,"emptyStringProp":"","newProperty":"is_undefined","pdfViewerEnabled":"available","plugins":"available"}'
);
});

1 comment on commit 486b5a9

@vercel
Copy link

@vercel vercel bot commented on 486b5a9 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.