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

add allowIframe option #592

Merged
merged 2 commits into from
Jun 30, 2021
Merged

add allowIframe option #592

merged 2 commits into from
Jun 30, 2021

Conversation

bachmanity1
Copy link
Contributor

@bachmanity1 bachmanity1 commented Jun 23, 2021

This PR fixes #585
Additional option allows iframes to retain src attribute. This option allong with custom-events can be used to replay content of iframes that is blocked by same-origin-policy such as youtube iframes.
By using below function I was able to replay content from youtube:

function allowIframe (src: string): boolean {
    try {
        const url = new URL(src);
        return url.origin === `https://www.youtube.com`;
    } catch {
        return false;
    }
}  

@Juice10
Copy link
Contributor

Juice10 commented Jun 23, 2021

I believe you are referring to issue #585 not #175

@bachmanity1
Copy link
Contributor Author

Thanks @Juice10, I edited the description.

Copy link
Member

@YunFeng0817 YunFeng0817 left a comment

Choose a reason for hiding this comment

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

Could you please show an example of how to recording YouTube videos with the option "allowIframe"?

@Yuyz0112
Copy link
Member

@Mark-Fenng I think @bachmanity1 means just allow loading YouTube iframe normally during replay, which is out of the control of rrweb's replayer.

@bachmanity1
Copy link
Contributor Author

bachmanity1 commented Jun 24, 2021

yes @Yuyz0112 is right, this option allows to load youtube iframe normally during replay and then using custom-events api we can transmit play, pause and seek events in youtube iframe from recorded page to replayed page.

@YunFeng0817
Copy link
Member

Ok, but from your changes, I can't see how this option allows loading youtube iframe.

@bachmanity1
Copy link
Contributor Author

bachmanity1 commented Jun 24, 2021

@Mark-Fenng here is example how we can record events from youtube iframe, using same logic you can process these events in replayed page

function allowIframe (src: string): boolean {
    try {
        const url = new URL(src);
        return url.origin === `https://www.youtube.com`;
    } catch {
        return false;
    }
}

record({
    emit: (e, c) => {
        // send to backend
    },
    allowIframe,
});

if (!(window as any).YT) {
    const tag = document.createElement(`script`);
    tag.src = `https://www.youtube.com/iframe_api`;
    (window as any).onYouTubeIframeAPIReady = onYTAPIReady;
    const firstScriptTag = document.getElementsByTagName(`script`)[0];
    firstScriptTag?.parentNode?.insertBefore(tag, firstScriptTag);
} else {
    onYTAPIReady();
}

function onYTAPIReady () {
    const onPlayerStateChange = (id: string) => (event: any) => {
        record.addCustomEvent(`stateChange`, {
            id,
            playerInfo: event.target.playerInfo,
        });
    };
    for(const iframe of document.getElementsByTagName(`iframe`)) {
        try {
            const src = (iframe as HTMLIFrameElement).getAttribute(`src`) ?? ``;
            const url = new URL(src);
            if (url.origin !== `https://www.youtube.com`) {
                continue;
            }
            const id = (iframe as HTMLIFrameElement).getAttribute(`id`) ?? ``;
            new (window as any).YT.Player(id, {
                events: {
                    onStateChange: onPlayerStateChange(id),
                },
            });
        } catch {
            continue;
        }
    }
}

@bachmanity1 bachmanity1 reopened this Jun 24, 2021
@Yuyz0112
Copy link
Member

Yuyz0112 commented Jun 24, 2021

Let me take a look at this PR a little more.

Thanks for the code and review @bachmanity1 @Mark-Fenng

@bachmanity1
Copy link
Contributor Author

@Yuyz0112 can you plz merge this before #595?

src/record/index.ts Outdated Show resolved Hide resolved
@Yuyz0112
Copy link
Member

@bachmanity1 Both this PR and the PR in rrweb-snapshot look good to me. The only remaining topic is whether we can call this option allowIframe or keepIframeSrc or something else more concrete?

@YunFeng0817
Copy link
Member

I prefer keepIframeSrc.

@bachmanity1
Copy link
Contributor Author

@Yuyz0112 I renamed allowIframe to keepIframeSrcFn

@Yuyz0112
Copy link
Member

@bachmanity1 Could you upgrade to rrweb-snapshot@1.1.5 in your branch?

@bachmanity1
Copy link
Contributor Author

@Yuyz0112 upgraded.

@Yuyz0112
Copy link
Member

Thanks!

@Yuyz0112 Yuyz0112 merged commit dee0457 into rrweb-io:master Jun 30, 2021
@bachmanity1 bachmanity1 deleted the allow-iframe branch June 30, 2021 04:58
@eoghanmurray
Copy link
Contributor

Is there a record/replay youtube pattern here that can be integrated into core rrweb such that people get youtube replay out of the box?
Could it be added as a recipe? Or as a plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Page containing iframe with youtube video is not replayed
5 participants