-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: Support iframe rendering #4828
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
+243
−228
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <template> | ||
| <div class="iframe-wrapper"> | ||
| <iframe | ||
| v-show="visible" | ||
| ref="iframeRef" | ||
| class="iframe" | ||
| :srcdoc="finalSource" | ||
| @load="resize" | ||
| sandbox="allow-scripts allow-same-origin" | ||
| /> | ||
| </div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { computed, ref, watch, nextTick } from 'vue' | ||
| const resize = async () => { | ||
| await nextTick() | ||
|
|
||
| const iframe = iframeRef.value | ||
| if (!iframe) return | ||
|
|
||
| const doc = iframe.contentDocument | ||
| if (!doc) return | ||
|
|
||
| const contentHeight = doc.documentElement.scrollHeight || doc.body.scrollHeight | ||
|
|
||
| const viewportHeight = window.innerHeight | ||
|
|
||
| const finalHeight = Math.min(contentHeight, viewportHeight) | ||
|
|
||
| iframe.style.height = finalHeight + 'px' | ||
|
|
||
| iframe.style.overflow = contentHeight > viewportHeight ? 'auto' : 'hidden' | ||
| } | ||
| const props = withDefaults( | ||
| defineProps<{ | ||
| source?: string | ||
| script_exec?: boolean | ||
| visible?: boolean | ||
| }>(), | ||
| { | ||
| source: '', | ||
| script_exec: true, | ||
| visible: true, | ||
| }, | ||
| ) | ||
|
|
||
| const iframeRef = ref<HTMLIFrameElement>() | ||
|
|
||
| // 如果不允许执行 script,就过滤掉 | ||
| const finalSource = computed(() => { | ||
| if (props.script_exec) return props.source | ||
|
|
||
| return props.source.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, '') | ||
| }) | ||
|
|
||
| // 如果 source 改变才刷新 iframe | ||
| watch( | ||
| () => props.source, | ||
| () => { | ||
| if (iframeRef.value) { | ||
| iframeRef.value.srcdoc = finalSource.value | ||
| } | ||
| }, | ||
| ) | ||
| </script> | ||
|
|
||
| <style scoped> | ||
| .iframe-wrapper { | ||
| width: 100%; | ||
| height: 100%; | ||
| } | ||
| .iframe { | ||
| width: 100%; | ||
| height: 100%; | ||
| border: none; | ||
| } | ||
| </style> | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code template has some potential areas for improvement in terms of security, readability, and performance. Here are my suggestions:
Security Improvements
sandboxattributes include necessary CSP directives like'content-security-policy': "default-src 'self';"to prevent XSS attacks.@if (iframes && Array.isArray(iframes)) { <script> try { document.top.location.href = window.self.location.toString(); } catch (e) {} </script> }Readability and Style Optimization
Variable Naming Consistency:
resize,finalSource,iframe, etc., should be clear and descriptive.Whitespace Reduction:
Performance Improvements
awaitwithout wrapping promises in parentheses when not needed.By implementing these improvements, you can enhance the robustness, security, and efficiency of your Vue.js component using Vue Composition API.
Security Note: Always consider the security implications of your implementation when handling external data, especially if it involves executing scripts or allowing arbitrary content loading (
<source>).