Reputation: 4482
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 $
Upvotes: 0
Views: 2848
Reputation: 4482
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"});
});
Upvotes: 2