I’m working on a Chrome extension that needs to get all text from a Google Docs document as users type, edit, or make changes. The problem is Google Docs doesn’t use regular text input fields, so normal methods don’t work.
What I’ve tried so far:
- Input event listeners - Doesn’t work with Google Docs structure
- Keydown events - Can’t track cursor position or paste operations properly
- Monitoring doc-appview elements - Not detecting changes
My code finds the container but the MutationObserver never triggers. What am I missing here?
Current approach:
function initializeDocsTextCapture() {
if (window.location.hostname !== 'docs.google.com') return;
console.log('🟢 Extension loaded on', window.location.href);
function attachTextWatcher() {
const docContainer = document.querySelector('.doc-appview');
if (!docContainer) {
setTimeout(attachTextWatcher, 300);
console.log('Searching for .doc-appview...');
return;
}
console.log('Container found, initializing watcher...');
let previousContent = '';
const textObserver = new MutationObserver(() => {
console.log('Change detected');
const textLines = Array.from(document.querySelectorAll('.doc-lineview'));
const documentText = textLines.map(element => element.innerText).join('\n').trim();
if (documentText !== previousContent) {
previousContent = documentText;
console.log('Text changed, updating storage');
chrome.runtime.sendMessage({
action: 'text_update',
content: documentText
});
}
});
textObserver.observe(docContainer, {
childList: true,
subtree: true,
characterData: true
});
console.log('📝 Text monitoring active');
}
attachTextWatcher();
}
window.addEventListener('load', initializeDocsTextCapture);
The observer setup looks correct but changes aren’t being detected. Any suggestions for a better approach or what I’m doing wrong?