When I write my code to the browser console, everything works fine, and when I integrate the code into an extension, I get an error. It seems that the extension starts first, and then the library, although I tried to add the library to the extension itself, did not help.

Closed due to the fact that off-topic participants VenZell , Streletz , Grundy , D-side , sercxjo 31 May '16 at 12:34 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - VenZell, Streletz, Grundy, D-side, sercxjo
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • How did you try to add libraries to the extension? - Pavel Mayorov
  • through the manifest. - SloGS
  • Did you add to the manifest before your file? - Mike
  • Yes of course! but for some reason it still fails to solve this problem - SloGS

2 answers 2

First, the page variables are accessible in the console, but not from the extension. If the $ variable is defined on the page, it will be used.

Secondly, in the console, $ has a special meaning.

Write in console

 $ 

And see the result.

Of the most common options:

  1. jQuery

     function (a,b){return new e.fn.init(a,b,h)} 
  2. A special wrapper over document.querySelector , available only in the console:

     function $(selector, [startNode]) { [Command Line API] } 
  • function(n,t){return t?t.querySelector.call(t,n):window.document.querySelector.apply(window.document,arguments)} - donkey - ReinRaus
  • @ReinRaus, oops .. I'm wrong. Still, the selector. - Qwertiy ♦
  • Thanks a lot, I had to copy the variable assignment from the sites to my extension - SloGS

The $ variable is usually declared by the well-known jQuery library. Apparently, when testing in the console, you did this from an open page, with the jQuery library connected. The extension code works in a separate environment from the current page and cannot use any code located on the page, including the connected libraries.

If you need the functionality provided by any libraries, such as jQuery, connect these libraries clearly inside your extension. For example, add them to the project and describe in manifest.json for the chrome extension, or similar methods for extensions of other browsers.

In extreme cases, try the library text itself to add directly to the top of the code for your extension.

Also look at the source code for the well-known adBlock extension for example, it uses jQuery and includes this library.

  • Past Probably. - Qwertiy ♦
  • I tried to add to the file itself where my code and even the same error even pops up - SloGS
  • Thank you very much for your help, figured out - SloGS