简体   繁体   English

jQuery在Chrome扩展程序中未定义?

[英]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: 我尝试在进行我的第一个chrome扩展时在js文件中运行一个简单的jQuery调用,但不确定为什么我一直收到此错误:

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: 这是弹出式js中的onclick调用:

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 $ foo被打印出来,但是然后我得到了$的错误

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: 对我有用的解决方案(因为我看到很多问题了)是在testScript之前没有执行它...我将其更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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