- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.2k
Make external iframe render work #35730
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
          
     Merged
      
      
    
      
        
          +294
        
        
          −92
        
        
          
        
      
    
  
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f90d3ba
              
                fix external render iframe
              
              
                wxiaoguang 7ac96f3
              
                fix test, fix open link (force push to make backport easier)
              
              
                wxiaoguang 72b3eeb
              
                Merge branch 'main' into fix-render-iframe
              
              
                wxiaoguang 9c74528
              
                fix sandbox (force push to update doc and fix test)
              
              
                wxiaoguang ea28b33
              
                fine tune
              
              
                wxiaoguang 9e94317
              
                new URL
              
              
                wxiaoguang File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /* dummy */ | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import {generateElemId, queryElemChildren} from '../utils/dom.ts'; | ||
| import {isDarkTheme} from '../utils.ts'; | ||
|  | ||
| export async function loadRenderIframeContent(iframe: HTMLIFrameElement) { | ||
| const iframeSrcUrl = iframe.getAttribute('data-src'); | ||
| if (!iframe.id) iframe.id = generateElemId('gitea-iframe-'); | ||
|  | ||
| window.addEventListener('message', (e) => { | ||
| if (!e.data?.giteaIframeCmd || e.data?.giteaIframeId !== iframe.id) return; | ||
| const cmd = e.data.giteaIframeCmd; | ||
| if (cmd === 'resize') { | ||
| iframe.style.height = `${e.data.iframeHeight}px`; | ||
| } else if (cmd === 'open-link') { | ||
| if (e.data.anchorTarget === '_blank') { | ||
| window.open(e.data.openLink, '_blank'); | ||
| } else { | ||
| window.location.href = e.data.openLink; | ||
| } | ||
| } else { | ||
| throw new Error(`Unknown gitea iframe cmd: ${cmd}`); | ||
| } | ||
| }); | ||
|  | ||
| let urlParams = ''; | ||
| urlParams += `&gitea-is-dark-theme=${isDarkTheme()}`; | ||
| urlParams += `&gitea-iframe-id=${iframe.id}`; | ||
| iframe.src = iframeSrcUrl + (iframeSrcUrl.includes('?') ? '&' : '?') + urlParams.substring(1); | ||
|         
                  silverwind marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
|         
                  wxiaoguang marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| export function initMarkupRenderIframe(el: HTMLElement) { | ||
| queryElemChildren(el, 'iframe.external-render-iframe', loadRenderIframeContent); | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* To manually test: | ||
|  | ||
| [markup.in-iframe] | ||
| ENABLED = true | ||
| FILE_EXTENSIONS = .in-iframe | ||
| RENDER_CONTENT_MODE = iframe | ||
| RENDER_COMMAND = `echo '<div style="width: 100%; height: 2000px; border: 10px solid red; box-sizing: border-box;"><a href="/">a link</a> <a target="_blank" href="//gitea.com">external link</a></div>'` | ||
|  | ||
| */ | ||
|  | ||
| function mainExternalRenderIframe() { | ||
| const u = new URL(window.location.href); | ||
| const iframeId = u.searchParams.get('gitea-iframe-id'); | ||
|  | ||
| // iframe is in different origin, so we need to use postMessage to communicate | ||
| const postIframeMsg = (cmd: string, data: Record<string, any> = {}) => { | ||
| window.parent.postMessage({giteaIframeCmd: cmd, giteaIframeId: iframeId, ...data}, '*'); | ||
| }; | ||
|  | ||
| const updateIframeHeight = () => postIframeMsg('resize', {iframeHeight: document.documentElement.scrollHeight}); | ||
| updateIframeHeight(); | ||
| window.addEventListener('DOMContentLoaded', updateIframeHeight); | ||
| // the easiest way to handle dynamic content changes and easy to debug, can be fine-tuned in the future | ||
| setInterval(updateIframeHeight, 1000); | ||
|  | ||
| // no way to open an absolute link with CSP frame-src, it also needs some tricks like "postMessage" or "copy the link to clipboard" | ||
| const openIframeLink = (link: string, target: string) => postIframeMsg('open-link', {openLink: link, anchorTarget: target}); | ||
| document.addEventListener('click', (e) => { | ||
| const el = e.target as HTMLAnchorElement; | ||
| if (el.nodeName !== 'A') return; | ||
| const href = el.getAttribute('href') || ''; | ||
| // safe links: "./any", "../any", "/any", "//host/any", "http://host/any", "https://host/any" | ||
| if (href.startsWith('.') || href.startsWith('/') || href.startsWith('http://') || href.startsWith('https://')) { | ||
| e.preventDefault(); | ||
| openIframeLink(href, el.getAttribute('target')); | ||
| } | ||
| }); | ||
| } | ||
|  | ||
| mainExternalRenderIframe(); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.