Run chrome extension and submit data to PHP script on page load (automate the process)

I am first time using chrome extension for web crawling. I get data in HTML popup and then submits it to PHP script on button click. Now I want to do this functionality on page load. So on page load data will be collected and submit it to PHP script. I am using extension as below


    "manifest_version": 2,
    "name":     "Test Extension",
    "version":  "1.0",
    "offline_enabled": true,

    "background": {
        "scripts": ["background.js"]
    "content_scripts": [{
        "matches": [ "http://*/*", "https://*/*"],
        "js":      ["jquery.min.js","content.js"],
         "run_at": "document_end"
    "permissions": [

    "page_action": {
        "default_title": "Test Extension",
        "default_popup": "popup.html"


chrome.runtime.onMessage.addListener(function(msg, sender, response) { 

    /* First, validate the message's structure */
    if (msg.from && (msg.from === "popup")
            && msg.subject && (msg.subject === "DOMInfo")) {
        /* Collect the necessary data 
         * (For your specific requirements `document.querySelectorAll(...)`
         *  should be equivalent to jquery's `$(...)`)*/

        var domInfo = {
            name : $('ul.companyDetails li:nth-child(1) > br').get(0).nextSibling.nodeValue.trim(),
            address : $('ul.companyDetails li:nth-child(2) > br').get(0).nextSibling.nodeValue.trim() + " "
                      + $('ul.companyDetails li:nth-child(2) > br').get(1).nextSibling.nodeValue.trim(),
            reg_date : $('ul.companyDetails li:nth-child(3) > br').get(0).nextSibling.nodeValue.trim(),
            legal_form : $('ul.companyDetails li:nth-child(4) > br').get(0).nextSibling.nodeValue.trim()
        /* Directly respond to the sender (popup), 
         * through the specified callback */


/* Update the relevant fields with the new data */
function setDOMInfo(info) { 
 document.getElementById("name").value =;
 document.getElementById("address").value = info.address;
 document.getElementById("reg_date").value = info.reg_date;
 document.getElementById("legal_form").value = info.legal_form;

/* Once the DOM is ready... */
window.addEventListener("DOMContentLoaded", function() {
 /* ...query for the active tab... */
 active: true,
 currentWindow: true
 }, function(tabs) {
 /* ...and send a request for the DOM info... */
 { from: "popup", subject: "DOMInfo" },
 /* ...also specifying a callback to be called 
 * from the receiving end (content script) */

 document.addEventListener('DOMContentLoaded', function () {
      .addEventListener('click', download);

function download()

    // The URL to POST our data to
    var postUrl = 'link to PHP script';

    // Set up an asynchronous AJAX POST request
    var xhr = new XMLHttpRequest();'POST', postUrl, true);

    // Prepare the data to be POSTed by URLEncoding each field's contents

    // Handle request state change events
    xhr.onreadystatechange = function() { 

    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    // Send the request and set status


 <script type="text/javascript" src="jquery.min.js"></script>
 <script type="text/javascript" src="popup.js"></script>
<form id="frm_info">
    <h3 style="font-weight:bold; text-align:center;"> DOM Info </h3>
    <input type="text" name="name" id="name" value="">
    <input type="text" name="address" id="address" value="">
    <input type="text" name="reg_date" id="reg_date" value="">
    <input type="text" name="legal_form" id="legal_form" value="">
    <input type="button" id="btn_php_call" value="submit">


Currently I need to click on chrome extension then data renders in popup.html and when I click on submit button then data goes to PHP. I want to do it on page load so user will no need to do it manually.

Edit : I added image of chrome extension when I click on extension (T symbol). then it load information in popup. When I click on submit data in popup goes to PHP script

enter image description here



Use the background page

In your manifest, change your current "background" key with:

"background": { "page" : "background.html", "persistent": true}

and your "content_scripts" key with:

"content_scripts": [{"matches":"the_URLs_you_want_to_scrape", "js" : ["content.js"], "run_at": "document_idle"}]

This will be background.html:

    <form id=myForm>
      <input type="text" name="name" id="name">
      <input type="text" name="address" id="address">
      <input type="text" name="reg_date" id="reg_date">
      <input type="text" name="legal_form" id="legal_form">
    <script src="background.js"></script>

In your background.js file include this:

  if (message.from == "cs") {
    document.getElementById("name").value =;
    document.getElementById("address").value = message.address;
    document.getElementById("reg_date").value = message.reg_date;
    document.getElementById("legal_form").value = message.legal_form;

    var encodedData = []"myForm").elements, function(acc,cur){
      acc.push("="+encodeURIComponent(cur.value));return acc},[]).join("&");

    var xhr = new XMLHttpRequest();
    xhr.onload = function(){
      //check the response
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

Then your content.js will directly be:

var myMessage = {
  from: "cs",
  name : document.querySelector('ul.companyDetails li:nth-child(1) > br').nextSibling.nodeValue.trim(),
  address : document.querySelector('ul.companyDetails li:nth-child(2) > br').nextSibling.nodeValue.trim() + " "
                  + document.querySelectorAll('ul.companyDetails li:nth-child(2) > br')[1].nextSibling.nodeValue.trim(),
  reg_date : document.querySelector('ul.companyDetails li:nth-child(3) > br').nextSibling.nodeValue.trim(),
  legal_form : document.querySelector('ul.companyDetails li:nth-child(4) > br').nextSibling.nodeValue.trim()


Bonus: you don't need JQuery :)


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.