admin管理员组文章数量:1307189
I'm doing quite a bit of work in the developer tools, and like to use jQuery in the console to run code snippets. To inject jQuery into the page (and the console), I'm pasting this into the devtools console:
var j = document.createElement('script'); j.src = "//ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"; document.getElementsByTagName('head')[0].appendChild(j);
Is there a way to inject jQuery automatically into the developer tools console? Ideally, without affecting window.$
or window.jQuery
for the current page.
I'm doing quite a bit of work in the developer tools, and like to use jQuery in the console to run code snippets. To inject jQuery into the page (and the console), I'm pasting this into the devtools console:
var j = document.createElement('script'); j.src = "//ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"; document.getElementsByTagName('head')[0].appendChild(j);
Is there a way to inject jQuery automatically into the developer tools console? Ideally, without affecting window.$
or window.jQuery
for the current page.
-
1
Personally, I've been using a JS bookmarklet that will load it on click, but theoretically you could load it with a Greasemonkey script if you have the extension. Note that even when you use the code above it modifies
window.$
andwindow.jQuery
.$.noConflict()
might help if you want to usewindow.$
for something else. – arcyqwerty Commented Oct 7, 2014 at 22:17 - Yeah, I thought about doing a greasemonkey script, but that will impact all page loading. I wonder if there's some hidden option in DevTools to just inject a script into the console, without it affecting the document. – silverwind Commented Oct 7, 2014 at 22:19
-
Would definitely be interested in seeing that. If you're only working on certain pages/domains, you could always en/disable the script or modify the
@include
and@match
as you'd like, but for many independent pages, that could be a hassle. – arcyqwerty Commented Oct 7, 2014 at 22:22 - Your question is a little hard to understand but I use injection of jQuery in Selenium tests, like this: jonausten.info/2014/06/23/… – djangofan Commented Oct 7, 2014 at 22:26
- Rephrased the question a bit. I'm not using Selenium, just the plain built-in console of the DevTools. – silverwind Commented Oct 7, 2014 at 22:30
2 Answers
Reset to default 5The Developer Toolbar has an inject mand that will allow you to inject a script into the current page a bit more easily. It supports monly used libraries like jQuery and underscore. For more, see the docs I linked to.
If you wanted to always do this, you could create an add-on similar to dotjs - the main difference is that you would need to expose the jQuery object into the page's actual DOM, a content script isn't good enough. You should probably also always try to detect an existing jQuery? I'm not quite sure what you mean by 'without affecting window.$ or window.jQuery for the current page' - the current scope of the console by default is the current page. This is only different if you are debugging and are stopped at a breakpoint inside some other scope.
You can copy/paste the below code into the console and jQuery will be available to you in DevTools
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis./ajax/libs/jquery/2.2.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... You might need to also run
jQuery.noConflict();
You might immediately see an error: Uncaught ReferenceError: jQuery is not defined.
Ignore it - DevTools is pulling your leg. (Google's weak attempt at humor, maybe...)
Then, in DevTools console, test it:
$('div').length; //press Enter
If you get an error, try it this way:
jQuery('div').length
Hopefully, the first will work - but sometimes you'll need to use the second method.
本文标签: javascriptFirefox DevTools Automatically injecting jQueryStack Overflow
版权声明:本文标题:javascript - Firefox DevTools: Automatically injecting jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741834694a2400144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论