I made a very simple Chrome extension where what should happen is the user can enter a value into a text box, press a button, and have the entered value filled into page text box (or any other element).
So, I have
popup.html:
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<script src="popup.js"></script>
</head>
<body>
<textarea type="text" id="data-details" cols="40" rows="5"></textarea>
<input type="button" id="btn-fill" value="Fill" />
</body>
</html>
popup.js:
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("btn-fill").addEventListener("click", injectTheScript);
});
function injectTheScript() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.executeScript(tabs[0].id, { file: "content_script.js" });
});
}
content_script.js:
function fillElements()
{
var dataDetailsStr = document.getElementById("data-details").value;
// error here : Uncaught TypeError: Cannot read property 'value' of null
document.getElementById("txt-page-input").value = dataDetailsStr;
}
fillElements();
manifest.json:
{
"manifest_version": 2,
"name": "Fill Data",
"description": "Fills data on the page",
"version": "1.0",
"permissions": [
"activeTab",
"http://*/*"
],
"browser_action": {
"default_icon": "img/icon.png",
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["http://*/*"],
"js": ["jquery.js", "content_script.js"]
}]
}
document.getElementById ("data-details")
always returns null
. How to access this element, or how to write this correct? I'm new at this.