开发者

jQuery and Chrome Extensions [closed]

开发者 https://www.devze.com 2023-03-21 10:54 出处:网络
Closed. This question开发者_运维知识库 is opinion-based. It is not currently accepting answers.
Closed. This question开发者_运维知识库 is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 6 years ago.

Improve this question

Is it smart to use jQuery as a JavaScript library in Chrome Extensions? jQuery and other DOM libraries are intended to hide the differences between browsers, and since I'm targeting Chrome only, should I use it?


There are no reasons to not use jQuery on one of your extension pages (background, popup, options etc). You should always use a local jQuery copy though, not load a remotely hosted one through http.

I would advise against injecting jQuery as a content script though, if possible. If all you need is some simple selector or event listener, injecting around 100kb of js code on each page visit is a bit of an overkill.


I use it for my Chrome extensions and I think it is very smart. jQuery is more than just hiding differences. It is full of features that allow you to code faster and more robustly. There are tons of extensions that allow you to extend your scripts. I think you would be hurting yourself by not using it!


In addition to coding faster and "Not reinventing the wheel (unnecessarily)" -- there may come a time when you need to work on something besides Chrome extensions.

jQuery makes it easier for you to reuse that effort on other platforms or in different circumstances.

Finally, by using a standard library, others will, more easily, be able to: help with, improve, or just plain trust, the code. (It won't be just yours, forever, if it's good.)


No, you should not use jQuery in Chrome extensions.

jQuery is fairly large and the vast majority of it is support for other browsers, something you just don't have to care about in an extension.

For instance you shouldn't need the Sizzle engine at all (use document.querySelector), though that's really valuable in IE6-8. You shouldn't need standardised event handlers as you won't be supporting IE or Opera at all (use element.addEventListener). You shouldn't need jQuery's deferred implementation when Chrome supports Promise natively. You don't need $.ajax when you have fetch. There are lots more.

In addition, jQuery doesn't support a lot of modern features, for instance you cannot use passive event handlers or load scripts asynchronously (it doesn't even support defer).

As the current selected answer also points out: jQuery is expensive to inject. It's worth optimising down to exactly what you need with minimal code.

As a general rule with any toolkit extension (jQuery, Underscore, etc) always start without them and only bring them in if you find you need them. If you do find that you need a framework consider more modern ones that leverage new functionality (that you can rely on being in Chrome) rather than ones designed to standardise code between browsers. Consider exactly what it is that you need from a framework - if it's just that you know and like jQuery then fine (familiarity can be a justification if there's a high cost for you/your team learning new things) but you should be aware of the related injection and execution cost in that decision.

jQuery is an exceptionally good toolkit with widespread support, it just isn't the right tool for Chrome-only extensions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号