I'm new to developing chrome extension, so please excuse my ignorance. I want to make a popup html button which click event open a new tab and fill input field inside new tab.
Here's my example code.
popup.html
<button onclick="test">click me</button>
popup.js
function test() {
chrome.tabs.create({ 'url': "https://www.google.com" }, function (tab) {
var new_tab_id: number = 0;
if (tab.id != undefined) {
new_tab_id = tab.id;
}
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(new_tab_id, { greeting: "hello" }, function (response) {
console.log(response.farewell);
});
});
})
}
}
content.js
chrome.runtime.onMessage.addListener(
function(request:any, sender:any, sendResponse:any) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello") {
const inputEl:HTMLInputElement|null = document.querySelector('input[name=q]');
if(inputEl !== null) {
inputEl.value = 'test input'
}
}
sendResponse({farewell: "goodbye"});
}
);
Is there something I did wrong? console from new tab, popup, background doesn't say anything..