Serchmatn / index.html
Ezmary's picture
Update index.html
b547b20 verified
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>جستجوگر متن موبایل</title>
<style>
* {
box-sizing: border-box;
outline: none;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #f0f2f5;
margin: 0;
padding: 0;
color: #333;
font-size: 16px; /* سایز مناسب برای خواندن در موبایل */
}
.header {
background-color: #ffffff;
padding: 15px;
text-align: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
position: sticky;
top: 0;
z-index: 100;
}
.header h1 {
margin: 0;
font-size: 18px;
color: #444;
}
.container {
padding: 15px;
max-width: 600px; /* جلوگیری از کش آمدن بیش از حد در تبلت */
margin: 0 auto;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
font-size: 14px;
color: #555;
}
/* استایل ورودی متن اصلی */
textarea {
width: 100%;
height: 150px;
padding: 12px;
border: 1px solid #ddd;
border-radius: 12px;
font-family: inherit;
font-size: 16px; /* جلوگیری از زوم خودکار در آیفون */
resize: vertical;
background: #fff;
transition: border-color 0.3s;
}
textarea:focus {
border-color: #007bff;
}
/* باکس جستجو */
.search-area {
background: white;
padding: 15px;
border-radius: 12px;
margin-top: 15px;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
input[type="text"] {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 8px;
font-family: inherit;
font-size: 16px;
margin-bottom: 10px;
}
button {
width: 100%;
padding: 14px;
background-color: #007bff;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: background 0.2s;
/* افکت لمس در موبایل */
touch-action: manipulation;
}
button:active {
background-color: #0056b3;
transform: scale(0.98);
}
/* بخش نمایش نتیجه */
#result-wrapper {
margin-top: 20px;
display: none; /* در ابتدا مخفی است */
}
.result-title {
font-size: 14px;
color: #666;
margin-bottom: 5px;
display: flex;
justify-content: space-between;
}
#result-box {
background: #fff;
padding: 15px;
border-radius: 12px;
line-height: 2;
border: 2px solid #007bff;
min-height: 100px;
white-space: pre-wrap; /* حفظ فاصله خطوط */
}
.highlight {
background-color: #ffeb3b;
color: #000;
padding: 2px 4px;
border-radius: 4px;
font-weight: bold;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
.error-msg {
color: red;
font-size: 13px;
margin-top: 5px;
display: none;
}
</style>
</head>
<body>
<div class="header">
<h1>جستجوگر متن پیشرفته</h1>
</div>
<div class="container">
<!-- ورودی متن اصلی -->
<label for="mainText">۱. متن اصلی را اینجا وارد کنید:</label>
<textarea id="mainText" placeholder="متن طولانی خود را اینجا پیست کنید یا بنویسید..."></textarea>
<!-- ورودی جستجو -->
<div class="search-area">
<label for="searchQuery">۲. دنبال چه کلمه‌ای هستید؟</label>
<input type="text" id="searchQuery" placeholder="مثلاً: سلام">
<button onclick="doSearch()">جستجو و نمایش نتیجه</button>
<div id="errorMsg" class="error-msg">لطفا هم متن اصلی و هم کلمه جستجو را وارد کنید.</div>
</div>
<!-- نمایش نتیجه -->
<div id="result-wrapper">
<div class="result-title">
<span>نتیجه جستجو:</span>
<span id="count-badge">0 مورد</span>
</div>
<div id="result-box"></div>
</div>
</div>
<script>
function doSearch() {
// گرفتن مقادیر
const mainText = document.getElementById('mainText').value;
const query = document.getElementById('searchQuery').value;
const resultWrapper = document.getElementById('result-wrapper');
const resultBox = document.getElementById('result-box');
const countBadge = document.getElementById('count-badge');
const errorMsg = document.getElementById('errorMsg');
// اعتبارسنجی: اگر فیلدها خالی باشند
if (!mainText.trim() || !query.trim()) {
errorMsg.style.display = 'block';
resultWrapper.style.display = 'none';
return;
}
errorMsg.style.display = 'none';
resultWrapper.style.display = 'block';
// تمیزکاری کاراکترهای خاص برای جلوگیری از خراب شدن Regex
// مثلا اگر کاربر ؟ یا + را جستجو کرد برنامه خراب نشود
const safeQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// ساختن Regex
// g = همه موارد
// i = حروف کوچک و بزرگ (برای انگلیسی)
const regex = new RegExp(`(${safeQuery})`, 'gi');
// هایلایت کردن
// ما از replace استفاده میکنیم تا متن ساده را به HTML تبدیل کنیم
const highlightedText = mainText.replace(regex, '<span class="highlight">$1</span>');
// نمایش در باکس نتیجه
resultBox.innerHTML = highlightedText;
// شمارش تعداد
const count = (mainText.match(regex) || []).length;
countBadge.innerText = count + " مورد پیدا شد";
// اسکرول نرم به سمت نتیجه برای موبایل
resultWrapper.scrollIntoView({ behavior: 'smooth' });
}
</script>
</body>
</html>