Get DOM values in browser extension

749 views Asked by At

I want to read a value of DOM element. I am new to browser extensions. I came across codes which provided help in executing a code in browser context but how do I fetch a value and use in extension's context?

var value = document.getElementById('id1'); // Here document should be of browser context.
2

There are 2 answers

1
Will White On BEST ANSWER

Try

var value = document.getElementById('id1').value;

// note the extra .value.

You have to access the DOM from content scripts.

0
Brian On

If you are looking to get the currently active tab, you would do this: https://developer.chrome.com/extensions/activeTab

How this will work will depend on whether you are in browser context, content scripts or background though.

I highly recommend reading the chrome extension guides: https://developer.chrome.com/extensions/getstarted. They help quite a bit, and the different scopes of the chrome extension are crucial to understand to successfully develop on it.