简体   繁体   中英

jQuery is undefined in chrome extension?

I am trying to run a simple jQuery call in a js file while making my first chrome extension but not sure why I keep getting this error:

Uncaught ReferenceError: $ is not defined

Here is my manifest file:

{
  "manifest_version": 2,
  "name": "Getting started example",
  "version": "1.0",

"description": "This extension shows a Google Image search result for the current page",

 "background": {
    "scripts": ["jquery-2.2.0.min.js", "popup.js"],
     "persistent": false
  },

  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "Click here!"
  },
  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/"
  ]
}

Here is the onclick call in popup js:

chrome.browserAction.onClicked.addListener(function(tab) {
  console.log("FIRE");
  chrome.tabs.executeScript(null, {file: "testScript.js"});
});

Here is the test script where the error shows:

console.log('foo'); 
console.log($('#page-top'));

foo gets printed but then I get the error on $

The solution that worked for me (since I see this question asked a lot) is that it was not executed before the testScript... I changed it to this:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null, {file: "jquery-2.2.0.min.js"});
  chrome.tabs.executeScript(null, {file: "testScript.js"});
});

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