You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测网页中是否包含指定文本?

Hey there! Let's troubleshoot this issue step by step—there are a few common culprits when your text detection condition isn't firing as expected. Here are the most likely fixes:

1. Check for Case Sensitivity

More often than not, case mismatches are the problem. If the page uses lowercase ("here is my code") but your condition looks for the capitalized version ("Here is my code"), the check will fail. Use a case-insensitive comparison to avoid this:

// Example in JavaScript
const pageText = document.body.textContent;
if (pageText.toLowerCase().includes("here is my code".toLowerCase())) {
  // Your logic when the phrase is found
}

2. Clean Up Whitespace & Hidden Characters

Web pages often have extra spaces, line breaks, or non-breaking spaces ( ) that break exact matches. Normalize both the page text and your target phrase first:

const normalizeText = (text) => text.replace(/\s+/g, ' ').trim();

const pageText = normalizeText(document.body.textContent);
const targetPhrase = normalizeText("Here is my code");

if (pageText.includes(targetPhrase)) {
  // Success!
}

3. Wait for Dynamic Content to Load

If the page uses JavaScript to load content (like React/Vue apps or AJAX calls), checking immediately will miss the text. Make sure to wait for the page to fully render:

// Wait for the entire page to load
window.addEventListener('load', () => {
  // Or wait for a specific element that contains your target text
  const targetElement = document.querySelector('.content-container');
  if (targetElement && targetElement.textContent.includes("Here is my code")) {
    // Your logic here
  }
});

4. Verify You're Checking the Right Text Source

Double-check that you're extracting text from the entire page (or the correct section). Using document.body.textContent will capture all visible text, whereas grabbing a single element's text might miss the phrase if it's elsewhere.

5. Avoid Strict Equality for Partial Matches

If you're using === instead of includes(), you're only matching when the entire page text is exactly your phrase—use includes() to check for the presence of the substring instead:

// ❌ Wrong: Only matches if the entire page text is exactly the phrase
if (pageText === "Here is my code") { ... }

// ✅ Correct: Checks if the phrase exists anywhere in the text
if (pageText.includes("Here is my code")) { ... }

Try these steps one by one—chances are one of them will fix your condition!

内容的提问来源于stack exchange,提问作者Al F.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:48:10