@@ -31,37 +31,6 @@ const initOutboundDomains = () => {
3131 }
3232 }
3333
34- function extractUrlFromSrcdoc ( srcdoc ) {
35- if ( ! srcdoc ) return null ;
36-
37- // Look for URLs in the srcdoc content
38- // This is a basic implementation - may need refinement based on actual Cal.com usage
39- const urlRegex = / h t t p s ? : \/ \/ [ ^ \s " ' < > ] + / g;
40- const matches = srcdoc . match ( urlRegex ) ;
41-
42- if ( matches && matches . length > 0 ) {
43- // Return the first URL found - this might need to be more sophisticated
44- // depending on how Cal.com structures their srcdoc content
45- return matches [ 0 ] ;
46- }
47-
48- return null ;
49- }
50-
51- function updateSrcdocWithTracking ( element , originalUrl , newUrl ) {
52- if ( ! element . srcdoc ) return false ;
53-
54- try {
55- // Replace the original URL with the new URL in the srcdoc content
56- const updatedSrcdoc = element . srcdoc . replace ( originalUrl , newUrl ) ;
57- element . srcdoc = updatedSrcdoc ;
58- return true ;
59- } catch ( e ) {
60- console . error ( 'Error updating srcdoc:' , e ) ;
61- return false ;
62- }
63- }
64-
6534 function addOutboundTracking ( clickId ) {
6635 // Handle both string and array configurations for outbound domains
6736 const outboundDomains = Array . isArray ( DOMAINS_CONFIG . outbound )
@@ -78,19 +47,40 @@ const initOutboundDomains = () => {
7847 const existingCookie = clickId || cookieManager . get ( DUB_ID_VAR ) ;
7948 if ( ! existingCookie ) return ;
8049
81- // Get all links and iframes (including those with srcdoc)
82- const elements = document . querySelectorAll (
83- 'a[href], iframe[src], iframe[srcdoc]' ,
84- ) ;
85- if ( ! elements || elements . length === 0 ) return ;
50+ // Get all links and iframes
51+ const elements = document . querySelectorAll ( 'a[href], iframe[src]' ) ;
52+
53+ // Also get nested iframes inside srcdoc iframes
54+ const srcdocIframes = document . querySelectorAll ( 'iframe[srcdoc]' ) ;
55+ const nestedElements = [ ] ;
56+
57+ srcdocIframes . forEach ( ( srcdocIframe ) => {
58+ try {
59+ // Access the content document of the srcdoc iframe
60+ const contentDoc = srcdocIframe . contentDocument ;
61+ if ( contentDoc ) {
62+ // Find iframes and links inside the srcdoc content
63+ const nestedIframes = contentDoc . querySelectorAll ( 'iframe[src]' ) ;
64+ const nestedLinks = contentDoc . querySelectorAll ( 'a[href]' ) ;
65+
66+ nestedElements . push ( ...nestedIframes , ...nestedLinks ) ;
67+ }
68+ } catch ( e ) {
69+ // contentDocument access might fail due to CORS or other security restrictions
70+ console . warn ( 'Could not access contentDocument of srcdoc iframe:' , e ) ;
71+ }
72+ } ) ;
73+
74+ // Combine all elements
75+ const allElements = [ ...elements , ...nestedElements ] ;
76+ if ( ! allElements || allElements . length === 0 ) return ;
8677
87- elements . forEach ( ( element ) => {
78+ allElements . forEach ( ( element ) => {
8879 // Skip already processed elements
8980 if ( outboundLinksUpdated . has ( element ) ) return ;
9081
9182 try {
92- const urlString =
93- element . href || element . src || extractUrlFromSrcdoc ( element . srcdoc ) ;
83+ const urlString = element . href || element . src ;
9484 if ( ! urlString ) return ;
9585
9686 // Check if the URL matches any of our outbound domains
@@ -104,29 +94,15 @@ const initOutboundDomains = () => {
10494 // Only add the tracking parameter if it's not already present
10595 if ( ! url . searchParams . has ( DUB_ID_VAR ) ) {
10696 url . searchParams . set ( DUB_ID_VAR , existingCookie ) ;
107- const newUrlString = url . toString ( ) ;
10897
10998 // Update the appropriate attribute based on element type
11099 if ( element . tagName . toLowerCase ( ) === 'a' ) {
111- element . href = newUrlString ;
112- outboundLinksUpdated . add ( element ) ;
100+ element . href = url . toString ( ) ;
113101 } else if ( element . tagName . toLowerCase ( ) === 'iframe' ) {
114- if ( element . src ) {
115- // Standard iframe with src attribute
116- element . src = newUrlString ;
117- outboundLinksUpdated . add ( element ) ;
118- } else if ( element . srcdoc ) {
119- // Iframe with srcdoc attribute (like Cal.com)
120- const updated = updateSrcdocWithTracking (
121- element ,
122- urlString ,
123- newUrlString ,
124- ) ;
125- if ( updated ) {
126- outboundLinksUpdated . add ( element ) ;
127- }
128- }
102+ element . src = url . toString ( ) ;
129103 }
104+
105+ outboundLinksUpdated . add ( element ) ;
130106 }
131107 } catch ( e ) {
132108 console . error ( 'Error processing element:' , e ) ;
0 commit comments