问题
I have started developing browser extensions. I noticed a common concept is that the extension can inject JS code into the current browser tab.
I am a bit puzzled of how that doesn't cause issues on a regular basis.
I mean, how can things still work if I inject version x of JQuery (through my browser extension) in a page that has already included version y of JQuery? Won't there be a conflict for the $()
function?
How is it possible things go that smoothly? Is there any particular technique the developer should employ to make sure such conflicts won't happen, or the browsers take care of everything?
回答1:
While noConflict()
may work most of the time for jQuery, it is still a bad idea for extensions and doesn't apply to most other libraries or utilities.
The smart thing to do, is do not inject code at all (with one exception1). The extension can include any libraries you need and there are many benefits to doing so:
There is no possibility of conflict with the page's javascript.
There is no dependency on the page's JS, which changes without notice, Your extension will break less often.
When you inject a library, it has to be fetched from some external server. This slows your extension and makes it vulnerable to crashes outside of your control.
When you include the library with your extension, it's: (a) always present, (b) always a known-good version, and (c) loads almost instantly from the local machine, not some far-off server.
Your extension can run, even if the page's javascript is disabled.
Your extension can take advantage of the library and the extension APIs, simultaneously.
Incorporating a library, like jQuery is easy with an extension. For example, with Chrome:
- Copy the library file(s) to your extension folder.
Add the library to the
manifest.json
file.A simple manifest that incorporated jQuery could be:
{ "content_scripts": [ { "exclude_globs": [ ], "include_globs": [ "*" ], "js": [ "jquery.min.js", "myJS.js" ], "matches": [ "http://stackoverflow.com/*" ] } ], "description": "Hello world with jQuery", "name": "Hello world with jQuery", "version": "1" }
The mechanism is similar for Firefox and other browsers.
1 The one exception? For an extension, inject code if, and only if, all of these conditions are met:
- The extension is only for one particular site.
- You absolutely, positively, have to use some object/function that the target page creates.
- It is too complicated to easily duplicate the functionality in your extension.
- You don't mind having to rewrite or adjust your extension for every little change to the site's design.
- You can be sure that all users will have the site's javascript enabled.
- You can be sure that the site serving your library is reasonably fast and isn't offline too often.
回答2:
The common solution to this problem is to use jQuery.noConflict() to alias your jQuery to a different variable, ensuring it doesn't conflict with the page's $
.
回答3:
jQuery has a noConflict()
function that can be used to isolate jQuery from anything else using the $
namespace (such as a different instance of jQuery).
A more general approach to avoiding forward namespace pollution is to consider executing JavaScript within a closure.
(function () {
// Your Code Here
})();
This will prevent anything within the scope of the closure from being available outside of your own code.
来源:https://stackoverflow.com/questions/11509678/browser-extensions-how-can-injecting-javascript-code-into-a-page-work-without-c