Invoke a page function frome google chrome extension

问题: 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 fi...

问题:

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)"

Reffered : Chrome call function in content scripts from background.js

Calling Content Script function on chrome.browserAction.onClicked


回答1:

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);
  • 发表于 2019-03-21 05:11
  • 阅读 ( 113 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除