Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/utils/CoinbasePixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class CoinbasePixel {
*/
private state: 'loading' | 'ready' | 'waiting_for_response' | 'failed' = 'loading';
private debug: boolean;
private hasStorageAccess?: boolean;

private host: string;
private pixelIframe?: HTMLIFrameElement;
Expand Down Expand Up @@ -87,6 +88,13 @@ export class CoinbasePixel {
this.addPixelReadyListener();
this.embedPixel();

// Handle browsers where we need to request access before embedding.
if ('hasStorageAccess' in document) {
document.hasStorageAccess().then((hasAccess) => {
this.hasStorageAccess = hasAccess;
this.log('hasStorageAccess()', hasAccess);
});
}
// Setup a timeout for errors that might stop the window from loading i.e. CSP
setTimeout(() => {
if (this.state !== 'ready') {
Expand Down Expand Up @@ -152,6 +160,18 @@ export class CoinbasePixel {
if (!this.isLoggedIn) {
// Embedded experience opens popup for signin
this.startDirectSignin(openEmbeddedExperience);
} else if (this.hasStorageAccess === false) {
// Explicitly check for false as this means the browser API is available and we have a response.
// Note that this needs to be called in a click handler but the iframe can
// be attached outside of the click context.
document
.requestStorageAccess()
.then(openEmbeddedExperience)
.catch((err) => {
this.state = 'failed';
console.error(err);
throw new Error('Error obtaining store access');
});
} else {
openEmbeddedExperience();
}
Expand Down