Skip to content

Conversation

@007DXR
Copy link

@007DXR 007DXR commented Dec 19, 2025

Fix Page Content Not Being Passed to LLM When useContext=true

🐛 Issue Description

When useContext = true, the browser extension is unable to access the current page content, and the LLM responds with "I cannot directly access or display webpage content."

🔍 Root Cause

  1. Page Content Not Added to Chat History: Although the fetchPageContents() function retrieves page content, it is never added to chatHistory.
  2. Timing Issue with window.onload: The original code uses window.onload, which may not trigger in certain cases, causing fetchPageContents() not to be called.

✅ Solution

1. Add Page Context Storage

let pageContext = ""; // Store the page context

2. Add Page Content to Chat History

// Store the page context
pageContext = msg.contents;

// Add page context to chat history as a system message
if (pageContext && chatHistory.length === 0) {
  chatHistory.push({
    role: "system",
    content: `You are a helpful assistant. Here is the content of the current webpage the user is viewing:\n\n${pageContext}\n\nPlease answer questions about this webpage based on the content provided above.`
  });
}

3. Improve Page Load Detection

// Use DOMContentLoaded instead of window.onload
if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', function() {
    if (useContext) {
      fetchPageContents();
    }
  });
} else {
  // Document already loaded, call immediately
  if (useContext) {
    fetchPageContents();
  }
}

@007DXR 007DXR marked this pull request as draft December 19, 2025 07:45
@007DXR 007DXR marked this pull request as ready for review December 22, 2025 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant