Невозможно отобразить значение элемента через нажатие кнопки моего расширения
У меня есть аккаунт на www.makemytrip.com, где поля "от" и "до" уже сохранены с помощью файлов cookie.
Используя расширение, я хотел бы отобразить значение, присутствующее в поле "от" этого сайта. (чей идентификатор 'hp-widget__sfrom')
При загрузке страницы я могу просмотреть значение, но то же самое, чего я не могу достичь, нажав на кнопку, определенную в моем расширении.
Ниже приведен код: index.html
<html>
<head>
<title>My new extension</title>
</head>
<body>
<input type="button" id="save" value="Save Text">
</body>
</html>
manifest.json
{
"manifest_version": 2,
"name": "Test1",
"description": "TestDesc",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"activeTab"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "index.html",
"default_title": "Test11"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["myContentScript.js"]
}
]
}
myContentScript.js
window.onload = function(){
alert("Hi by contentScript");
//alert(document.getElementById('hp-widget__sfrom').value); //this works!
//but the below doesn't work; onclick of button
document.getElementById('save').onclick = function(){
alert(document.getElementById('hp-widget__sfrom').value);
}
}
background.js
window.onload = function(){
alert("Hi by backgroundJS");
}
Я ценю любую помощь!