简体   繁体   中英

Running JQuery in Chrome Extension

My html file:

<html><body>example !</body></html>

My manifest.json

    {
  "name": "Test",
  "version": "0.1",
  "description": "Test",
  "content_scripts": [
    {
      "matches": ["http://*/*","file://*/*"],
      "run_at": "document_end",
      "js": ["myscript.js","jquery-1.10.2.min.js"]
    }
  ],
  "manifest_version": 2
}

myscript.js file:

/*var divtest = document.createElement("div");
    divtest.innerHTML = "new div";
    divtest.id = "divTest";
    document.body.appendChild(divtest);*/

$("body").append("Test");

The JS commented-out code works. The JQuery on the other hand does nothing.

The problem is that you are trying to run your script before jQuery is initialized, so by the time you run $("body") - $ is not "jQuery" yet. You need to load jQuery first:

{
  "name": "Test",
  "version": "0.1",
  "description": "Test",
  "content_scripts": [
    {
      "matches": ["http://*/*","file://*/*"],
      "run_at": "document_end",
      "js": ["jquery-1.10.2.min.js", "myscript.js"]
    }
  ],
  "manifest_version": 2
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM