1

As a beginner, I am sorry to say that I don't know how to invoke a function which is defined in webpage index.html from a google chrome extension.

I want to change two fields in the page i am able to change one textbox field, the second one is a selection box and there is a jquery function to be invoked to add items in selection box.Is it possible to invoke it from my extension.

My manifest file

{
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
      "tabs", "https://*/*"
    ],
    "content_scripts": [
      {
        "matches": ["https://*/*"],
        "js": ["jquery-1.7.2.min.js","content.js"],
        "run_at": "document_end"
      }
  ]
}

My content.js

var subdist = document.getElementById('ContentPlace');

subdist.value="5673";

Webpage : index.php

The function to be invoked to update the selection box is :

onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolderMainContent$DropDownVillage\',\'\')', 0)"

Referred to: Chrome call function in content scripts from background.js

Calling Content Script function on chrome.browserAction.onClicked

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rahul R
  • 303
  • 2
  • 11

1 Answers1

2

With a content script you can inject a script tag into the DOM in order to access variables and functions in the original page as explained here

Like :

var script = document.createElement("script");
script.type="text/javascript";
script.innerHTML = "window.pageFunction(args);"
document.body.appendChild(script);
Krrish Yadav
  • 176
  • 1
  • 7