<script>
const siteURL = 'https://huggingface-projects-llama-3-2-vision-11b.hf.space/';
let progress = 0;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loadingContainer = document.getElementById('loadingContainer');
let interval;
let iframeLoadedFlag = false;
let observer;
function startProgress() {
loadingContainer.style.display = 'flex';
progressBar.style.width = '0%';
progress = 0;
interval = setInterval(() => {
if (progress < 100) {
progress += 10;
progressBar.style.width = progress + '%';
}
if (progress === 100) {
clearInterval(interval);
if (!iframeLoadedFlag) {
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
}
}, 1000);
}
function iframeLoaded() {
iframeLoadedFlag = true;
clearInterval(interval);
loadingContainer.style.display = 'none';
const iframeContainer = document.querySelector('.iframe-container');
iframeContainer.classList.remove('normal');
iframeContainer.classList.add('fullscreen');
document.getElementById('myIframe').style.display = 'block';
modifyIframeContent();
setupMutationObserver();
}
function iframeError() {
clearInterval(interval);
loadingContainer.style.display = 'flex';
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
function loadIframe() {
fetch(siteURL, { method: 'HEAD', mode: 'no-cors' })
.then(() => {
const iframe = document.getElementById('myIframe');
iframe.src = siteURL;
iframe.onload = iframeLoaded;
iframe.onerror = iframeError;
})
.catch(() => {
iframeError();
});
}
retryButton.addEventListener('click', function() {
progress = 0;
progressBar.style.width = '0%';
iframeLoadedFlag = false;
retryButton.style.display = 'none';
errorMessage.style.display = 'none';
loadingContainer.style.display = 'flex';
document.getElementById('myIframe').style.display = 'none';
loadIframe();
startProgress();
});
function setVh() {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
setVh();
window.addEventListener('resize', setVh);
loadIframe();
startProgress();
function modifyIframeContent() {
const iframe = document.getElementById('myIframe');
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
// قطع اتصال MutationObserver
if (observer) observer.disconnect();
// مخفی کردن متون ناخواسته
const unwantedTexts = [
'ساخته شده با gradio',
'Use via API',
'To learn more about Llama Vision, visit our blog post.',
'Which era does this piece belong to? Give details about the era.'
];
unwantedTexts.forEach(unwanted => {
const textNodes = getTextNodesContaining(iframeDocument.body, unwanted);
textNodes.forEach(node => {
const parentElement = node.parentElement;
if (parentElement) {
parentElement.style.display = 'none';
}
});
});
// جایگزینی متن مشخص با متن جدید
const targetText = 'Try Multimodal Llama by Meta with transformers in this demo. Upload an image, and start chatting about it, or simply try one of the examples below.';
const newText = 'جدیدترین نسخه چت بات متا با تحلیل تصاویر. یک تصویر آپلود کنید و درباره آن چت کنید و یا یکی از نمونه های پایین رو امتحان کنید.';
const targetNodes = getTextNodesContaining(iframeDocument.body, targetText);
targetNodes.forEach(node => {
node.textContent = newText;
});
// جایگزینی متن دیگر با ترجمه فارسی
const additionalTargetText = 'Which era does this piece belong to? Give details about the era.';
const additionalNewText = 'این اثر به کدام دوره تعلق دارد؟ جزئیات مربوط به آن دوره را بیان کنید.';
const additionalTargetNodes = getTextNodesContaining(iframeDocument.body, additionalTargetText);
additionalTargetNodes.forEach(node => {
node.textContent = additionalNewText;
});
// اتصال مجدد MutationObserver
if (observer) observer.observe(iframeDocument.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error("نمیتوان به محتوای iframe دسترسی پیدا کرد: ", error);
}
}
function getTextNodesContaining(node, text) {
let textNodes = [];
if (node.nodeType === Node.TEXT_NODE && node.nodeValue.includes(text)) {
textNodes.push(node);
} else {
node.childNodes.forEach(child => {
textNodes = textNodes.concat(getTextNodesContaining(child, text));
});
}
return textNodes;
}
function setupMutationObserver() {
const iframe = document.getElementById('myIframe');
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
modifyIframeContent();
}
});
});
observer.observe(iframeDocument.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error("نمیتوان به محتوای iframe دسترسی پیدا کرد: ", error);
}
}
</script>
Certainly! Below is the complete HTML code with the requested modifications. The script has been updated to hide the additional texts and replace them with their corresponding Farsi translations. The previous hiding operations have been retained to ensure consistent behavior.
Updated HTML Code
html
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>Iframe Loader</title>
<style>
/* تنظیمات پایه برای body و html */
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
/* متغیر CSS برای ارتفاع نمایشگر */
:root {
--vh: 100%;
}
/* حالت عادی */
.iframe-container.normal {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
.iframe-container.normal iframe {
width: 100%;
height: 100%;
border: none;
display: none;
position: relative;
top: 0;
}
/* حالت تمام صفحه */
.iframe-container.fullscreen {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: calc(var(--vh) * 100);
overflow: hidden;
z-index: 1;
}
.iframe-container.fullscreen iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: none;
display: block;
}
/* استایلهای مشترک */
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
z-index: 10000;
}
.loading-message {
margin-bottom: 20px;
font-size: 18px;
}
.progress {
width: 80%;
max-width: 400px;
height: 20px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
}
.progress-bar {
height: 100%;
width: 0;
background-color: #4caf50;
transition: width 1s;
}
.error-message {
display: none;
color: red;
margin-top: 20px;
font-size: 16px;
text-align: center;
}
#retryButton {
display: none;
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
}
</style>
</head>
<body>
<!-- لودینگ و iframe -->
<div class="iframe-container normal">
<div class="loading" id="loadingContainer">
<div class="loading-message">لطفاً صبر کنید...</div>
<div class="progress">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="error-message" id="errorMessage">
بارگیری زیاد طول کشید.<br />
در صورت عدم بارگذاری، فیلترشکن خود را روشن کنید.
</div>
<button id="retryButton">بارگذاری مجدد</button>
</div>
<iframe id="myIframe" sandbox="allow-scripts allow-same-origin allow-forms allow-modals" allow="clipboard-read; clipboard-write"></iframe>
</div>
<script>
const siteURL = 'https://huggingface-projects-llama-3-2-vision-11b.hf.space/';
let progress = 0;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loadingContainer = document.getElementById('loadingContainer');
let interval;
let iframeLoadedFlag = false;
let observer;
function startProgress() {
loadingContainer.style.display = 'flex';
progressBar.style.width = '0%';
progress = 0;
interval = setInterval(() => {
if (progress < 100) {
progress += 10;
progressBar.style.width = progress + '%';
}
if (progress === 100) {
clearInterval(interval);
if (!iframeLoadedFlag) {
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
}
}, 1000);
}
function iframeLoaded() {
iframeLoadedFlag = true;
clearInterval(interval);
loadingContainer.style.display = 'none';
const iframeContainer = document.querySelector('.iframe-container');
iframeContainer.classList.remove('normal');
iframeContainer.classList.add('fullscreen');
document.getElementById('myIframe').style.display = 'block';
modifyIframeContent();
setupMutationObserver();
}
function iframeError() {
clearInterval(interval);
loadingContainer.style.display = 'flex';
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
function loadIframe() {
fetch(siteURL, { method: 'HEAD', mode: 'no-cors' })
.then(() => {
const iframe = document.getElementById('myIframe');
iframe.src = siteURL;
iframe.onload = iframeLoaded;
iframe.onerror = iframeError;
})
.catch(() => {
iframeError();
});
}
retryButton.addEventListener('click', function() {
progress = 0;
progressBar.style.width = '0%';
iframeLoadedFlag = false;
retryButton.style.display = 'none';
errorMessage.style.display = 'none';
loadingContainer.style.display = 'flex';
document.getElementById('myIframe').style.display = 'none';
loadIframe();
startProgress();
});
function setVh() {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
setVh();
window.addEventListener('resize', setVh);
loadIframe();
startProgress();
function modifyIframeContent() {
const iframe = document.getElementById('myIframe');
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
// قطع اتصال MutationObserver
if (observer) observer.disconnect();
// مخفی کردن متون ناخواسته
const unwantedTexts = [
'ساخته شده با gradio',
'Use via API',
'To learn more about Llama Vision, visit our blog post.',
'Which era does this piece belong to? Give details about the era.',
'Retry',
'Undo',
'Clear',
'Examples',
'Where do the droughts happen according to this diagram?',
'What happens when you take out white cat from this chain?',
'How long does it take from invoice date to due date? Be short and concise.',
'Where to find this monument? Can you give me other recommendations around the area?',
'Additional Inputs',
'Maximum number of new tokens to generate'
];
unwantedTexts.forEach(unwanted => {
const textNodes = getTextNodesContaining(iframeDocument.body, unwanted);
textNodes.forEach(node => {
const parentElement = node.parentElement;
if (parentElement) {
parentElement.style.display = 'none';
}
});
});
// جایگزینی متون با ترجمه فارسی
const replacements = [
{
target: 'Try Multimodal Llama by Meta with transformers in this demo. Upload an image, and start chatting about it, or simply try one of the examples below.',
replacement: 'جدیدترین نسخه چت بات متا با تحلیل تصاویر. یک تصویر آپلود کنید و درباره آن چت کنید و یا یکی از نمونه های پایین رو امتحان کنید.'
},
{
target: 'Which era does this piece belong to? Give details about the era.',
replacement: 'این اثر به کدام دوره تعلق دارد؟ جزئیات مربوط به آن دوره را بیان کنید.'
},
{
target: 'Where do the droughts happen according to this diagram?',
replacement: 'مطابق با این نمودار، خشکسالیها در کجاها رخ میدهند؟'
},
{
target: 'What happens when you take out white cat from this chain?',
replacement: 'وقتی گربه سفید را از این زنجیره خارج میکنید، چه اتفاقی میافتد؟'
},
{
target: 'How long does it take from invoice date to due date? Be short and concise.',
replacement: 'مدت زمان از تاریخ فاکتور تا تاریخ سررسید چقدر است؟ مختصر باشید.'
},
{
target: 'Where to find this monument? Can you give me other recommendations around the area?',
replacement: 'چگونه میتوان این بنا را پیدا کرد؟ آیا میتوانید توصیههای دیگری در اطراف منطقه به من بدهید؟'
},
{
target: 'Additional Inputs',
replacement: 'ورودیهای اضافی'
},
{
target: 'Maximum number of new tokens to generate',
replacement: 'حداکثر تعداد نشانههای جدید برای تولید'
}
];
replacements.forEach(pair => {
const targetNodes = getTextNodesContaining(iframeDocument.body, pair.target);
targetNodes.forEach(node => {
node.textContent = pair.replacement;
});
});
// اتصال مجدد MutationObserver
if (observer) observer.observe(iframeDocument.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error("نمیتوان به محتوای iframe دسترسی پیدا کرد: ", error);
}
}
function getTextNodesContaining(node, text) {
let textNodes = [];
if (node.nodeType === Node.TEXT_NODE && node.nodeValue.includes(text)) {
textNodes.push(node);
} else {
node.childNodes.forEach(child => {
textNodes = textNodes.concat(getTextNodesContaining(child, text));
});
}
return textNodes;
}
function setupMutationObserver() {
const iframe = document.getElementById('myIframe');
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
modifyIframeContent();
}
});
});
observer.observe(iframeDocument.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error("نمیتوان به محتوای iframe دسترسی پیدا کرد: ", error);
}
}
</script>
</body>
</html>
توضیحات تغییرات
اضافه کردن متون جدید به لیست unwantedTexts:
تمامی متونی که شما درخواست کردهاید برای مخفیسازی به آرایه unwantedTexts اضافه شدند. این متون هنگام بارگذاری آیفریم، مخفی خواهند شد.
javascript
const unwantedTexts = [
'ساخته شده با gradio',
'Use via API',
'To learn more about Llama Vision, visit our blog post.',
'Which era does this piece belong to? Give details about the era.',
'Retry',
'Undo',
'Clear',
'Examples',
'Where do the droughts happen according to this diagram?',
'What happens when you take out white cat from this chain?',
'How long does it take from invoice date to due date? Be short and concise.',
'Where to find this monument? Can you give me other recommendations around the area?',
'Additional Inputs',
'Maximum number of new tokens to generate'
];
تعریف لیست جایگزینی replacements:
یک آرایه از اشیاء تعریف شده است که هر شیء شامل target (متن اصلی) و replacement (ترجمه فارسی) میباشد. این متون جایگزین متون اصلی در محتوای آیفریم خواهند شد.
javascript
توجه مهم
دسترسی به محتوای آیفریم:
لطفاً توجه داشته باشید که دسترسی به محتوای آیفریمها که از دامنههای مختلف بارگذاری شدهاند معمولاً توسط سیاستهای امنیتی مرورگرها (CORS) محدود شده است. برای اینکه این کد به درستی عمل کند، نیاز است که سرور آیفریم به نحوی تنظیم شده باشد که اجازه دسترسی از طریق جاوااسکریپت را دهد. در غیر این صورت، شما ممکن است با خطاهای دسترسی مواجه شوید و تغییرات اعمال نگردد.
نتیجهگیری
با اعمال این تغییرات، تمامی متونی که شما درخواست کردهاید مخفی شده و در صورت امکان، ترجمههای فارسی جایگزین آنها نمایش داده میشود. این امر تجربه کاربری بهتری را برای کاربران فارسیزبان فراهم میآورد. اگر به مشکلاتی در عملکرد کد برخوردید، لطفاً تنظیمات CORS سرور آیفریم را بررسی کنید یا از راههای دیگری برای تغییر محتوای آن استفاده نمایید.
const replacements = [
{
target: 'Try Multimodal Llama by Meta with transformers in this demo. Upload an image, and start chatting about it, or simply try one of the examples below.',
replacement: 'جدیدترین نسخه چت بات متا با تحلیل تصاویر. یک تصویر آپلود کنید و درباره آن چت کنید و یا یکی از نمونه های پایین رو امتحان کنید.'
},
{
target: 'Which era does this piece belong to? Give details about the era.',
replacement: 'این اثر به کدام دوره تعلق دارد؟ جزئیات مربوط به آن دوره را بیان کنید.'
},
{
target: 'Where do the droughts happen according to this diagram?',
replacement: 'مطابق با این نمودار، خشکسالیها در کجاها رخ میدهند؟'
},
{
target: 'What happens when you take out white cat from this chain?',
replacement: 'وقتی گربه سفید را از این زنجیره خارج میکنید، چه اتفاقی میافتد؟'
},
{
target: 'How long does it take from invoice date to due date? Be short and concise.',
replacement: 'مدت زمان از تاریخ فاکتور تا تاریخ سررسید چقدر است؟ مختصر باشید.'
},
{
target: 'Where to find this monument? Can you give me other recommendations around the area?',
replacement: 'چگونه میتوان این بنا را پیدا کرد؟ آیا میتوانید توصیههای دیگری در اطراف منطقه به من بدهید؟'
},
{
target: 'Additional Inputs',
replacement: 'ورودیهای اضافی'
},
{
target: 'Maximum number of new tokens to generate',
replacement: 'حداکثر تعداد نشانههای جدید برای تولید'
}
];
اعمال مخفیسازی و جایگزینی در تابع modifyIframeContent:
ابتدا با استفاده از getTextNodesContaining تمامی نودهای متنی حاوی متون ناخواسته پیدا شده و والدین آنها مخفی میشوند.
سپس، با استفاده از لیست replacements, متون اصلی با ترجمه فارسی جایگزین میشوند.
با استفاده از MutationObserver, هر تغییر در محتوای آیفریم نظارت شده و در صورت افزودن گرههای جدید، مجدداً modifyIframeContent فراخوانی میشود تا مطمئن شویم که تغییرات جدید نیز اعمال شدهاند.
javascript
if (observer) observer.observe(iframeDocument.body, {
childList: true,
subtree: true
});