Problem
I'm trying to create a very simple Chrome extension. It only has 2 files. I have full control of the site, mywebsite.com.
- It will create a new tab to
mywebsite.com - It will select an input element by id
- It will change the value of the input element
Unfortunately, I get this error: Uncaught TypeError: Caot set property 'value' of null
No matter what I do, the element is always null.
I've tried the following:
- Various ways to wait until the page is loaded
- Enabled CORS for my chrome extension on
mywebsite.com - All optional arguments for
chrome.tabs.create, andchrome.tabs.executeScript
Files
manifest.json
{
"manifest_version": 2,
"name": "Extension Name",
"description": "Extension Description",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {},
"permissions": [
"tabs",
"https://mywebsite.com/*"
]
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({url : 'https://mywebsite.com'}, function (tab) {
var code = "document.getElementById('first_name').value = 'FirstName';";
chrome.tabs.executeScript(tab.id, {code: code});
});
});
