Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bg/tabEvents): don't clear sessions on iframe navigation #831

Merged
merged 11 commits into from
Jan 15, 2025
3 changes: 3 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"noExplicitAny": "off", // TODO: turn on, too many cases
"noConsole": "warn"
},
"correctness": {
"noUnusedVariables": "warn"
},
"a11y": {
"noSvgWithoutTitle": "off"
}
Expand Down
2 changes: 1 addition & 1 deletion src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class TabEvents {
* if loading and no url -> clear all sessions but not the overpaying state
* if loading and url -> we need to check if state keys include this url.
*/
if (changeInfo.status === 'loading') {
if (changeInfo.status === 'loading' && changeInfo.url) {
Copy link
Member

Choose a reason for hiding this comment

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

Just so I can understand, when clicking in an iframe, onUpdated is triggered?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not clicking, but if iframe navigates. In case of 1password, clicking ❌ did an internal navigation (URL change for iframe) and then closed itself.

const url = tab.url ? removeQueryParams(tab.url) : '';
const clearOverpaying = this.tabState.shouldClearOverpaying(tabId, url);

Expand Down
7 changes: 6 additions & 1 deletion src/pages/popup/components/NotMonetized.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export const NotMonetized = ({ text }: { text: string }) => {
<div className="flex-shrink-0">
<WarningSign className="size-10 text-medium" />
</div>
<h3 className="text-center text-lg leading-tight text-medium">{text}</h3>
<h3
className="text-center text-lg leading-tight text-medium"
data-testid="not-monetized-message"
>
{text}
</h3>
</div>
);
};
138 changes: 138 additions & 0 deletions tests/e2e/special.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { spy } from 'tinyspy';
import { test, expect } from './fixtures/connected';

test('iframe add/remove does not de-monetize main page', async ({
page,
popup,
}) => {
const walletAddressUrl = process.env.TEST_WALLET_ADDRESS_URL;
const playgroundUrl = 'https://webmonetization.org/play/';

await test.step('prepare', async () => {
await expect(popup.getByTestId('not-monetized-message')).toBeVisible();

await page.goto(playgroundUrl);

const monetizationCallback = spy<[Event], void>();
await page.exposeFunction('monetizationCallback', monetizationCallback);
await page.evaluate(() => {
window.addEventListener('monetization', monetizationCallback);
});

await page
.getByLabel('Wallet address/Payment pointer')
.fill(walletAddressUrl);
await page.getByRole('button', { name: 'Add monetization link' }).click();

await page.$eval(
'link[rel="monetization"]',
(el) => new Promise((res) => el.addEventListener('load', res)),
);

await page.waitForTimeout(2000);
sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
await expect(monetizationCallback).toHaveBeenCalledTimes(1);
await expect(monetizationCallback).toHaveBeenLastCalledWithMatching({
paymentPointer: walletAddressUrl,
});

await expect(popup.getByTestId('home-page')).toBeVisible();

return monetizationCallback;
});

await test.step('add iframe', async () => {
await page.evaluate(() => {
return new Promise((resolve) => {
const iframe = document.createElement('iframe');
iframe.id = 'cross-origin-iframe';
iframe.src = 'https://example.com';
document.body.prepend(iframe);
iframe.addEventListener('load', resolve);
});
});

await expect(page.locator('#cross-origin-iframe')).toBeVisible();
await expect(popup.getByTestId('home-page')).toBeVisible();
});

await test.step('remove iframe', async () => {
await page.evaluate(() => {
const iframe = document.getElementById('cross-origin-iframe');
iframe?.remove();
});
await expect(page.locator('#cross-origin-iframe')).not.toBeAttached();

await expect(popup.getByTestId('not-monetized-message')).not.toBeVisible();
await expect(popup.getByTestId('home-page')).toBeVisible();
});
});

test('iframe navigate does not de-monetize main page', async ({
page,
popup,
}) => {
const walletAddressUrl = process.env.TEST_WALLET_ADDRESS_URL;
const playgroundUrl = 'https://webmonetization.org/play/';

await test.step('prepare', async () => {
await expect(popup.getByTestId('not-monetized-message')).toBeVisible();

await page.goto(playgroundUrl);

const monetizationCallback = spy<[Event], void>();
await page.exposeFunction('monetizationCallback', monetizationCallback);
await page.evaluate(() => {
window.addEventListener('monetization', monetizationCallback);
});

await page
.getByLabel('Wallet address/Payment pointer')
.fill(walletAddressUrl);
await page.getByRole('button', { name: 'Add monetization link' }).click();

await page.$eval(
'link[rel="monetization"]',
(el) => new Promise((res) => el.addEventListener('load', res)),
);

await page.waitForTimeout(2000);
await expect(monetizationCallback).toHaveBeenCalledTimes(1);
await expect(monetizationCallback).toHaveBeenLastCalledWithMatching({
paymentPointer: walletAddressUrl,
});

await expect(popup.getByTestId('home-page')).toBeVisible();

return monetizationCallback;
});

await test.step('add iframe', async () => {
await page.evaluate(() => {
return new Promise((resolve) => {
const iframe = document.createElement('iframe');
iframe.id = 'cross-origin-iframe';
iframe.src = 'https://example.com';
document.body.prepend(iframe);
iframe.addEventListener('load', resolve, { once: true });
});
});
});

await test.step('navigate iframe', async () => {
await page.evaluate(() => {
return new Promise((resolve, reject) => {
const iframe = document.getElementById('cross-origin-iframe');
if (!iframe) {
reject(new Error('iframe not found'));
return;
}
iframe.addEventListener('load', resolve, { once: true });
iframe.addEventListener('error', resolve, { once: true });
iframe.setAttribute('src', 'https://example.net');
});
});

await expect(popup.getByTestId('not-monetized-message')).not.toBeVisible();
await expect(popup.getByTestId('home-page')).toBeVisible();
});
});