codezart
codezart

Reputation: 87

I want to do DOM manipulation in my chrome extension using chrome.commands

I want to make a chrome extension where I can use key commands on specific pages (Eg: "Ctrl+Shift+A") which then triggers some DOM manipulation code (such as clicking a button).

But the issue I am facing is that "chrome.commands" runs in the background environment (background property in manifest.json) and If I include "chrome.commands.onCommand" in content_scripts (inside manifest.json) I get this error.

Uncaught TypeError: Cannot read property 'onCommand' of undefined.

How do I make it work?

Upvotes: 0

Views: 167

Answers (1)

woxxom
woxxom

Reputation: 73846

Use two scripts. The background script registers a chrome.commands listener which injects a content script (or sends a message to a content script declared in manifest.json). The content script runs in the web page and has access to its DOM.

Upvotes: 1

Related Questions