123345566i 123345566i - 10 months ago 45
Javascript Question

Fill forms with a data from popup.html

I have been trying to create an extension that fills a form with data from a popup, I'm a bit confused regarding the use of "background" and "content" files, I don't think I need one. Here is my code:


"name": "Browser action for Form",
"description": "Fill a form with data from the popup",
"version": "1.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
"browser_action": {
"default_title": "Form Test",
"default_icon": "icon.png",
"default_popup": "popup.html"
"content_scripts": [
"matches": ["https://the-site-with-a-form.com/*"],
"js": ["jquery-3.1.1.min.js", "content.js"]
"manifest_version": 2


<!doctype html>
<script src="popup.js"></script>
<textarea id="txtArea"></textarea>
<input type="button" id="btn1" value="Run">


function click(){
var text = document.getElementById("txtArea")
{from: 'popup', subject: 'DOMInfo',data1: text});



chrome.runtime.onMessage.addListener(function (msg, sender, response) {
if ((msg.from === 'popup') && (msg.subject === 'DOMInfo')) {
//Fill the form with the data of the popup
document.getElementById("The textbox from the form").value = msg.data1;

what is wrong in the code?

Xan Xan
Answer Source

Please learn to debug extension popups. If you did, you would see an informative error message.

With that in mind, tabs in your popup code doesn't come from anywhere - so your code there stops with an error. Clearly, that part is ripped out of context (of a tabs.query call, most likely). Note that if your intent is to message the currently active tab, you can just skip the first argument of sendMessage entirely.

You defintiely do need a content script, since it's the only part of an extension that can interact with a webpage's form. Recommended reading: How do all types of Chrome extension scripts work?