跳到正文
DEV Community· CiteProof·· 6 天前AI 評分45

用於 AI 搜尋的主控台程式碼片段:檢查頁面是否先給出答案

A console snippet to check if your page answers first (for AI search)

AI 導讀

這段可貼到瀏覽器主控台的程式碼片段會列出每個 H1–H3 標題及其後首 60 個詞,協助檢查頁面是否一開始便回答問題。文章建議標題下首 40–60 個詞先回答問題,並指出這是建議做法而非搜尋引擎已公佈的規則;程式碼最適合標題與段落位於同一層的頁面。

正文

CiteProof

AI search engines like ChatGPT, Perplexity and Google's AI Overviews don't cite whole pages: they pull out passages that answer a question and cite their source. A practical way to make those passages easy to take is to put the answer in the first lines of the page and in the first lines under every heading.

Checking that by eye on a long page is slow. Here is a small snippet you can paste in the browser console: it lists every H1–H3 and the first 60 words that follow it, so you can see at a glance which sections answer and which ones warm up.

The snippet

// Paste in the browser console on any page.
// Prints each heading with the first 60 words that follow it.
(() => {
  const words = (t) => t.replace(/\s+/g, ' ').trim().split(' ').filter(Boolean);
  const heads = [...document.querySelectorAll('h1, h2, h3')];
  const rows = heads.map((h) => {
    let text = '';
    let el = h.nextElementSibling;
    while (el && !/^H[1-3]$/.test(el.tagName) && words(text).length < 60) {
      text += ' ' + el.innerText;
      el = el.nextElementSibling;
    }
    const w = words(text);
    return {
      level: h.tagName,
      heading: h.innerText.trim().slice(0, 80),
      first60: w.slice(0, 60).join(' '),
      words: w.length,
    };
  });
  console.table(rows);
  return rows.length + ' headings checked';
})();

It only reads siblings that follow each heading, so it works best on pages where headings and paragraphs sit at the same level (most blog and CMS templates). If your theme wraps every section in its own <div>, adapt the loop.

How to read the table

For each row, ask three questions:

  1. Does the heading use the words of the question? "How much does a dental implant cost in Denver?" beats "Let's talk money".
  2. Do the first sentences give the answer? A number, a yes or no, a choice. Not "there are many factors to consider".
  3. Does the passage make sense on its own? A common guideline is that the first 40–60 words under a heading should answer it. That's a recommended practice, not a published engine rule, but it is the shape of a quotable passage.

Rows with words: 0 are headings with nothing under them before the next heading: worth a look too.

Fixing a section

The formula is two or three sentences, then the rest:

  • name the topic with the words of the question;
  • give the answer;
  • then explain conditions, exceptions and examples.

Before: "In the ever-evolving landscape of the dental industry, there are many factors to consider."
After: "A dental implant in Denver usually costs between [low] and [high]. The price depends on three things."

(Prices left blank on purpose: it's an illustration.)

Try it

Run the snippet on one page tied to a question where AI answers don't cite you. Rewrite the first hundred words and three sections, then re-run it.

The method is lesson 5.1 of our free AEO and GEO course: The answer in the first lines.

I'm Adrian, founder of CiteProof.

來源:DEV Community · dev.to